简体   繁体   English

DateTimeFormatter 不能应用于 (java.util.Date)

[英]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.我决定使用 DateTimeFormatter 而不是 SimpleDateFormat 因为我听说 SimpleDateFormat 不是线程安全的。 I have declared DateTimeFormatter inside Constant file as follows.我在常量文件中声明了 DateTimeFormatter 如下。

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 "因为它说 DateTimeFormatter 中的格式 (java.time.temporal.TemporalAccessor) 不能应用于 (java.util.Date) 并返回一些这样的错误,“ 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.谢谢你。

new Date(date.get()) would mean that date.get() returns a long with the milliseconds-since-epoch. new Date(date.get())意味着date.get()返回一个带有自纪元以来的毫秒数的long

Since you're using DateTimeFormatter from the new Java Time API, you must give it a date object from that same API.由于您使用的是来自新 Java 时间 API 的DateTimeFormatter ,因此您必须从同一 API 为其提供一个日期对象。 Instant is such an object that can be created with a milliseconds-since-epoch value. Instant是这样一个对象,可以使用自纪元以来的毫秒值来创建。

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.但是,您使用的格式字符串不能与Instant一起使用,因此您首先需要在相关时区将其转换为ZonedDateTime 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.在下面的代码中,我们假设您需要 JVM 的默认时区,这是旧的java.util.Date API 将使用的。

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);

DateTimeFormatter is a part of java new time API. DateTimeFormatter是 java new time API 的一部分。 If you want to use DateTimeFormatter than you should consider using LocalDateTime instead of java.util.Date如果你想使用DateTimeFormatter ,那么你应该考虑使用LocalDateTime而不是java.util.Date

You can utilize the DateTimeFormator in your case like below:您可以在您的情况下使用DateTimeFormator ,如下所示:

String value = Constant.GENERAL_TZ_FORMATTER.format(LocalDateTime.now());
class Constant {
    public static final SimpleDateFormat GENERAL_TZ_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");

    public static void main(String[] args) {
        String value = Constant.GENERAL_TZ_FORMATTER.format(new Date());
        System.out.println(value);
    }
}

You can use, SimpleDateFormat as mentioned above.您可以使用上面提到的SimpleDateFormat

More about SimpleDateFormat ,you will get an idea by following link有关 SimpleDateFormat 的更多信息,您将通过以下link获得一个想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM