Java SimpleDateFormat format month as "2", "02", "Feb", "February"

Description

Java SimpleDateFormat format month as "2", "02", "Feb", "February"

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

public class Main {

  public static void main(String[] args) {

    Date date = new Date();

    // formatting month in M format like 1,2 etc

    SimpleDateFormat sdf = new SimpleDateFormat("M");

    System.out.println("Current Month in M format : " + sdf.format(date));

    sdf = new SimpleDateFormat("MM");
    System.out.println("Current Month in MM format : " + sdf.format(date));

    // formatting Month in MMM format like Jan, Feb etc.

    sdf = new SimpleDateFormat("MMM");
    System.out.println("Current Month in MMM format : " + sdf.format(date));

    // formatting Month in MMMM format like January, February etc.
    sdf = new SimpleDateFormat("MMMM");
    System.out.println("Current Month in MMMM format : " + sdf.format(date));

  }//from   www  . ja  v  a 2s  . c o m
}



PreviousNext

Related