0

I want to convert a String 24 May 2020 07:40 AM to date format Mon May 24 07:40:55 IST 2020. I tried using Calendar and SimpleDateFormatter but didn't find a solution. Any help is appreciated.

I want the return type to be Date since I have to compare it with a couple of Dates.

6
  • 1
    I recommend you don’t use SimpleDateFormat and Calendar. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use LocalDateTime and DateTimeFormatter, both from java.time, the modern Java date and time API. Commented Jun 1, 2020 at 12:51
  • 1
    Where do you expect :55 and IST to come from when they are not in the original string? Or should they just always be :55 and IST? Commented Jun 1, 2020 at 12:54
  • LocalDateTime.parse("24 May 2020 07:40 AM", DateTimeFormatter.ofPattern("d MMM uuuu hh:mm a", Locale.ENGLISH)).atZone(ZoneId.of("Europe/Dublin")).format(DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH)) yields Sun May 24 07:40:00 IST 2020. Please break up into smaller statements. And be aware that more than one time zone can print as IST, so pick the right one for you. Commented Jun 1, 2020 at 13:00
  • 1
    Welcome to Stack Overflow. When you’ve tried something and it didn’t work, if you publish your attempt in your question along with the observed problem with it (error message, for example), you will generally find that people here acknowledge your effort and are much friendlier and more willing to help. Also when we can see what’s gone wrong for you, we can understand your situation much better and can write better answers. Commented Jun 1, 2020 at 13:18
  • 1
    @OleV.V. Your solution is printing the required Date in the required format, but i want the return type to be Date since i have to compare it with a couple of Dates. Your solution is returning it in String format. Commented Jun 2, 2020 at 7:12

1 Answer 1

1

java.time

When you’ve got some Date objects — likely from a legacy API that you cannot afford to upgrade to java.time just now — I still recommend that you use java.time, the modern Java date and time API, for your comparisons.

In the following example I am using Instant from java.time, but you may use ZonedDateTime or some other modern type too.

    DateTimeFormatter fromFormatter = DateTimeFormatter.ofPattern("d MMM uuuu hh:mm a", Locale.ENGLISH);

    Date anOldfashionedDate = new Date(1_590_286_000_000L);
    Date anotherOldfashionedDate = new Date(1_590_287_000_000L);
    System.out.println("The Date objects are " + anOldfashionedDate + " and " + anotherOldfashionedDate);

    String aString = "24 May 2020 07:40 AM";

    Instant instantFromDate = anOldfashionedDate.toInstant();
    Instant instantFromAnotherDate = anotherOldfashionedDate.toInstant();
    Instant instantFromString = LocalDateTime.parse(aString, fromFormatter)
            .atZone(ZoneId.of("Asia/Kolkata"))
            .toInstant();

    System.out.println("Comparing " + instantFromDate + " and " + instantFromString + ": "
            + instantFromDate.compareTo(instantFromString));
    System.out.println("Comparing " + instantFromAnotherDate + " and " + instantFromString + ": "
            + instantFromAnotherDate.compareTo(instantFromString));

Output is (when running in Asia/Kolkata time zone):

The Date objects are Sun May 24 07:36:40 IST 2020 and Sun May 24 07:53:20 IST 2020
Comparing 2020-05-24T02:06:40Z and 2020-05-24T02:10:00Z: -1
Comparing 2020-05-24T02:23:20Z and 2020-05-24T02:10:00Z: 1

An Instant prints in UTC; this is what its toString method generates. The trailing Z means UTC. Since India Standard Time is 5 hours 30 minutes ahead of UTC, 07:40 AM in India is the same time as 02:10 in UTC.

Given that you embark on using java.time now, you are well prepared when one day your legacy API gets upgraded to using java.time too.

The opposite conversion

If you do insist on using Date, to answer your question as asked, the opposite conversion is easy too:

    Date oldfashionedDateFromInstantFromString = Date.from(instantFromString);
    System.out.println("Converting to old-fashioned: " + oldfashionedDateFromInstantFromString);

Converting to old-fashioned: Sun May 24 07:40:00 IST 2020

Link

Oracle tutorial: Date Time explaining how to use java.time.

Sign up to request clarification or add additional context in comments.

1 Comment

My god, how do you always find the time to write those top quality answers for java.time? +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.