
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SimpleDateFormat Format Codes in Java
The java.text.SimpleDateFormat class is used to format and parse a string to date and date to string.
Parsing a date string
One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object
- Instantiate this class by passing desired format string.
- Parse the date string using the parse() method.
Following is the list of letters for formatting with their descriptions and examples −
Letter |
Component |
Example |
G |
Era designator |
AD, BC |
y |
Year |
2005, 96 |
Y |
Week year |
2005, 96 |
M |
Month in year |
September, Sep, 09 |
L |
Month in year |
September, Sep, 09 |
w |
Week in year |
23 |
W |
Week in month |
3 |
D |
Day in year |
129 |
d |
Day in month |
27 |
F |
Day of week in month |
5 |
E |
Day in a week (name) |
Monday, Mon |
u |
Day in a week (number) |
1 |
a |
AM/PM |
Pm. AM |
H |
Hour in day (0-23) |
0, 22 |
k |
Hour in day (1-24) |
1, 12, 24 |
K |
Hour in am/pm (0-11) |
0, 5, 11 |
h |
Hour in am/pm (1-12) |
1, 5, 12 |
m |
Minute in hour |
25 |
s |
Second in minute |
24 |
S |
Millisecond |
756 |
z |
Time zone |
pST, GMT |
Z |
Time zone |
-0500 |
X |
Time zone |
-06, -0600, -06:00 |
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat("yyyy/dd/MM"); Date date = formatter.parse("2007/25/06"); System.out.println("Date value: "+date); formatter = new SimpleDateFormat("y:G"); date = formatter.parse("1920:BC"); System.out.println("Date value: "+date); formatter = new SimpleDateFormat("D-M-Y"); date = formatter.parse("25-05-1989"); System.out.println("Date value: "+date); } }
Output
Date value: Mon Jun 25 00:00:00 IST 2007 Date value: Sun Jan 01 00:00:00 IST 1920 Date value: Sun Jan 01 00:00:00 IST 1989
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Sample { public static void main(String args[]) throws ParseException { SimpleDateFormat formatter1 = new SimpleDateFormat("HH:mm:ss"); Date time1 = formatter1.parse("07:25:30"); System.out.println("Date value: "+time1); SimpleDateFormat formatter2 = new SimpleDateFormat("EEE:MMM-d:YYYY"); Date time2 = formatter2.parse("Sun:Jan-8:2018"); System.out.println("Date value: "+time2); SimpleDateFormat formatter3 = new SimpleDateFormat("hh 'o''clock' a"); Date time3 = formatter3.parse("09 o'clock AM"); System.out.println("Date value: "+time3); } }
Output
Date value: Thu Jan 01 07:25:30 IST 1970 Date value: Sun Dec 31 00:00:00 IST 2017 Date value: Thu Jan 01 09:00:00 IST 1970
Advertisements