SimpleDateFormat in Java is used to format Date in Java. You can format date on any String format based upon various attribute available in SimpleDateFormat class e.g. mm, dd, YY etc. You can also put timezone information in formatted Date using Z attribute of DateFormat class. SimpleDateFormat is sub class of DateFormat and provide format() and parse() method to convert Date to and from String in Java. Worth noting is that SimpleDateFormat is not thread-safe and should not be shared with others. Avoid using static SimpleDateFormat in Java classes. If you want to share SimpleDateFormat or want to make it thread-safe, you can use ThreadLocal variable in Java to avoid sharing SimpleDateFormat among multiple threads. parse() method of SimpleDateFormat throws ParseException if String input is not a valid date or can not be parsed into mentioned format.
How to format Date in Java?
In order to format dates using SimpleDateFormat, we first needs to define a String date format e.g. "dd-MM-yyyy" will print dates in that format e.g. 01-11-2012. You can defined format based upon identifiers supported by SimpleDateFormat class. e.g. d means day of month, y means year and M means Month of year.
The Javadoc of SimpleDateFormat has complete list of supported Date and Time patterns . Once you create a DateFormat, you can just call format() method which accept java.util.Date and returns String, which is formatted Date. In this way you can also convert Date to String in Java.
SimpleDateFormat formats date in same pattern which is provided to it while creating instance of SimpleDateFormat. You can also include time information e.g. hour, minutes and seconds while formatting dates by using HH, mm and SS time pattern. DateFormat class also supports inclusion of timezone in formatted date String by z and Z.
If you use z you can show timezone information as abbreviation e.g. PST, IST etc. If you use Z you provide timezone, relative to GMT e.g. +0530. In next section we will see couple of SimpleDateFormat example in Java to get hands on on date formatting.
The Javadoc of SimpleDateFormat has complete list of supported Date and Time patterns . Once you create a DateFormat, you can just call format() method which accept java.util.Date and returns String, which is formatted Date. In this way you can also convert Date to String in Java.
SimpleDateFormat formats date in same pattern which is provided to it while creating instance of SimpleDateFormat. You can also include time information e.g. hour, minutes and seconds while formatting dates by using HH, mm and SS time pattern. DateFormat class also supports inclusion of timezone in formatted date String by z and Z.
If you use z you can show timezone information as abbreviation e.g. PST, IST etc. If you use Z you provide timezone, relative to GMT e.g. +0530. In next section we will see couple of SimpleDateFormat example in Java to get hands on on date formatting.
SimpleDateFormat Example in Java
Here is complete code example of How to format Date in Java using SimpleDateFormat. In this example we will see simple date formatting without time information e.g. dd-MM-yyyy, Date formatting with time information with patterns like dd-MM-yyyy:HH:mm:SS and Dates with timezone information in it e.g. dd-MM-yyyy:HH:mm:SS z or dd-MM-yyyy:HH:mm:SS Z.
import java.text.SimpleDateFormat;
import java.util.Date;
/**
*
* Java program to show how to format date in Java using SimpleDateFormat
* Examples. Java allows to include date, time and timezone information
* while formatting dates in Java.
*
* @author http://java67.blogspot.com
*/
public class DateFormatExample {
public static void main(String args[]) {
// 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);
//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 is : Fri Nov 02 16:11:27 IST 2012
Today in dd-MM-yyyy format : 02-11-2012
Today in dd/MM/yy pattern : 02/11/12
Today in dd-MM-yy:HH:mm:SS : 02-11-12:16:11:316
Today in dd-MM-yy:HH:mm:SSZ : 02-11-12:16:11:316 +0530
import java.util.Date;
/**
*
* Java program to show how to format date in Java using SimpleDateFormat
* Examples. Java allows to include date, time and timezone information
* while formatting dates in Java.
*
* @author http://java67.blogspot.com
*/
public class DateFormatExample {
public static void main(String args[]) {
// 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);
//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 is : Fri Nov 02 16:11:27 IST 2012
Today in dd-MM-yyyy format : 02-11-2012
Today in dd/MM/yy pattern : 02/11/12
Today in dd-MM-yy:HH:mm:SS : 02-11-12:16:11:316
Today in dd-MM-yy:HH:mm:SSZ : 02-11-12:16:11:316 +0530
That's all on these SimpleDateFormat examples in Java. We have seen How to format date with time and timezone information in Java. Though using SimpleDateFormat is most easy way to format Date in Java but it also has its own set of problems. It's not thread-safe and should be used carefully.
Avoid using DateFormat as static variable, don't share SimpleDateFormat between multiple threads, the result is unpredictable if it's shared among multiple threads.
Other Java programming tutorials you may like
I was looking to format date in DD-MM-YYYY format and thanks to you, I know lot more than that. This is one of the best tutorial on formatting date in Java. Didn't know that we can show even timezone as part of date in Java. Kudos
ReplyDeleteDon't Use SimpleDateFormat, its buggy and creates lot of issue.
ReplyDeleteI need to format Date with Timestamp, which I am receiving as String, in format "25 JUL 2013 20:30". I know that, I first neeed to convert String to Date and than need to format, but I am getting ParseException as below :
ReplyDeletejava.text.ParseException: Unparseable date: "25 JUL 2013 20:30"
at java.text.DateFormat.parse(DateFormat.java:337)
I am using following code
try {
Date date = new SimpleDateFormat("dd MM yyyy HH:mm").parse(str);
System.out.println(date);
String formatedDate = new SimpleDateFormat("dd/MM/yyyy").format(date);
System.out.println(formatedDate);
} catch (ParseException ex) {
ex.printStackTrace();
}
Can you please let me how to format such dates, where is the problem?
Hi Rashmi,
ReplyDeleteYour approach is right, I think only issue is with format String, In order to show month in three letters, you should use "MMM" instead of "MM". I suggest try following format "dd MMM yyyy HH:mm", while coverting String "25 JUL 2013 20:30" to Date first. Just for information for others, month in year is represented by "M" but it can be used differently e.g. for a date of 25th July, differnet number of M prints month in differnet format :
M:7
MM:07
MMM:Jul or JUL
MMMM:January
MMMMM:J
I hope this helps.
SimpleDateFormat is not buggy, but it is NOT thread-safe and is expensive to create. I usually use a static ThreadLocal to resolve that issue where applicable.
ReplyDeleteMy input is String 1995-05-12 11:12:13,now I need to store the value in Data Base as DateTime type .I want output 1995-05-12 11:12:13 as Date type.
ReplyDeleteinput String 1995-05-12 11:12:13
required output Date 1995-05-12 11:12:13.
try this :
ReplyDeleteDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:SS).
Date d = df.parset(inputString);
First parse and then format:
ReplyDeleteString strDate = "2015-11-04 04:00:00";
DateFormat dfYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dfDDMMMYYYY = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println("Date is: "+dfDDMMMYYYY.format(dfYYYYMMDD.parse(strDate)));
Date is: 04-Nov-2015
How about detecting if it's "Today", "Tomorrow" etc. and replacing the date with this word? (and in all other cases print a date) - agenda-like style. Is there an automated way or should it be done manually?
ReplyDeleteError date ):
ReplyDelete> HH:mm:SS
ReplyDeleteThis is wrong through-out the article.
Capital S is milliseconds, so should be lowercase s in HH:mm:SS