1

I want to parse a timestamp in the form of yyyy-MM-dd'T'HH:mm:ss as LocalDateTime. When doing so, it strips the seconds if they are 00.

As described here, I need to use a custom formatter

LocalDateTime date = LocalDateTime.parse("2008-10-02T12:30:00");
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

String dateString = date.toString();
String dateFormatted = date.format(f);

System.out.println(dateString); // 2008-10-02T12:30

This one works, but it returns a String:

System.out.println(dateFormatted); // 2008-10-02T12:30:00

When I parse the string to LocalDateTime it strips the 00 again:

LocalDateTime dateLDT = LocalDateTime.parse(dateFormatted, f);
System.out.println(dateLDT); // 2008-10-02T12:30

So how can I parse a date as LocalDateTime, instead of String, and keep the 00 at the end?

Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
  • You need to parse `dateFormatted`, where is the problem? You cannot *parse* a `LocalDateTime`, you can only ever parse `String`s - and that `String`s have to have the correct format. And how a date chooses to overload its `toString` method should not matter to you since you always have to use a formatter to get any proper format. – luk2302 Mar 01 '19 at 09:45
  • I believe that [`DateTimeFormatterBuilder`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatterBuilder.html) provides for optional elements, but you'll need to play around with it – MadProgrammer Mar 01 '19 at 09:45
  • 1
    Seems you meant to print and parse `dateFormatted`, **not** `dateString` – ernest_k Mar 01 '19 at 09:46
  • @ernest_k: Please see my update. It strips the 00-es again. – Evgenij Reznik Mar 01 '19 at 09:49
  • 1
    **That Does Not Matter** - I repeat what I already said: "how a date chooses to overload its toString method should not matter to you since you always have to use a formatter to get any proper format." – luk2302 Mar 01 '19 at 09:50
  • @luk2302: The output of a formatter is a String, but I need a LocalDateTime. – Evgenij Reznik Mar 01 '19 at 09:54
  • `LocalDateTime` internally uses a format that we’re not concerned about, and it doesn’t make sense to talk about whether it has or hasn’t seconds in it. Only its `toString` method produced a string without seconds if seconds and fraction of seconds are zero. – Ole V.V. Mar 01 '19 at 09:58

2 Answers2

3

You should expect a difference in output between

LocalDateTime dateLDT = LocalDateTime.parse(dateFormatted, f);
System.out.println(dateLDT);

And

System.out.println(dateLDT.format(f)) //or f.format(dateLDT)

System.out.println(dateLDT); prints the value of dateLDT.toString(), which is not expected to produce the same output as your pattern.

When you look at LocalDateTime.toString(), you'll see that it delegates the time part to LocalTime.toString(), which prints seconds conditionally:

public String toString() {
    ...
    if (secondValue > 0 || nanoValue > 0) {
        buf.append(secondValue < 10 ? ":0" : ":").append(secondValue);
        ...
        }
    }
    return buf.toString();
}

It simply omits the seconds field if its value is 0.

What you need to do in this case is always use a DateTimeFormatter to format your date if you have to be certain about the output/input format.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • Hi @ernest_k I have a timestamp [ 2020-08-26T11:41:25.310 ]. How to extract date from it? I can use regex but is there an inbuilt method for it in new Java.time? – MdBasha Aug 27 '20 at 07:48
  • @MdBasha - `LocalDateTime.parse("2020-08-26T11:41:25.310").toLocalDate()` should return `2020-08-26` (as a `LocalDate` object). Just note that it works without a custom pattern because `2020-08-26T11:41:25.310` is in the default format used by `LocalDateTime` – ernest_k Aug 27 '20 at 09:08
  • Okay understand. Thank you! – MdBasha Aug 27 '20 at 09:27
1

Every date object has seconds. Wether or not it chooses to display them with the default toString implementation does not matter. If you want a specific format you always need to use a formatter.

If you want to see the seconds, either use a proper formatter or set a breakpoint and inspect the object. Looking at any toString implementation of anything is never guaranteed to display anything useful or represent the actual object state. The object could just return a random date string.

For now you can just use date, it is perfectly fine, it has seconds.

luk2302
  • 55,258
  • 23
  • 97
  • 137