Format month using Java SimpleDateFormat class. Month can be formatted in M, MM, MMM and MMMM formats. - Java Date Time

Java examples for Date Time:SimpleDateFormat

Description

Format month using Java SimpleDateFormat class. Month can be formatted in M, MM, MMM and MMMM formats.

Demo Code


import java.text.SimpleDateFormat;
import java.util.Date;
 
public class Main {
 
  public static void main(String[] args) {
 
     Date date = new Date();
   //from   w  w w.  j  a  v a 2s . com
     //formatting month in M format like 1,2 etc
     String strDateFormat = "M";
     SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
     
     System.out.println("Current Month in M format : " + sdf.format(date));
     
     //formatting Month in MM format like 01, 02 etc.
     strDateFormat = "MM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MM format : " + sdf.format(date));
     
     //formatting Month in MMM format like Jan, Feb etc.
     strDateFormat = "MMM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MMM format : " + sdf.format(date));
     
     //formatting Month in MMMM format like January, February etc.
     strDateFormat = "MMMM";
     sdf = new SimpleDateFormat(strDateFormat);
     System.out.println("Current Month in MMMM format : " + sdf.format(date));
     
  }
}

Result


Related Tutorials