1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
/* Formatting month using SimpleDateFormat This example shows how to format month using Java SimpleDateFormat class. Month can be formatted in M, MM, MMM and MMMM formats. */ import java.text.SimpleDateFormat; import java.util.Date; public class FormattingMonth { public static void main(String[] args) { //create Date object Date date = new Date(); //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)); } } /* Typical output would be Current Month in M format : 2 Current Month in MM format : 02 Current Month in MMM format : Feb Current Month in MMMM format : February */ |
Add Comment