Formatting date in custom formats using SimpleDateFormat - Java Date Time

Java examples for Date Time:SimpleDateFormat

Description

Formatting date in custom formats using SimpleDateFormat

Demo Code

 
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);
   /* www. ja  va  2 s  . c  o m*/
    sdf = new SimpleDateFormat("dd/MM/yyyy");
    strDate = sdf.format(date);
    System.out.println("formatted date in dd/MM/yyyy : " + strDate);
   
    sdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
    strDate = sdf.format(date);
    System.out.println("formatted date in mm-dd-yyyy hh:mm:ss : " + strDate);
   
  }
}
 

Result


Related Tutorials