Use various format


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

public class Main{

  public static void main(String args[]) {
    Date date = new Date();
    SimpleDateFormat sdf;
    sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("EEE, MMM d, ''yy");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("h:mm a");
    System.out.println(sdf.format(date));

    sdf = new SimpleDateFormat("hh 'o''clock' a, zzzz" );
    System.out.println(sdf.format(date));

    sdf = new SimpleDateFormat("K:mm a, z");
    System.out.println(sdf.format(date));

    sdf = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
    System.out.println(sdf.format(date));

    sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    System.out.println(sdf.format(date));

    sdf = new SimpleDateFormat("yyMMddHHmmssZ");
    System.out.println(sdf.format(date));

    sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    System.out.println(sdf.format(date));

  }
}

The output:


2010.10.30 AD at 10:36:09 PDT
Sat, Oct 30, '10
10:36 AM
10 o'clock AM, Pacific Daylight Time
10:36 AM, PDT
02010.October.30 AD 10:36 AM
Sat, 30 Oct 2010 10:36:09 -0700
101030103609-0700
2010-10-30T10:36:09.656-0700

MM/dd/yy

 


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

public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
    String strDate = sdf.format(date);
    System.out.println("formatted date in mm/dd/yy : " + strDate);

  }
}

 

The output:


formatted date in mm/dd/yy : 11/14/10

dd/MM/yyyy

 



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

public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    String strDate = sdf.format(date);
    System.out.println("formatted date in dd/MM/yyyy : " + strDate);
    
  }
}

 

The output:


formatted date in dd/MM/yyyy : 14/11/2010

MM-dd-yyyy hh:mm:ss

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

public class Main {
  public static void main(String[] args) {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
    String strDate = sdf.format(date);
    System.out.println("formatted date in MM-dd-yyyy hh:mm:ss:" + strDate);
    
  }
}

 

The output:


formatted date in MM-dd-yyyy hh:mm:ss:11-14-2010 10:02:55
Home 
  Java Book 
    Essential Classes  

SimpleDateFormat:
  1. SimpleDateFormat class introduction
  2. SimpleDateFormat formats date, day,
  3. Add AM PM to time format using SimpleDateFormat
  4. Formatting date in default formats using DateFormat
  5. Formatting day using SimpleDateFormat
  6. Formatting day of week using SimpleDateFormat
  7. Formatting hour using SimpleDateFormat
  8. Formatting hour using SimpleDateFormat: k
  9. Formatting Minutes using SimpleDateFormat
  10. Formatting month using SimpleDateFormat
  11. Formatting seconds using SimpleDateFormat
  12. Formatting TimeZone using SimpleDateFormat
  13. Formatting year using SimpleDateFormat
  14. Use various format
  15. Convert date string from one format to another format using SimpleDateFormat