Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Java DateTime class should I use?

We have a library used for generating reports. It reads from a data file (SQL, XML, JSON, etc.), the datetime may then be modified in a user written equation, and then it is formatted as specified for the report output.

The use in an equation can be add a timespan, get parts of the value as in "if date.month == 2", and pretty much all the datetime macros in Excel.

Because the data can be JSON (or XML with no schema) the datetime can be "2019-01-25", "2019-01-25T14:32:23", "2019-01-25T14:32:23.12345", "2019-01-25T14:32:23Z", or "2019-01-25T14:32:23Z-0500" (last two can have the ".12345" also).

If there's no timezone offset we assume the datetime is UTC. While that should be true, often it isn't and it's local time but the way it's used, it doesn't matter. So making it UTC unless a timezone offset is specified works (up till now we've used Date).

First question - what class should I use to hold this value? From what I've read I think ZonedDateTime, but maybe Instant?

Second question - what class should I use for timespan when I have to do something like add 3 days to the datetime?

Third question - is there some parser that can parse all the different strings as I listed above? Or do I need to call String.contains() to determine the format and then do an explicit pattern based on that? And if so, using what class?

like image 211
David Thielen Avatar asked Jan 25 '19 14:01

David Thielen


People also ask

What is the use of date class in Java?

Java provides the Date class available in java.util package, this class encapsulates the current date and time. The Date class supports two constructors as shown in the following table. Date( ) This constructor initializes the object with the current date and time.

How to deal with date and time with Java?

It provides constructors and methods to deal with date and time with java. Date () : Creates date object representing current date and time. Date (long milliseconds) : Creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT.

What is date-time Java?

This date-time Java tutorial describes how to use the java.time APIs introduced in JDK 8 to write date and time code. The core package uses the standard calendar as defined in the ISO calendar system.

What are the classes of the date-time API?

The Date-Time API provides four classes that deal exclusively with date information, without respect to time or time zone. The use of these classes are suggested by the class names: LocalDate, YearMonth, MonthDay, and Year. A LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time.


1 Answers

Third question - is there some parser that can parse all the different strings as I listed above? Or do I need to call String.contains() to determine the format and then do an explicit pattern based on that? And if so, using what class?

I might be horrible wrong, but can you use DateTimeFormatter with optional parts on pattern and parseBest method:

List<String> dates = List.of(
    "2019-01-25", 
    "2019-01-25T14:32:23",
    "2019-01-25T14:32:23.12345", 
    "2019-01-25T14:32:23Z", 
    "2019-01-25T14:32:23Z-0500"
);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
   "yyyy-MM-dd['T'[HH:mm:ss][.SSSSS]][z][x]"
); // all the possible combinations

dates.forEach( date -> {
            TemporalAccessor accessor = formatter.parseBest(date,                       
       OffsetDateTime::from, // going from most specific date
       LocalDateTime::from, 
       LocalDate::from); // to the less specific 

            System.out.println( accessor.getClass() + " " + accessor);
        }

);

// output for this is 
class java.time.LocalDate 2019-01-25
class java.time.LocalDateTime 2019-01-25T14:32:23
class java.time.LocalDateTime 2019-01-25T14:32:23.123450
class java.time.OffsetDateTime 2019-01-25T14:32:23Z
class java.time.OffsetDateTime 2019-01-25T14:32:23-05:00
like image 186
Anton Balaniuc Avatar answered Oct 14 '22 18:10

Anton Balaniuc