如何将时间字符串转换成日期和时间格式?

How to convert time string into date and time format?

我很难尝试将字符串时间格式化为 MM:DD::YY 并且只有时间。我从 IP 获取以下格式的时间

2021-09-10T00:37:42Z

我想显示日期和时间:

09/08/2021

时间

09:50PM

将给定的字符串解析为 OffsetDateTime,然后从中得到 LocalDateLocalTime 部分。

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

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2021-09-10T00:37:42Z";
        OffsetDateTime odt = OffsetDateTime.parse(strDateTime);
        LocalTime time = odt.toLocalTime();
        LocalDate date = odt.toLocalDate();
        System.out.println(time);
        System.out.println(date);

        // #########Custom formats #########

        DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
        String formattedDateString = date.format(dtfDate);
        System.out.println(formattedDateString);

        DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
        String formattedTimeString = time.format(dtfTime);
        System.out.println(formattedTimeString);
    }
}

输出:

00:37:42
2021-09-10
09/10/2021
12:37 AM

ONLINE DEMO

了解有关 modern Date-Time API* from Trail: Date Time 的更多信息。

根据 Ole V.V. 的重要评论更新:

The OP — or their user — may want the date and time in their own time zone.

为了获取特定时区的日期和时间部分,例如America/Los_Angeles,您应该将给定的日期时间字符串解析为 ZonedDateTime,并使用 ZonedDateTime#withZoneSameInstant 将其转换为特定时区的 ZonedDateTime。其余的事情将与原来的答案保持一致。

演示:

import java.time.LocalDate;
import java.time.LocalTime;
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) {
        String strDateTime = "2021-09-10T00:37:42Z";
        ZonedDateTime zdt = ZonedDateTime.parse(strDateTime);

        ZonedDateTime zdtLosAngeles = zdt.withZoneSameInstant(ZoneId.of("America/Los_Angeles"));

        LocalTime time = zdtLosAngeles.toLocalTime();
        LocalDate date = zdtLosAngeles.toLocalDate();
        System.out.println(time);
        System.out.println(date);

        // #########Custom formats #########

        DateTimeFormatter dtfDate = DateTimeFormatter.ofPattern("MM/dd/uuuu", Locale.ENGLISH);
        String formattedDateString = date.format(dtfDate);
        System.out.println(formattedDateString);

        DateTimeFormatter dtfTime = DateTimeFormatter.ofPattern("hh:mm a", Locale.ENGLISH);
        String formattedTimeString = time.format(dtfTime);
        System.out.println(formattedTimeString);
    }
}

输出:

17:37:42
2021-09-09
09/09/2021
05:37 PM

ONLINE DEMO


* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用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