import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExample { public static void main(String args[]) { // This is how to get today date in Java Date today = new Date(); //If you print Date, you will get un formatted output System.out.println("Today is : " + today); //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); //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); //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); //SimpleDateFormat example - Date with timezone information DATE_FORMAT = new SimpleDateFormat("dd-MM-yy:HH:mm:SS Z"); date = DATE_FORMAT.format(today); System.out.println("Today in dd-MM-yy:HH:mm:SSZ : " + date); } }
Output:
Today: Thu Nov 02 16:11:27 IST 2012
Today, in the format dd-MM-yyyy: 02-11-2012
Today, in the format dd / MM / yy: 02/11/12
Today at dd-MM-yy: HH: mm: SS: 02-11-12: 16: 11: 316
Today at dd-MM-yy: HH: mm: SSZ: 02-11-12: 16: 11: 316 +0530
Nilesh Jadav Dec 16 '14 at 6:28 2014-12-16 06:28
source share