使用正确时间将 Java 字符串时间戳转换为 12 小时 AM/PM

问题描述

我有一个字符串时间戳,如下所示, $('.AddFeature thead tr th').each(function (i) { var title = $(this).text(); var html = $($(this).closest("table").find("tbody tr td")[i]).html(); if (html.indexOf("btn-toggle") == -1 && html.indexOf("fa-trash-alt") == -1) { $(this).append('<input class="d-block" type="text" placeholder="Search ' + title + '" />'); $('input',this).on('keyup change',function () { if (table.column(i).search() !== this.value) { table .column(i) .search(this.value) .draw(); } }); } }); ,我试图将其转换为上午/下午 12 小时。我尝试了以下方法

2021-06-22T21:54:18.496Z

输出显示正确 String myTime = "2021-06-22T21:54:18.496Z"; var zoneddatetime = zoneddatetime.parse(myTime).format(DateTimeFormatter.ofPattern("MMMM dd,yyyy h:mm a")); System.out.println(zoneddatetime); 但时间不正确。我想我需要在解析之前先获取并设置正确的时区,但我不确定如何去做。

任何帮助将不胜感激。

解决方法

编辑: 我编辑了答案以展示如何使用 JVM 的当前默认时区和特定时区。

public static void main(String[] args) {
    String myTime = "2021-06-22T21:54:18.496Z";
    parseFormatAndPrintWithSpecificTimeZone(myTime);
    parseFormatAndPrintUsingDefaultTimeZone(myTime);
}

private static void parseFormatAndPrintUsingDefaultTimeZone(String myTime) {
    var zonedDateTime = ZonedDateTime.parse(myTime)
            .format(DateTimeFormatter.ofPattern("MMMM dd,yyyy h:mm:ss.SSS a").withZone(
                    ZoneId.systemDefault() // Get the JVM’s current default time zone. Can change at any moment during runtime. If important,confirm with the user.x
                    ));
    System.out.println("Time using my JVM current default time zone:"+ zonedDateTime);
    
}

private static void parseFormatAndPrintWithSpecificTimeZone(String myTime) {

    var zonedDateTime = ZonedDateTime.parse(myTime)
            .format(DateTimeFormatter.ofPattern("MMMM dd,yyyy h:mm:ss.SSS a").withZone(
                    ZoneId.of("Asia/Singapore") //Specified TimeZone
                    ));
    System.out.println("Time using specific timezone (singapore in this case):"+zonedDateTime);
    
}
,

tl;博士

替换您想要的 ZoneIdFormatStyleLocale

Instant
.parse( "2021-06-22T21:54:18.496Z" )
.atZone(
    ZoneId.of( "Europe/Istanbul" )             // Or ZoneId.systemDefault()
)
.format(
    DateTimeFormatter
    .ofLocalizedDateTime( FormatStyle.FULL ) 
    .withLocale( Locale.FRANCE )               // Or Locale.getDefault()
)

看到那个code run live at IdeOne.com

注意不同的日期和时间,但仍代表同一时刻,时间轴上的同一点。

mercredi 2021 年 6 月 23 日 à 00:54:18 Heure d'Europe de l'Est

详情

虽然 Answer by N.M. 是正确的,但我建议采用稍微不同的方法。

您的输入字符串 2021-06-22T21:54:18.496Z 是标准的 ISO 8601 格式。 T 将日期部分与时间部分分开。末尾的 Z 表示与 UTC 零时分秒的偏移量。 Z 发音为“Zulu”,意思是“祖鲁时间”。

这种特定类型的 Instant 字符串的适当类型。无需指定格式模式,因为 java.time 类在解析/生成文本时默认使用 ISO 8601 格式。

Instant instant = Instant.parse( "2021-06-22T21:54:18.496Z" ) ;

Instant 表示 UTC 中的时刻,始终采用 UTC,表示偏移为零。

请注意,您的输入和 Instant 使用 offset-from-UTC 但缺少 time zone。时区被命名为特定地区人民过去、现在和未来的偏移量变化的历史。 real time zone name 使用 Continent/Region 格式。

如果要从零偏移调整到特定时区,请指定 ZoneID 以生成 ZonedDateTime

ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

您可以选择在生成文本时指定一种格式来表示 ZonedDateTime 对象中的值。但我建议软编码,让 java.time 自动本地化。指定 Locale 以确定本地化问题。

Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale(locale ) ;
String output = zdt.format( f ) ;

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...