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

47 comments on “How to convert String to Date – Java

  1. How to convert below string “2020-11-22T05:57:10+01:00” using Java8 ?

    Reply
  2. Hi
    How to handle the format “2020–03–01 3:15 pm”?

    Reply
  3. 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

    Reply
  4. 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.

    Reply
  5. Hello Sir,
    I would like to ask, how to convert six june 1990 to 06/06/1990. Thanks

    Reply
  6. Do any one know what will be the formatter for this: 2018-06-19 14:59:29.0T-07:00
    Thank you!

    Reply
  7. 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 ?

    Reply
  8. how to convert . . String dateInString = “tomorrow” or String dateInString = “today” into date

    Reply
  9. 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 ?

    Reply
  10. 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.

    Reply
  11. 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 ;
    }
    }

    Reply
    1. 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 .

      Reply
  12. 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.

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

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

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

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

      To convert String to Date, use formatter.parse

      Reply
  16. 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

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

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

    Reply
    1. 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);

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

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

      Reply
    2. 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

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

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *