Java Format Date
Example
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/*from www. jav a 2 s . c o m*/
public class Main {
public static void main(String[] args) throws Exception {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println("dd/MM/yyyy:"+dateFormat.format(calendar.getTime()));
SimpleDateFormat formatter = new SimpleDateFormat("h");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("hh");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("H");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("HH");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("k");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("kk");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("K");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("KK");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("m");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("mm");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("M");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("MM");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("MMM");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("MMMM");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("s");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("ss");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("a");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("z");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("zz");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("zzz");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("zzzz");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("Z");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("d");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("dd");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("E");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("EEEE");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("hh:mm:ss a");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("HH.mm.ss");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("HH:mm:ss Z");
Date date = (Date) formatter.parseObject("22:14:02 -0500");
System.out.println(date);
formatter = new SimpleDateFormat("MM/dd/yy");
System.out.println(formatter.format(date));
formatter = new SimpleDateFormat("dd-MMM-yy");
System.out.println(formatter.format(date));
formatter = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(formatter.format(date));
formatter = new SimpleDateFormat("MM/dd/yy");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("yyyy");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("yy");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
System.out.println(formatter.format(new Date()));
formatter = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
System.out.println(formatter.format(new Date()));
}
}
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Get to know java.awt.BasicStroke
- JDK Version for java.awt.BasicStroke
- Fields from java.awt.BasicStroke
- Constructors from java.awt.BasicStroke
- Methods from java.awt.BasicStroke
Java Formatter Class
Java Format Specifier
Java Format Specifier Uppercase
Java Format Precision
Java Format Flags
Java Format Justifying Output
Java Format Negative and Positive
Java Format Line up Space
Java Format Parentheses
Java Format Zero Padding
Java Format Comma
Java Format Alternate Conversion
Java Format Argument Index
Java Format date time
Java Format Specifier
Java Format Specifier Uppercase
Java Format Precision
Java Format Flags
Java Format Justifying Output
Java Format Negative and Positive
Java Format Line up Space
Java Format Parentheses
Java Format Zero Padding
Java Format Comma
Java Format Alternate Conversion
Java Format Argument Index
Java Format date time