SimpleDateFormat formats date, day,


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("hh:mm:ss");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("dd MMM yyyy hh:mm:ss zzz");
    System.out.println(sdf.format(date));
    sdf = new SimpleDateFormat("E MMM dd yyyy");
    System.out.println(sdf.format(date));

  }
}

The output:


10:33:10
30 Oct 2010 10:33:10 PDT
Sat Oct 30 2010

Parsing date string into Date object using SimpleDateFormat


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

public class Main {
  public static void main(String[] args) {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
    try {
       Date date = sdf.parse("31/12/10");
      System.out.println(date);
    } catch (ParseException pe) {
      System.out.println("Parse Exception : " + pe);
    }
  }
}

The output:


Fri Dec 31 00:00:00 PST 2010
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