-1

嗨,我正在尝试使用 将 a 转换String为日期DateTimeFormatter
例如"20210628"to "Mon Jun 28 00:00:00 UTC 2021"
它可以很容易地使用SimpleDateFormatter,但我想使用DateTimeFormatter.

4

3 回答 3

2

java.time

java.util日期时间 API 及其格式化 API已SimpleDateFormat过时且容易出错。建议完全停止使用它们并切换到现代 Date-Time API *

使用java.time现代日期时间 API 的解决方案:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("uuuuMMdd", Locale.ENGLISH);
        LocalDate date = LocalDate.parse("20210628", dtfInput);
        OffsetDateTime odt = date.atTime(OffsetTime.of(LocalTime.MIN, ZoneOffset.UTC));
        System.out.println(odt);

        // Custom format
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss O uuuu", Locale.ENGLISH);
        System.out.println(dtfOutput.format(odt));
    }
}

输出:

2021-06-28T00:00Z
Mon Jun 28 00:00:00 GMT 2021

ONLINE DEMO

或者,

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("uuuuMMdd", Locale.ENGLISH);
        LocalDate date = LocalDate.parse("20210628", dtfInput);
        ZonedDateTime zdt = date.atStartOfDay(ZoneId.of("Etc/UTC"));
        System.out.println(zdt);

        // Custom format
        DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss O uuuu", Locale.ENGLISH);
        System.out.println(dtfOutput.format(zdt));
    }
}

输出:

2021-06-28T00:00Z[Etc/UTC]
Mon Jun 28 00:00:00 GMT 2021

ONLINE DEMO

从Trail: Date Time了解有关现代日期时间 API 的更多信息。

于 2021-06-28T10:43:47.103 回答
1

一种不同的方法java.time也使用...

使用 aDateTimeFormatterBuilder来完全控制String转换。

这是一个真正打印UTC而不是GMTZ在所需输出中的小示例:

public static void main(String[] args) {
    // example String
    String value = "20210628";
    // define a formatter that parses the example String
    DateTimeFormatter dateParser = DateTimeFormatter.ofPattern("uuuuMMdd");
    // define a formatter that converts to a String as desired
    DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                                    .appendPattern("EEE MMM dd HH:mm:ss")
                                    .appendLiteral(' ')
                                    .appendZoneRegionId()
                                    .appendLiteral(' ')
                                    .appendPattern("uuuu")
                                    .toFormatter(Locale.ENGLISH);
    // parse the date and use the formatter in order to get the desired result
    String otherValue = LocalDate.parse(value, dateParser)
                                 // add the start of the day
                                 .atStartOfDay()
                                 // apply the desired zone
                                 .atZone(ZoneId.of("UTC"))
                                 // and format it 
                                 .format(dtf);
    // finally print the conversion
    System.out.println(value + " ---> " + otherValue);
}

输出如下:

20210628 ---> Mon Jun 28 00:00:00 UTC 2021
于 2021-06-28T10:49:38.650 回答
0

如果您已经有一个 LocalDateTime 对象,那么您可以使用以下内容。

String getFormattedDate(LocalDateTime datetime){

DateTimeFormatter formatterPreUTC = DateTimeFormatter.ofPattern("E MMM dd HH:mm:ss");
DateTimeFormatter formatterPostUTC = DateTimeFormatter.ofPattern("YYYY");

String textPreUTCPart = datetime.format(formatterPreUTC);
String utc = " UTC ";
String textPostUTCPart = datetime.format(formatterPostUTC);

return textPreUTCPart + utc + textPostUTCPart;
}
于 2021-06-28T10:34:17.240 回答