How to use SimpleDateFormat in Java
**Package : **java.text.SimpleDateFormat
**Extends : **DateFormat class (java.text.DateFormat class extends java.text.Format, java.text.Format extends java.lang.Object)
**Implements : **Serializable Interface, Cloneable Interface.
**Brief Description : **SimpleDateFormat is class for formatting and parsing of date and time. It also allows us to convert the text to date or date to the text format in the user defined formats.
How to format a date object using SimpledateFormat
//creating the simpledateformat with desired pattern
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
//date object with present date
Date date = new Date();
//parsing the date object to the desired string format
String formattedString = format.format(date);
How to parse the String to date Object using SimpleDateFormat
//creating the simpledateformat with desired pattern
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
//date in string format
String dateString = "04/09/2014";
//Always when we are parsing we should surround with try catch
try {
Date parsingDate = format.parse(dateString);
} catch (ParseException e) {
System.out.println(e.getMessage());
}
** Different date and time patterns for SimpleDateFormat**
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G | Era designator | Text | AD |
y | Year | Year | 1996; 96 |
Y | Week year | Year | 2009; 09 |
M | Month in year | Month | July; Jul; 07 |
w | Week in year | Number | 27 |
W | Week in month | Number | 2 |
D | Day in year | Number | 189 |
d | Day in month | Number | 10 |
F | Day of week in month | Number | 2 |
E | Day name in week | Text | Tuesday; Tue |
u | Day number of week (1 = Monday, …, 7 = Sunday) | Number | 1 |
a | Am/pm marker | Text | PM |
H | Hour in day (0-23) | Number | |
k | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | |
h | Hour in am/pm (1-12) | Number | 12 |
m | Minute in hour | Number | 30 |
s | Second in minute | Number | 55 |
S | Millisecond | Number | 978 |
z | Time zone | General time zone | Pacific Standard Time; PST; GMT-08:00 |
Z | Time zone | RFC 822 time zone | -0800 |
X | Time zone | ISO 8601 time zone | -08; -0800; -08:00 |
Example Program to show Different Date Formats
package com.javaindetail.simpledateformat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class SimpleDateFormatInDetail {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
System.out.println(dateFormat.format(date));
dateFormat = new SimpleDateFormat("EEE, MMM d, ''yy");
System.out.println(dateFormat.format(date));
dateFormat = new SimpleDateFormat("h:mm a");
System.out.println(dateFormat.format(date));
dateFormat = new SimpleDateFormat("hh 'o''clock' a, zzzz");
System.out.println(dateFormat.format(date));
dateFormat = new SimpleDateFormat("K:mm a, z");
System.out.println(dateFormat.format(date));
dateFormat = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
System.out.println(dateFormat.format(date));
dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
System.out.println(dateFormat.format(date));
dateFormat = new SimpleDateFormat("yyMMddHHmmssZ");
System.out.println(dateFormat.format(date));
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
System.out.println(dateFormat.format(date));
}
}
Output
2014.09.04 AD at 01:12:53 IST
Thu, Sep 4, '14
1:12 AM
01 o'clock AM, India Standard Time
1:12 AM, IST
02014.September.04 AD 01:12 AM
Thu, 4 Sep 2014 01:12:53 +0530
140904011253+0530
2014-09-04T01:12:53.270+0530
Example to parse the String to date object using SimpleDateFormat
package com.javaindetail.simpledateformat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParsingInDetail {
public static void main(String[] args) {
// creating the simpledateformat with desired pattern
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
// creating date object
Date parsingDate = new Date();
// date in string format
String dateString = "01/10/2024";
// Always when we are parsing we should surround with try catch
try {
System.out.println("Date before parsing is " + parsingDate);
parsingDate = format.parse(dateString);
System.out.println("Date after parsing is " + parsingDate);
} catch (ParseException e) {
System.out.println(e.getMessage());
}
}
}
Output
Date before parsing is Thu Sep 04 01:21:24 IST 2014
Date after parsing is Tue Oct 01 00:00:00 IST 2024
Enjoy Reading This Article?
Here are some more articles you might like to read next: