簡體   English   中英

如何僅使用@DateTimeFormat 將日期@ConfigurationProperties 與時間綁定?

[英]How to bind date @ConfigurationProperties with time only using @DateTimeFormat?

spring-boot 的新功能。

我正在嘗試使用注釋@ConfigurationProperties 解析文件中的屬性。 我能夠解析日期字段以外的字段。

問題是我的屬性文件只有時間沒有日期。 即日期=09:30:00。

我可以用@DateTimeFormat(pattern = "HH:mm:ss") 解析它。 但問題是,它給出的日期為 date=Thu Jan 01 09:30:00 GST 1970。

我想將日期設為今天的日期,時間為 09:30:00。 可能嗎?

@ConfigurationProperties
public class Config {

    private int id;
    
    private int version;
        
    @DateTimeFormat(pattern = "HH:mm:ss")
    private Date date;
    
}

財產

id=12
version=2
date=09:30:00

為什么不使用只代表時間的類型呢?

    @DateTimeFormat(pattern = "HH:mm:ss")
    private LocalTime time;

    public LocalDateTime getDate() {
        return LocalDateTime.of(LocalDate.now(), time);
    } 

您得到的 output 符合預期,因為使用SimpleDateFormat將字符串解析為java.util.Date ,默認日期時間為January 1, 1970, 00:00:00 GMT

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) throws ParseException {
        String text = "09:30:00";
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date date = sdf.parse(text);
        System.out.println(date);
    }
}

我的時區(歐洲/倫敦)中的 output:

Thu Jan 01 09:30:00 GMT 1970

請注意, java.util.Date object 不是真正的日期時間 object 像現代日期時間類型 相反,它表示自稱為“紀元”的標准基准時間(即January 1, 1970, 00:00:00 GMT (或 UTC)以來的毫秒數。 當您打印 java.util.Date 的java.util.Date時,它的toString方法返回 JVM 時區中的日期時間,這是根據這個毫秒值計算的。 如果您需要在不同的時區打印日期時間,則需要將時區設置為SimpleDateFormat並從中獲取格式化字符串。 java.util date-time API 及其格式 API、 SimpleDateFormat不僅過時而且容易出錯,因為有很多這樣的事情。 建議完全停止使用它們並切換到現代日期時間 API 1

下面給出了幾個選項:

  1. 推薦:使用真正代表時間的LocalTime
    @DateTimeFormat(pattern = "HH:mm:ss")
    private LocalTime time;
  1. 骯臟的方式:將您的字段聲明為String並在您的業務邏輯中對其進行解析,使其容易出錯且骯臟。
    private String time;

我強烈建議不要使用第二個選項 go。

使用LocalTime的快速演示:

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String text = "9:30:00";

        // The optional part can be put inside square bracket
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("H:m[:s]", Locale.ENGLISH);
        LocalTime time = LocalTime.parse(text, dtfInput);

        // Default implementation of LocalTime#toString omits the seconds part if it is zero
        System.out.println(time);

        // Custom output
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.ENGLISH);
        String formatted = dtfOutput.format(time);
        System.out.println(formatted);
    }
}

Output:

09:30
09:30:00

Trail: Date Time了解有關現代日期時間 API 的更多信息。


1. For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project .

不要使用舊的和過時的 class 日期。 查看 package java.time ,特別是在您的情況下 - class LocalTime

將您的代碼更改為:@ConfigurationProperties public class Config {

private int id;

private int version;
    
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm:ss")
private LocalTime date;

}

這應該有效。 您可能需要添加以下依賴項:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>

這是這個問題的修改答案: Spring Data JPA - ZonedDateTime format for json serialization

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM