0

I've been trying to parse String into Date, But, The conversion isn't working.

For example, I've these Strings, retrieved from a webservice.

Dec 9, 2016 4:36:00 PM
Jan 12, 2017 11:36:15 AM

I've tried to use these formats, But, The conversion failed.

MMM d, yyyy HH:mm:ss aaa
MMM d, yyyy hh:mm:ss a

Where am I wrong?

I'm getting this exception
java.text.ParseException: Unparseable date: "Dec 9, 2016 4:36:00 PM"

Geferson
  • 115
  • 4
  • 6
    You don't format Strings into Dates - you format Dates into Strings. And you parse Strings into Dates. What exactly do you want to do, formatting or parsing? And what does "the conversion failed" mean - do you get an error? If so, then what's the error message? – Jesper Aug 13 '20 at 14:05
  • 2
    You don't need three `a`s, one is enough. – akuzminykh Aug 13 '20 at 14:07
  • 1
    And not HH (24 hours) but hh (12 hours) as AM/PM. – Joop Eggen Aug 13 '20 at 14:08
  • Sorry for expressing myself badly, I would like to parse a string to a date object. – Geferson Aug 13 '20 at 14:15
  • 1
    Please read [mcve] and enhance your question accordingly. – GhostCat Aug 13 '20 at 14:19
  • 1
    At this stage, you should stay away from the date types from `java.util` (and `java.text.SimpleDateFormat`) and stick to `java.time` types. – cassiomolin Aug 13 '20 at 14:24

2 Answers2

2

Please check the time format:

String dateString1 = "Dec 9, 2016 4:36:00 PM";
String dateString2 = "Jan 12, 2017 11:36:15 AM";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM d, yyyy h:mm:ss a");

LocalDateTime localDateTime1 = LocalDateTime.parse(dateString1, formatter);
LocalDateTime localDateTime2 = LocalDateTime.parse(dateString2, formatter);

System.out.println(localDateTime1);
System.out.println(localDateTime2);

See the format in this case (time part) is: h:mm:ss a (because the hour is 4, not 04), so the format hh:mm:ss a will fail

For your inputs, the print will be: 2016-12-09T16:36 2017-01-12T11:36:15

C.P.O
  • 803
  • 9
  • 23
  • Unfortunately is returning ```java.time.format.DateTimeParseException: Text 'Dec 9, 2016 4:36:00 PM' could not be parsed at index 0``` – Geferson Aug 13 '20 at 14:40
  • 1
    @Geferson Usage: it is *throwing* that exception. *Returning* a value is what a (non-void) *method* does when *not* throwing an exception. – Ole V.V. Aug 13 '20 at 15:58
2

This is just a variation of the @C.P.O answer and uses a slightly different pattern.

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.junit.Test;

public class blam
{
    private static final String VALUE1 = "Dec 9, 2016 4:36:00 PM";
    private static final String VALUE2 = "Jan 12, 2017 11:36:15 AM";

    @Test
    public void test()
    {
        DateTimeFormatter dateTimeFormatter;
        LocalDateTime value;

        dateTimeFormatter = DateTimeFormatter.ofPattern("MMM d, u h:m:s a");

        value = LocalDateTime.parse(VALUE1, dateTimeFormatter);
        System.out.println("value1. " + VALUE1 + " becomes: " + value);


        value = LocalDateTime.parse(VALUE2, dateTimeFormatter);
        System.out.println("value2. " + VALUE2 + " becomes: " + value);
    }
}

Some notes:

  • y is "year of era"
  • u is "year"
  • MMM is required. Neither "M" nor "MM" will work in your case.
  • I prefer "s" to "ss" because "s" will parse both 2 and 12, but "ss" will fail to parse "2"

I don't know the real impact of using 'y' instead of 'u'.

DwB
  • 33,855
  • 10
  • 50
  • 78
  • I'm getting this exception ```java.time.format.DateTimeParseException: Text 'Dec 9, 2016 4:36:00 PM' could not be parsed at index 0``` – Geferson Aug 13 '20 at 14:41
  • What version of Java are you using? I'm running v8. – DwB Aug 13 '20 at 14:46
  • I'm running with Java8 too – Geferson Aug 13 '20 at 15:09
  • 1
    The code I show above is correct. The pattern works for the sample dates you listed. – DwB Aug 13 '20 at 15:54
  • 1
    As to why it works for DwB and not for Geferson see the linked original question and its answers. Or more precsely [here](https://stackoverflow.com/questions/50526234/java-datetimeformatterbuilder-fails-on-testtime). – Ole V.V. Aug 13 '20 at 16:00
  • 1
    I was not setting ```Locale.English``` on DateTimeFormatter, doing this, the above code working for me. – Geferson Aug 13 '20 at 17:26
  • For the impact of usnig `y` instead of `u` see [This question: `uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?](https://stackoverflow.com/questions/41177442/uuuu-versus-yyyy-in-datetimeformatter-formatting-pattern-codes-in-java) – Ole V.V. Aug 13 '20 at 21:13