簡體   English   中英

如何將 LocalDate 對象格式化為 MM/dd/yyyy 並保持格式

[英]How to format LocalDate object to MM/dd/yyyy and have format persist

我正在閱讀文本並將日期存儲為 LocalDate 變量。

有什么方法可以讓我保留 DateTimeFormatter 的格式,這樣當我調用 LocalDate 變量時它仍然是這種格式。

編輯:我希望 parsedDate 以 25/09/2016 的正確格式存儲,而不是作為字符串打印

我的代碼:

public static void main(String[] args) 
{
    LocalDate date = LocalDate.now();
    DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
    String text = date.format(formatters);
    LocalDate parsedDate = LocalDate.parse(text, formatters);

    System.out.println("date: " + date); // date: 2016-09-25
    System.out.println("Text format " + text); // Text format 25/09/2016
    System.out.println("parsedDate: " + parsedDate); // parsedDate: 2016-09-25

    // I want the LocalDate parsedDate to be stored as 25/09/2016
}

編輯:考慮到您的編輯,只需將 parsedDate 設置為等於您的格式化文本字符串,如下所示:

parsedDate = text;

LocalDate 對象只能以 ISO8601 格式 (yyyy-MM-dd) 打印。 為了以其他格式打印對象,您需要對其進行格式化並將 LocalDate 保存為字符串,就像您在自己的示例中演示的那樣

DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
String text = date.format(formatters);

只需在打印時格式化日期:

public static void main(String[] args) {
    LocalDate date = LocalDate.now();
    DateTimeFormatter formatters = DateTimeFormatter.ofPattern("d/MM/uuuu");
    String text = date.format(formatters);
    LocalDate parsedDate = LocalDate.parse(text, formatters);

    System.out.println("date: " + date);
    System.out.println("Text format " + text);
    System.out.println("parsedDate: " + parsedDate.format(formatters));
}

簡短的回答:沒有。

長答案: LocalDate是一個表示年、月和日的對象,這些是它將包含的三個字段。 它沒有格式,因為不同的語言環境將有不同的格式,並且它會使執行想要在LocalDate上執行的操作(例如添加或減去天數或添加時間)變得更加困難。

String 表示(由toString() )是關於如何打印日期的國際標准。 如果您想要不同的格式,您應該使用您選擇的DateTimeFormatter

不,您不能保留格式,因為您不能覆蓋 LocalDate 的 toString(LocalDate 的構造函數是私有的,不可能擴展)並且沒有一種方法可以持久更改 LocalDate 中的格式。

也許,您可以創建一個新類並使用靜態方法來更改格式,但是當您需要其他格式時,您必須始終使用 MyLocalDate.myToString(localDate) 而不是 localDate.toString()。

 public class MyLocalDate {
    
    public static String myToString(LocalDate localDate){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        return localDate.format(formatter);
    }   
}

當您調用時,您必須使用這種方式

FechaInicioTextField.setText(MyLocalDate.myToString(fechaFacturaInicial));

而不是

FechaInicioTextField.setText(fechaFacturaInicial.toString());

如果您可以擴展 LocalDate 並覆蓋toString()方法,那么這可能是可能的,但是 LocalDate 類是不可變的,因此(安全 oop)是最終的。 這意味着,如果您希望使用此類,則您可以使用的唯一toString()方法是上述方法(從 LocalDate 來源復制):

@Override
public String toString() {
    int yearValue = year;
    int monthValue = month;
    int dayValue = day;
    int absYear = Math.abs(yearValue);
    StringBuilder buf = new StringBuilder(10);
    if (absYear < 1000) {
        if (yearValue < 0) {
            buf.append(yearValue - 10000).deleteCharAt(1);
        } else {
            buf.append(yearValue + 10000).deleteCharAt(0);
        }
    } else {
        if (yearValue > 9999) {
            buf.append('+');
        }
        buf.append(yearValue);
    }
    return buf.append(monthValue < 10 ? "-0" : "-")
        .append(monthValue)
        .append(dayValue < 10 ? "-0" : "-")
        .append(dayValue)
        .toString();
}

如果您必須覆蓋此行為,您可以將 LocalDate 裝箱並使用您的自定義toString方法,但這可能會導致比它解決的問題更多的問題!

暫無
暫無

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

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