2

I have this code, it's a simple string that I want to parse it to a LocalDateTime

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

    public class DateClass {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {

            String dateRaw = "2019-05-03 7:05:03";        

            DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("uuuu-mm-dd HH:mm:ss").toFormatter(); 

            LocalDateTime date= LocalDateTime.parse(dateRaw, dtf);
            System.out.println(date.toString());
        }

    }

And when y runing, I have the next error:

Exception in thread "main" java.time.format.DateTimeParseException: Text '2019-05-03 7:05:03' could not be parsed at index 11
    at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    at java.time.LocalDateTime.parse(LocalDateTime.java:492)
    at lectordeachvio.DateClass.main(DateClass.java:18)

what I doing wrong? and why has a fault with de space????

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
Alberto D
  • 45
  • 1
  • 3
  • 1
    HH would be 07, but why are you using minutes twice? – OneCricketeer Nov 16 '19 at 00:55
  • Try with '2019-05-03 07:05:03', as your date pattern – newOne Nov 16 '19 at 00:57
  • Upper case M for month and y for year – Jens Nov 16 '19 at 01:08
  • @Jens Strictly speaking `y` is not for year. `u` is for a (signed) year. `y` is for year of era. See [`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. Nov 16 '19 at 09:19
  • It’s a good question. I think the expected result is clear, and the question has minimal. reproducible example and specific problem in the form of a well-formatted stack trace. Well done for a new Stacker. Also I think that this question may be useful for many future readers (if they can find it, I admit that it may be hard to search for). – Ole V.V. Nov 16 '19 at 09:23

2 Answers2

1
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd H:mm:ss");

With this change your program outputs:

2019-05-03T07:05:03

  • A single format pattern letter H will match hour of day in either 1 or two digits. That is, it will accept 7, 07, 13, etc. Two HH on the other hand requires two digits like 07 or 13, so 7 alone cannot be parsed. This was the reason for the exception that you got.
  • Index 11 of your string is not where the space is. It is where the 7 is. Indices are 0-based.
  • As others have mentioned you also need to use uppercase MM for month number. Lowercase mm is for minute of hour.
  • As an aside you don’t necessarily need a DateTimeFormatterBuilder for this case. DateTimeFormatter.ofPattern works OK.

Just out of curiosity, if your formatter is for parsing only, you may omit all repetitions of pattern letters. This works too:

    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s");

Normally we would not want this, though. We’d prefer to validate that there are two digits for minutes and seconds, often also for month and day of month. Putting two pattern letters accomplishes that.

Partly related question about mm in the format pattern: Convert LocalDate in DD/MM/YYYY LocalDate [duplicate].

Ole V.V.
  • 65,573
  • 11
  • 96
  • 117
  • 1
    Thank you very much, to all of you and specially to you Ole.V.V. it works using as you said String dateRaw = "2019-05-03 7:05:03"; DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("uuuu-MM-dd H:mm:ss").toFormatter(); Using this one: DateTimeFormatter dtf = DateTimeFormatter.ofPattern("u-M-d H:m:s"); I have a error – Alberto D Nov 19 '19 at 14:56
  • heres is the correct code: `public class DateClass { public static void main(String[] args) { String dateRaw = "2019-05-03 12:05:03"; DateTimeFormatter dtf = new DateTimeFormatterBuilder().appendPattern("uuuu-MM-dd H:mm:ss").toFormatter(); LocalDateTime date= LocalDateTime.parse(dateRaw, dtf); System.out.println("LocalDatetime ->"+date.toString()); try { Date date1 = new SimpleDateFormat("yyyy-MM-dd H:mm:ss").parse(dateRaw); System.out.println("Date ->"+ date1); }catch(Exception e){}}}` – Alberto D Nov 22 '19 at 17:48
  • Thanks for posting your code. Please for your own good don’t involve the `SimpleDateFormat` class, it’s a notorious troublemaker. And java,time gives you all the functionality you need and more. And don’t “swallow exceptions”: an empty catch block is a nono, it will hide errors from you, so it’s like coding blindfolded. – Ole V.V. Dec 08 '19 at 02:03
0

You should change your pattern regards to month, because you use minutes:

"uuuu-MM-dd HH:mm:ss"

Please see docs. And change in example date hour part from 7 to 07, as provided pattern.

newOne
  • 599
  • 1
  • 8
  • 26