Java Format Date

Example


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
//from w  ww .ja va  2  s. c om
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.





















Home »
  Java Tutorial »
    Data Format »




Java Formatter
Java Number Formatter