3

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

5
  • 1
    HH would be 07, but why are you using minutes twice? Commented Nov 16, 2019 at 0:55
  • Try with '2019-05-03 07:05:03', as your date pattern Commented Nov 16, 2019 at 0:57
  • Upper case M for month and y for year Commented Nov 16, 2019 at 1: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? Commented Nov 16, 2019 at 9: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). Commented Nov 16, 2019 at 9:23

2 Answers 2

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

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

3 Comments

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
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){}}}
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.
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.

3 Comments

Change the Date or the pattern?
Also i think there are duplicates on SO, so you should link zehn and not Write a new answer.
@Jens You could easily be right about the dupe, so if you can find an original? I haven’t found any (yet).

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.