The java.text.SimpleDateFormat
class helps you parse and output a date/time string in any custom format. To see how you can use this class, create DateFormatExample.java file with the following content:
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.text.ParseException; import java.util.Date; public class DateFormatExample { public static void main(String[] args) { // Make a new Date object. It will be initialized to the // current time. Date now = new Date(); // Print the result of toString() String dateString = now.toString(); System.out.println(" 1. " + dateString); // Make a SimpleDateFormat for toString()'s output. This // has short (text) date, a space, short (text) month, a space, // 2-digit date, a space, hour (0-23), minute, second, a space, // short timezone, a final space, and a long year. SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); // See if we can parse the output of Date.toString() try { Date parsed = format.parse(dateString); System.out.println(" 2. " + parsed.toString()); } catch(ParseException pe) { System.out.println("ERROR: Cannot parse \"" + dateString + "\""); } // Print the result of formatting the now Date to see if the result // is the same as the output of toString() System.out.println(" 3. " + format.format(now)); } }
After creating DateFileExample.java, compile the java file
$ javac DateFormatExample.javaand execute it as follows:
$ java DateFormatExample 1. Tue Nov 04 21:53:43 EST 2003 2. Tue Nov 04 21:53:43 EST 2003 3. Tue Nov 04 21:53:43 EST 2003You will see three identical strings from the program as shown above.
Now let us follow the program in more detail to see the above output is generated by the program. Inside main() the program first obtains the current time in the following line:
Date now = new Date();It then generates the first output line by converting the date to string using toString() of Date:
String dateString = now.toString();Note that the output of the above function call, "Tue Nov 04 21:53:43 EST 2003", is the default string representation of any Data object. After generating this string, the program creates a SimpleDateFormat to parse this string:
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");Note that the constructor of SimpleDateFormat takes a date/time format string as an input parameter. The date format string "EEE MMM dd HH:mm:ss zzz yyyy" specifies that this SimpleDateFormat will be used for a date/time formatted like "Tue Nov 04 21:53:43 EST 2003". More precisely, the meaning of each letter in the format string is provided in the following table:
Symbol | Meaning | Type | Example |
y | Year | Number | "yy" -> "03" "yyyy" -> "2003" |
M | Month | Text or Number | "M" -> "7" "M" -> "12" "MM" -> "07" "MMM" -> "Jul" "MMMM" -> "December" |
d | Day in month | Number | "d" -> "3" "dd" -> "03" |
h | Hour (1-12, AM/PM) | Number | "h" -> "12" "hh" -> "12" |
H | Hour (0-23) | Number | "H" -> "0" "HH" -> "00" |
k | Hour (1-24) | Number | "k" -> "24" "kk" -> "24" |
a | AM/PM | Text | "a" -> "AM" "aa" -> "AM" |
m | Minute | Number | "m" -> "7" "m" -> "15" "mm" -> "15" |
s | Second | Number | "s" -> "15" "ss" -> "15" |
z | Time zone | Text | "z" -> "EST" "zzz" -> "EST" "zzzz" -> "Eastern Standard Time" |
E | Day in week | Text | "EEE" -> "Tue" "EEEE" -> "Tuesday" |
a | AM/PM | Text | "a" -> "AM" "aa" -> "AM" |
Date parsed = format.parse(dateString);We can also output a Date variable as string by calling format() method of the SimpleDateFormat object in the above program:
System.out.println(" 3. " + format.format(now));