【问题标题】:How to convert String with no time into full date with time using DateTimeFormatter如何使用 DateTimeFormatter 将没有时间的字符串转换为有时间的完整日期
【发布时间】:2021-06-28 10:07:04
【问题描述】:

您好,我正在尝试使用 DateTimeFormatterString 转换为日期,
比如"20210628""Mon Jun 28 00:00:00 UTC 2021"
使用SimpleDateFormatter 可以轻松实现,但我想使用DateTimeFormatter 实现。

【问题讨论】:

  • 到目前为止你尝试过什么?什么没有奏效?请您展示一些尝试吗?
  • 开始了解如何将您的String 转换为LocalDate,然后根据您的要求了解如何转换为LocalDateTimeZonedDateTime
  • 是的,正如 deHaar 已经提到的,请展示你的尝试。如果您已经知道如何使用SimpleDateFormatter 进行操作,那么您已经很接近了。

标签: java datetimeformatter


【解决方案1】:

java.time

java.util 日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用,改用modern Date-Time API*

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

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 的更多信息。

【讨论】:

  • 我也有这个想法...但是 OP 在输出 String 中明确要求提供区域 ID UTC。我花了很长时间才知道这是怎么可能的 ;-)
  • UTC 和 GMT 是一回事。
  • 是的,我知道,但它们不是同一个术语... OP 也可以接受 Z
  • 但是,您的回答在这里增加了价值。
【解决方案2】:

另外一种使用 java.time 的方法...

使用DateTimeFormatterBuilder 来完全控制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

【讨论】:

    【解决方案3】:

    如果您已经有一个 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 2018-08-06
      • 1970-01-01
      • 2018-02-19
      相关资源
      最近更新 更多