
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
Set Date Patterns with SimpleDateFormat in Java
The following pattern letters are defined (all other characters from 'A' to 'Z' and from 'a' to 'z' are reserved) for Date and Time in Java
Reference − Oracle Java
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 | 0 |
K | Hour in day (1-24) | Number | 24 |
K | Hour in am/pm (0-11) | Number | 0 |
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 | -800 |
X | Time zone |
The above pattern letters are combined to format the date and time. For example −
dd/MM/yyyy dd-MM-yyyy MM/dd/yyyy yyyy-MM-dd
The following is another example −
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class Demo { public static void main(String[] args) throws Exception { String pattern = "MM/dd/yyyy"; SimpleDateFormat dateFormat = new SimpleDateFormat(pattern); Date dt = dateFormat.parse("22/11/2018"); System.out.println(dateFormat.format(new Date())); } }
Output
11/22/2018
Advertisements