Java examples for Date Time:Legacy Date Format
Formatting a Date Using a Custom Format
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;/* w w w .ja v a 2 s .c om*/ // The year formatter = new SimpleDateFormat("yy"); // 02 formatter = new SimpleDateFormat("yyyy"); // 2002 // The month formatter = new SimpleDateFormat("M"); // 1 formatter = new SimpleDateFormat("MM"); // 01 formatter = new SimpleDateFormat("MMM"); // Jan formatter = new SimpleDateFormat("MMMM"); // January // The day formatter = new SimpleDateFormat("d"); // 9 formatter = new SimpleDateFormat("dd"); // 09 // The day in week formatter = new SimpleDateFormat("E"); // Wed formatter = new SimpleDateFormat("EEEE"); // Wednesday // Get today's date Date date = new Date(); // Some examples formatter = new SimpleDateFormat("MM/dd/yy"); String s = formatter.format(date); System.out.println(s); formatter = new SimpleDateFormat("dd-MMM-yy"); s = formatter.format(date); System.out.println(s); // Examples with date and time; see also // Formatting the Time Using a Custom Format formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss"); s = formatter.format(date); System.out.println(s); formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z"); s = formatter.format(date); System.out.println(s); } }