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

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]

References

  1. SimpleDateFormat JavaDoc
  2. Java 8 – How to convert String to LocalDate
  3. Stackoverflow : simpledateformat parsing date with ‘Z’ literal
  4. Wikipedia : ISO 8601
  5. Time Zone and Offset Classes
  6. GMT VS UTC
  7. What is a Time Zone?
  8. Joda Time

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Faith Khumalo
Guest
Faith Khumalo

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.

ajay
Guest
ajay

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 ?

Pumpkin
Guest
Pumpkin

Awesome..!!

ik142
Guest
ik142

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 ?

Navneet
Guest
Navneet

Hi Mkyong, Thanks. This came in handy for me!

Aashish Soni
Guest
Aashish Soni

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 ;
}
}

Aashish Soni
Guest
Aashish Soni

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 .

Willy Makend
Guest
Willy Makend

Very clean

Nilesh Akhade
Guest
Nilesh Akhade

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.

Nat Gross
Guest
Nat Gross

Concise and clear.
Thanks!

trackback
Java 8 – How to convert String to LocalDate

[…] Related – How to convert String to java.util.Date in Java […]

chenchu
Guest
chenchu

How to I get 07/26/2016 to MM/dd/YYY formate like all formates.

mkyong
Guest
mkyong

YYY is what format?

Try this SimpleDateFormat JavaDoc
http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Kunal Varade
Guest
Kunal Varade

how do i convert the date like this 17-JUN-16 to 17-06-16

mkyong
Guest
mkyong

17-JUN-16 = dd-MMM-yy
17-06-16 = dd-MM-yy

Akhalesh Khare
Guest
Akhalesh Khare

As heading says convert String to Date but while writing formatter.format(date) it will always return String only

mkyong
Guest
mkyong

The above article is displaying the result with formatter.format(date), for demonstrate only.

To convert String to Date, use formatter.parse

nocdib
Guest
nocdib

Thanks. This came in handy for me!

jony
Guest
jony

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

senthil
Guest
senthil

[2015-06-25T12:51:43-04:00] which format

mkyong
Guest
mkyong
Megha
Guest
Megha

How can i covert an Object type to utils.date in java?

Kushal
Guest
Kushal

Hi, how to write “+5:30” of “2015-04-09T12:58:26+05:30” in dateformat pattern in java 6.

mkyong
Guest
mkyong

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

Angelo
Guest
Angelo

If the date string is “2014-10-05T15:23:01Z” ?

mkyong
Guest
mkyong

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);

Prem Pratick Kumar
Guest
Prem Pratick Kumar

Very helpful
Prem Pratick Kumar

Beri
Guest
Beri

man…. you rock!!

mkyong
Guest
mkyong

thanks.

Jalaram
Guest
Jalaram

Thanks… fantastic example

john
Guest
john

How to use “Date.parse(String)” method ,please? Is the argument possible to be a string returned by Date.toString() ?

mkyong
Guest
mkyong

The Date.parse(String) is desperated, DO NOT use this. Replace with SimpleDateFormat.parse(String)

Sumit
Guest
Sumit

how to 2014-01-12 date format to date type in java…

mkyong
Guest
mkyong

yyyy-MM-dd

Jojn
Guest
Jojn

retarid… usae String.format…

JPed
Guest
JPed

Thanks for a lot of useful examples and info

sumit
Guest
sumit

Thanks it did my work.

trackback
Java Date and Calendar examples

[…] Java – Convert String to Date […]

viswam
Guest
viswam

very useful for beginners and thanks

Danilo
Guest
Danilo

Thanks a lot, all the information on your site is very helpful.