79515758

Date: 2025-03-17 21:19:33
Score: 0.5
Natty:
Report link

java.time

I warmly recommend that you use java.time, the modern Java date and time API, for all of your date and time work.

        String dateString = "2025-03-09T02:37:51.742";
        LocalDateTime ldt = LocalDateTime.parse(dateString);
        System.out.println(ldt);

Output from the snippet is:

2025-03-09T02:37:51.742

A LocalDateTime is a date and time without time zone or offset from UTC. So it doesn’t know summer time (DST) and happily parses times that happen to fall in the spring gap that happens in your own default time zone between 2 and 3 in the night on this particular date where the clocks are turned forward.

Don’t we need a formatter? No, not in this case. Your string is in the standard ISO 8601 format. Which is good. The classes of java.time generally default parse the most common ISO 8601 variants natively, that is, without any explicit formatter. So parsing proceeds nicely without one. You also notice in the output that LocalDateTime prints the same format back from its toString method, just like the other java.time types do.

Don’t use Date and the other legacy date-time types

The Date class was always poorly designed and has fortunately been outdated since java.time was launched with Java 8 more than 10 years ago. Avoid it.

Tutorial link

Oracle Tutorial: Trail: Date Time exlaining how to use java.time.

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Unregistered user (0.5):
  • Low reputation (1):
Posted by: Reem JOnes