Disclosure: This text might include affiliate hyperlinks. Whenever you buy, we might earn a fee.
The way to format Date in Java?
The Javadoc of SimpleDateFormat has full checklist of supported Date and Time patterns . When you create a DateFormat, you possibly can simply name format() technique which settle for java.util.Date and returns String, which is formatted Date. On this approach you can even convert Date to String in Java.
SimpleDateFormat codecs date in identical sample which is offered to it whereas creating occasion of SimpleDateFormat. You may also embody time info e.g. hour, minutes and seconds whereas formatting dates through the use of HH, mm and SS time sample. DateFormat class additionally helps inclusion of timezone in formatted date String by z and Z.
Should you use z you possibly can present timezone info as abbreviation e.g. PST, IST and so forth. Should you use Z you present timezone, relative to GMT e.g. +0530. In subsequent part we’ll see couple of SimpleDateFormat instance in Java to get palms on on date formatting.
SimpleDateFormat Instance in Java
Right here is full code instance of The way to format Date in Java utilizing SimpleDateFormat. On this instance we’ll see easy date formatting with out time info e.g. dd-MM-yyyy, Date formatting with time info with patterns like dd-MM-yyyy:HH:mm:SS and Dates with timezone info in it e.g. dd-MM-yyyy:HH:mm:SS z or dd-MM-yyyy:HH:mm:SS Z.
import java.util.Date;
/**
*
* Java program to indicate learn how to format date in Java utilizing SimpleDateFormat
* Examples. Java permits to incorporate date, time and timezone info
* whereas formatting dates in Java.
*
* @creator http://java67.blogspot.com
*/
public class DateFormatExample {
public static void essential(String args[]) {
// That is learn how to get as we speak’s date in Java
Date as we speak = new Date();
//Should you print Date, you’ll get un formatted output
System.out.println(“In the present day is : “ + as we speak);
//formatting date in Java utilizing SimpleDateFormat
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(“dd-MM-yyyy”);
String date = DATE_FORMAT.format(as we speak);
System.out.println(“In the present day in dd-MM-yyyy format : “ + date);
//One other Instance of formatting Date in Java utilizing SimpleDateFormat
DATE_FORMAT = new SimpleDateFormat(“dd/MM/yy”);
date = DATE_FORMAT.format(as we speak);
System.out.println(“In the present day in dd/MM/yy sample : “ + date);
//formatting Date with time info
DATE_FORMAT = new SimpleDateFormat(“dd-MM-yy:HH:mm:SS”);
date = DATE_FORMAT.format(as we speak);
System.out.println(“In the present day in dd-MM-yy:HH:mm:SS : “ + date);
//SimpleDateFormat instance – Date with timezone info
DATE_FORMAT = new SimpleDateFormat(“dd-MM-yy:HH:mm:SS Z”);
date = DATE_FORMAT.format(as we speak);
System.out.println(“In the present day in dd-MM-yy:HH:mm:SSZ : “ + date);
}
}
Output:
In the present day is : Fri Nov 02 16:11:27 IST 2012
In the present day in dd-MM-yyyy format : 02-11–2012
In the present day in dd/MM/yy sample : 02/11/12
In the present day in dd-MM-yy:HH:mm:SS : 02-11–12:16:11:316
In the present day in dd-MM-yy:HH:mm:SSZ : 02-11–12:16:11:316 +0530
Different Java programming tutorials chances are you’ll like