TechieHints SOFTWARE

How to parse a date in different format ?

Here is very good example for Java developers to parse date as per requirement.

// This is how to get today’s date in Java
Date today = new Date();

//If you print Date, you will get un formatted output
System.out.println(“Today is : ” + today);

// Prints: Today is : Tue Dec 29 18:39:06 IST 2015

//formatting date in Java using SimpleDateFormat
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(“dd-MM-yyyy”);
String date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yyyy format : ” + date);

// Prints: Today in dd-MM-yyyy format : 29-12-2015
//Another Example of formatting Date in Java using SimpleDateFormat
DATE_FORMAT = new SimpleDateFormat(“dd/MM/yy”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd/MM/yy pattern : ” + date);

// Prints: Today in dd/MM/yy pattern : 29/12/15

//formatting Date with time information
DATE_FORMAT = new SimpleDateFormat(“dd-MM-yy:HH:mm:SS”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yy:HH:mm:SS : ” + date);

// Prints: Today in dd-MM-yy:hh:mm:SS : 29-12-15:18:39:40

// Date with timezone information
DATE_FORMAT = new SimpleDateFormat(“dd-MM-yy:HH:mm:SSZ”);
date = DATE_FORMAT.format(today);
System.out.println(“Today in dd-MM-yy:HH:mm:SSZ : ” + date);

// Prints: Today in dd-MM-yy:HH:mm:SSZ : 29-12-15:18:39:40+0530