How to Format Time in MMMM Format using Java



Formatting the current month in the full month name (MMMM format) can be done by using the SimpleDateFormat class in Java. By specifying the pattern "MMMM", you can retrieve and display the month's name in full, like "January", "February", etc.

Displaying Current Month in MMMM Format

SimpleDateFormat: SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting.

Date class: The Java Util Date class represents a specific instant in time, with millisecond precision.

Steps

1. Create a Date object: Initializes a Date object that holds the current date and time.
Date date = new Date();
2. Define the date format: Creates a SimpleDateFormat object sdf with the pattern "MMMM" to format the month as a full name (e.g., "December").
SimpleDateFormat sdf = new SimpleDateFormat("MMMM");
3. Format the date: Uses the format method of sdf to format the current date into the full month name.
sdf.format(date);

Example

The following example formats the month with the help of SimpleDateFormat('MMMM') constructor and sdf.format(date) method of SimpleDateFormat class

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main{
   public static void main(String[] args) {
      Date date = new Date();
      SimpleDateFormat sdf = new SimpleDateFormat("MMMM");
      System.out.println("Current Month in MMMM format : " + sdf.format(date));
   }
}

Output

Current Month in MMMM format : May

Display Month in MMMM Format

We will format the current month name, but it uses the Format class for more flexibility. The result will vary depending on the current system date (e.g., "November").

Example

The following is another example of Month

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main { 
   public static void main(String[] argv) throws Exception {
      Format formatter = new SimpleDateFormat("MMMM"); 
      String s = formatter.format(new Date());
      System.out.println(s);
   }
}

Output

November
java_date_time.htm
Advertisements