Search code examples
javaexceptiondatetime-formatsimpledateformat

DateTimeFormatter cannot be applied to (java.util.Date)


I have decided to use a DateTimeFormatter instead of using SimpleDateFormat because I heard SimpleDateFormat is not thread-safe. I have declared DateTimeFormatter inside Constant file as follows.

public static final DateTimeFormatter GENERAL_TZ_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");

and inside my class file my implementation like this.

String value = Constant.GENERAL_TZ_FORMATTER.format(new Date(date.get()));

but seems like this is not correct implementation. Because it says format (java.time.temporal.TemporalAccessor) in DateTimeFormatter cannot be applied to (java.util.Date) and returns some error like this, "java.incompatible.types: java.util.Date cannot be converted to java.time.temporal.TemporalAccessor"

I'm doing background research for this and still struggling to find the best way to resolve it. I like to know your ideas about this and it will help me to find a better solution. Really appreciate your suggestions. Thank you.


Solution

  • new Date(date.get()) would mean that date.get() returns a long with the milliseconds-since-epoch.

    Since you're using DateTimeFormatter from the new Java Time API, you must give it a date object from that same API. Instant is such an object that can be created with a milliseconds-since-epoch value.

    However, the format string you're using cannot be used with an Instant, so you first need to convert it to a ZonedDateTime, in the relevant time zone. In the code below, we'll assume you want the default time zone of the JVM, which is what the old java.util.Date API would have used.

    long epochMilli = 1598336543358L;
    
    DateTimeFormatter GENERAL_TZ_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
    
    Instant instant = Instant.ofEpochMilli(epochMilli);
    ZonedDateTime dateTime = instant.atZone(ZoneId.systemDefault());
    String value = GENERAL_TZ_FORMATTER.format(dateTime);
           // or:  dateTime.format(GENERAL_TZ_FORMATTER)
    System.out.println(value);
    

    Since I'm in the US Eastern time zone, I get:

    2020-08-25 02:22:23 EDT
    

    As a single statement, you can write it as follows, but you'd probably format it over multiple lines to make it easier to read.

    // Single line
    String value = Instant.ofEpochMilli(epochMilli).atZone(ZoneId.systemDefault()).format(GENERAL_TZ_FORMATTER);
    
    // Formatted for readability
    String value = Instant.ofEpochMilli(epochMilli)
                          .atZone(ZoneId.systemDefault())
                          .format(GENERAL_TZ_FORMATTER);