How to convert String to Date – Java
In this tutorial, we will show you how to convert a String to java.util.Date
. Many Java beginners are stuck in the Date conversion, hope this summary guide will helps you in some ways.
// String -> Date
SimpleDateFormat.parse(String);
// Date -> String
SimpleDateFormat.format(date);
Refer to table below for some of the common date and time patterns used in java.text.SimpleDateFormat
, refer to this JavaDoc
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 |
Note
You may interest at this Java 8 example – How to convert String to LocalDate
You may interest at this Java 8 example – How to convert String to LocalDate
1. String = 7-Jun-2013
If 3 ‘M’, then the month is interpreted as text (Mon-Dec), else number (01-12).
TestDateExample1.java
package com.mkyong.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateExample1 {
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();
}
}
}
Output
Fri Jun 07 00:00:00 MYT 2013
07-Jun-2013
2. String = 07/06/2013
TestDateExample2.java
package com.mkyong.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateExample2 {
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateInString = "07/06/2013";
try {
Date date = formatter.parse(dateInString);
System.out.println(date);
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output
Fri Jun 07 00:00:00 MYT 2013
07/06/2013
3. String = Fri, June 7 2013
TestDateExample3.java
package com.mkyong.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateExample3 {
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();
}
}
}
Output
Fri Jun 07 00:00:00 MYT 2013
Fri, Jun 07 2013
4. String = Friday, Jun 7, 2013 12:10:56 PM
TestDateExample4.java
package com.mkyong.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateExample4 {
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();
}
}
}
Output
Fri Jun 07 12:10:56 MYT 2013
Friday, Jun 07, 2013 12:10:56 PM
5. String = 2014-10-05T15:23:01Z
Z suffix means UTC, java.util.SimpleDateFormat
doesn’t parse it correctly, you need to replace the suffix Z with ‘+0000’.
TestDateExample5.java
package com.mkyong.date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDateExample5 {
public static void main(String[] argv) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
String dateInString = "2014-10-05T15:23:01Z";
try {
Date date = formatter.parse(dateInString.replaceAll("Z$", "+0000"));
System.out.println(date);
System.out.println("time zone : " + TimeZone.getDefault().getID());
System.out.println(formatter.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Output
Sun Oct 05 23:23:01 MYT 2014
time zone : Asia/Kuala_Lumpur
2014-10-05T23:23:01+0800
In Java 8, you can convert it into a java.time.Instant
object, and display it with a specified time zone.
TestDateExample6.java
package com.mkyong.date;
import java.time.*;
public class TestDateExample6 {
public static void main(String[] argv) {
String dateInString = "2014-10-05T15:23:01Z";
Instant instant = Instant.parse(dateInString);
System.out.println(instant);
//get date time only
LocalDateTime result = LocalDateTime.ofInstant(instant, ZoneId.of(ZoneOffset.UTC.getId()));
System.out.println(result);
//get date time + timezone
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Africa/Tripoli"));
System.out.println(zonedDateTime);
//get date time + timezone
ZonedDateTime zonedDateTime2 = instant.atZone(ZoneId.of("Europe/Athens"));
System.out.println(zonedDateTime2);
}
}
Output
2014-10-05T15:23:01Z
2014-10-05T15:23:01
2014-10-05T17:23:01+02:00[Africa/Tripoli]
2014-10-05T18:23:01+03:00[Europe/Athens]
How to convert below string “2020-11-22T05:57:10+01:00” using Java8 ?
Hi I have a requirement to convert DateofBirth, StartDate and TerminationDate fields in a file with about 100 lines. The current date format is 00.00.0000 and the requirement is 00/00/0000.
Thanks. This came in handy for me!
Hi, in your program you have DateFormat defined as ‘dd-MMM-yyyy’ then how come 23-September-2015 is a valid date as well? I’ve tried setting up lenient to false but that still won’t work, any suggestions please?
Thanks
Hi
How to handle the format “2020–03–01 3:15 pm”?
Yoo solor quiero insertar una hora en mi base de datos y no entiendo nada!! el dato de mi base de datos es date, pero solo quiero pner hora por ejemplo 2:30 solo eso
In Method 4 4. String = Friday, Jun 7, 2013 12:10:56 PM
my requirement is if time is less than 12 (Friday, Jun 7, 2013 11:10:56 PM ) i am getting Unparseable date:
But in 12 hours format that is how we will write date.
Hello Sir,
I would like to ask, how to convert six june 1990 to 06/06/1990. Thanks
Do any one know what will be the formatter for this: 2018-06-19 14:59:29.0T-07:00
Thank you!
Hello Sir,
In example 5th the string i have passed is String = 14-10-05T15:23:01Z so in this case the output which i am getting is
Sun Oct 05 23:23:01 MYT 0014
time zone : Asia/Kuala_Lumpur
0014-10-05T23:23:01+0800
instead of parse exception which is causing data corruption. Please help how to handle this case ?
Awesome..!!
hello in my DB i Have declared month like a Long type ..so in my function I wan to use month like a Long value not String ..example : July i need it 07
How to proceed for that conversion ?
Hi Mkyong, Thanks. This came in handy for me!
Hello Sir,
Can you please guide me here –
import java.util.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/*Java date format issue
Input- 2016-11-23 23:38:03.850454 having milisec and nano in format
output- 20161123233803850454 in format YYYYMMDDHHmmssSSSSSS
my output- 201612361235213000454 which is incorrect , need help
*/
public class TestingDate
{
public static void main(String[] args) throws ParseException
{
TestingDate obj = new TestingDate();
System.out.println(obj.check(“2016-11-23 23:38:03.850454”));
}
public String check(String string) throws ParseException
{
String dateStr = string;
DateFormat srcDf = new SimpleDateFormat(“YYYY-MM-DD HH:mm:ss.SSSSSS”);
Date date = srcDf.parse(dateStr);
DateFormat destDf = new SimpleDateFormat(“YYYYMMDDHHmmssSSSSSS”);
dateStr = destDf.format(date);
return dateStr ;
}
}
I also tried this–
String dateStr = “2016-11-23 23:38:03.850454”;
DateFormat srcDf = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSSSSS”);
Date date = srcDf.parse(dateStr);
DateFormat destDf = new SimpleDateFormat(“yyyyMMddHHmmssSSSSSS”);
dateStr = destDf.format(date);
output- 20161123235213000454 — getting zero in place of 850
I also heard about JODA api but dont know how it gona help .
Very clean
5th example is exactly what I was looking for. But it does not cover the need “How to convert date of any format with timezone info into Instant”. Example only converts strings in the format DateTimeFormatter.ISO_INSTANT.
How should I convert date string from http header to java.util.Date? I am using DateTimeFormatter.RFC_1123_DATE_TIME.
Concise and clear.
Thanks!
How to I get 07/26/2016 to MM/dd/YYY formate like all formates.
YYY is what format?
Try this SimpleDateFormat JavaDoc
http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
how do i convert the date like this 17-JUN-16 to 17-06-16
17-JUN-16 = dd-MMM-yy
17-06-16 = dd-MM-yy
As heading says convert String to Date but while writing formatter.format(date) it will always return String only
The above article is displaying the result with formatter.format(date), for demonstrate only.
To convert String to Date, use formatter.parse
[2015-06-25T12:51:43-04:00] which format
ISO_OFFSET_DATE_TIME
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
How can i covert an Object type to utils.date in java?
Hi, how to write “+5:30” of “2015-04-09T12:58:26+05:30” in dateformat pattern in java 6.
Not sure about Java 6, try http://www.joda.org/joda-time/
Try upgrade to Java 8 and refer to the following predefined formatter – https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
If the date string is “2014-10-05T15:23:01Z” ?
Z suffix means UTC, in Java 8, you can convert to Instant class directly.
String date = "2014-10-05T15:23:01Z";
//convert to instant directly
Instant instant = Instant.parse(date);
//convert to a specified time zone
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Africa/Tripoli"));
System.out.println(zonedDateTime);
Very helpful
Prem Pratick Kumar
man…. you rock!!
thanks.
Thanks… fantastic example
How to use “Date.parse(String)” method ,please? Is the argument possible to be a string returned by Date.toString() ?
private static final DateFormat dateformat = new SimpleDateFormat(“dd-MM-yyyy HH:mm:ss”);
//input
Date date = new Date();
dateformat.format(date);
System.out.println(dateformat.format(date));
//output
03-02-2020 15:06:34
Time :class java.lang.Object
The Date.parse(String) is desperated, DO NOT use this. Replace with SimpleDateFormat.parse(String)
how to 2014-01-12 date format to date type in java…
yyyy-MM-dd
retarid… usae String.format…