In this tutorial, we will show you how to convert a String to java.util.Date. Many Java beginners Always find this problem of Date conversion. so we decided to post this solution !! let understand how to convert String Date in JAVA.
// String -> Date
SimpleDateFormat.parse(String);
// Date -> String
SimpleDateFormat.format(date);
java.text.SimpleDateFormat provide many date and time patterns :
Letter | Description | Examples |
y | Year | 2013 |
M | Month in year | July, 07, 7 |
d | Day in month | 1-31 |
E | Day name in week | Friday, Sunday |
a | Am/pm marker | AM, PM |
H | Hour in day | 0-23 |
h | Hour in am/pm | 1-12 |
m | Minute in hour | 0-60 |
s | Second in minute | 0-60 |
When we have a date string like : 9-Oct-2008
package com.example.demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main{
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
String dateInString = "7-Jun-2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
The output will be like :
Thu Oct 09 00:00:00 CEST 2008
09-Oct-2008
When we have a date string like : 09/10/2008
package com.example.demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Mainn {
public static void main(String[] args) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "09/10/2008";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
The output :
Thu Oct 09 00:00:00 CEST 2008
09-Oct-2008
When we have a String Date like : Fri, June 7 2013
package com.example.demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main{
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
String dateInString = "Fri, June 7 2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
The output will be like :
Fri Jun 07 00:00:00 MYT 2013
Fri, Jun 07 2013
When the string Date is like : Friday, Jun 7, 2013 12:10:56 PM
package com.example.demo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main{
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
String dateInString = "Friday, Jun 7, 2013 12:10:56 PM";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
the output :
Fri Jun 07 12:10:56 MYT 2013
Friday, Jun 07, 2013 12:10:56 PM
References
- Mkyong
- SimpleDateFormat JavaDoc
- Java 8 – How to convert String to LocalDate
- Stackoverflow : simpledateformat parsing date with ‘Z’ literal
- Wikipedia : ISO 8601
- Time Zone and Offset Classes
- GMT VS UTC
- What is a Time Zone?
- Joda Time
Also read : Convert String to int
Pingback: Convert Instant to ZonedDateTime in JAVA » JavaTuto