- Whenever are having a requirement of converting string (date as string ) in to date we need to use SimpledateFormat class which is present in java.text package.
- To convert string to date in java we need to follow two steps.
- First we need to create SimpledateFormat class object by passing pattern of date which we want as a string.
- Ex: SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
- And next we need to call parse() method on the object of SimpleDateFormat then it will returns Date object.
- parse() method throws ParseException so need to handle this exception.
- By using format method of the object will get string format of given date
- formatter.format(date).
- How to convert java string to date object
- Java SimpleDateFormat String to Date conversion and parsing examples
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G | Era designator | Text | AD |
y | Year | Year | 2015; 15 |
Y | Week year | Year | 2009; 09 |
M | Month in year | Month | July; Jul;07 |
w | Week in year | Number | 25 |
W | Week in month | Number | 3 |
D | Day in year | Number | 143 |
d | Day in month | Number | 37 |
F | Day of week in month | Number | 1 |
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 | 12 |
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 | 30 |
S | Millisecond | Number | 778 |
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 on SimpleDateFormat :
1. Convert string to date in java in MMM dd, yyyy format
Input : Jan 1, 2015 (MMM dd, yyyy):
- package com.instanceofjava;
- public Class DateFormatDemo{
- public static void main (String args[]) {
- SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");
- String dateInString = "Jan 1, 2015";
- try{
- Date date = formatter.parse(dateInString);
- System.out.println(date);
- System.out.println(formatter.format(date));
- }catch(ParseException e){
- e.printStackTrace();
- }
- }
- }
- Output:
- Thu Jan 01 00:00:00 IST 2015
- Jan 01, 2015
2.How to convert string into date format in java day MMM dd, yyyy format example
Input : Sat, Feb 14 2015 (E, MMM dd yyyy):
- package com.instanceofjava;
- public Class DateFormatDemo{
- public static void main (String args[]) {
- SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
- String dateInString = "Sat, February 14 2015";
- try{
- Date date = formatter.parse(dateInString);
- System.out.println(date);
- System.out.println(formatter.format(date));
- }catch(ParseException e){
- e.printStackTrace();
- }
- }
- }
- Output:
- Sat Feb 14 00:00:00 IST 2015
- Sat, Feb 14 2015
3. Convert string to date in java in dd/MM/ yyyy format example program
Input : 01/01/2015 (dd/MM/yyyy):
- package com.instanceofjava;
- public Class DateFormatDemo{
- public static void main (String args[]) {
- SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
- String dateInString = "01/01/2015";
- try{
- Date date = formatter.parse(dateInString);
- System.out.println(date);
- System.out.println(formatter.format(date));
- }catch(ParseException e){
- e.printStackTrace();
- }
- }
- }
- Output:
- Thu Jan 01 00:00:00 IST 2015
- 01/01/2015
4. Convert string to date in java in dd-MMM-yyyy format example program
Input : 1-Jan-2015 (dd-MMM-yyyy):
- package com.instanceofjava;
- public Class DateFormatDemo{
- public static void main (String args[]) {
- SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
- String dateInString = "1-Jan-2015";
- try{
- Date date = formatter.parse(dateInString);
- System.out.println(date);
- System.out.println(formatter.format(date));
- }catch(ParseException e){
- e.printStackTrace();
- }
- }
- }
- Output:
- Thu Jan 01 00:00:00 IST 2015
- 01-Jan-2015
5. Convert string to date in java in yyyyMMdd format example program
Input : 20150101 (yyyyMMdd):
- package com.instanceofjava;
- public Class DateFormatDemo{
- public static void main (String args[]) {
- SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
- String dateInString = "20150101";
- try{
- Date date = formatter.parse(dateInString);
- System.out.println(date);
- System.out.println(formatter.format(date));
- }catch(ParseException e){
- e.printStackTrace();
- }
- }
- }
- Output:
- Thu Jan 01 00:00:00 IST 2015
- 20150101
6.Convert string to date in java in ddMMyyyy format example program
Input : 01012015 (ddMMyyyy):
- package com.instanceofjava;
- public Class DateFormatDemo{
- public static void main (String args[]) {
- SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");
- String dateInString = "01012015";
- try{
- Date date = formatter.parse(dateInString);
- System.out.println(date);
- System.out.println(formatter.format(date));
- }catch(ParseException e){
- e.printStackTrace();
- }
- }
- }
- Output:
- Thu Jan 01 00:00:00 IST 2015
- 01012015
7.Convert string to date in java in dd-MMM-yy format example program
Input : 01-January-15 (dd-MMM-yy):
- package com.instanceofjava;
- public Class DateFormatDemo{
- public static void main (String args[]) {
- SimpleDateFormat formatter = new SimpleDateFormat("dd-MMMM-yy");
- String dateInString = "01-January-2015";
- try{
- Date date = formatter.parse(dateInString);
- System.out.println(date);
- System.out.println(formatter.format(date));
- }catch(ParseException e){
- e.printStackTrace();
- }
- }
- }
- Output:
- Thu Jan 01 00:00:00 IST 2015
- 01012015
No comments