1

我想在 Java 中创建没有时间的当前日期的本地化版本,但使用用户的时区。

java.time.format.FormatStyle 具有值 - FULL:

Full text style, with the most detail. For example, the format might be 'Tuesday, April 12, 1952 AD' or '3:30:42pm PST'.

和长。

Long text style, with lots of detail. For example, the format might be 'January 12, 1952'.

我基本上想要两者的混合 - 我只想显示本地化的长日期,例如:1952 年 1 月 12 日 - 但此外,我想添加这个日期的时区,1952 年 1 月 12 日 PST 或 1952 年 1 月 12 日 MEZ等等。

我试过了:

ZonedDateTime dateTime = zonedDateTime(instant, timezoneId);
DateTimeFormatter dateFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(userLocale);
String localizedDate = dateTime.format( dateFormatter.withZone(timezoneId));

但这并没有将时区添加到日期。当我也打印出时间时,它可以工作,但在这种情况下,这不是我想要的。

4

1 回答 1

2

更新

OP 提到他想使用特定于语言环境的模式,但以英语输出。为此,DateTimeFormatterBuilder.getLocalizedDateTimePattern可用于获取特定于语言环境的模式,然后可以在DateTimeFormatterwith中使用相同的模式Locale.ENGLISH

演示:

import java.time.Instant;
import java.time.ZoneId;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        ZoneId timezoneId = ZoneId.of("America/Los_Angeles");
        Instant instant = Instant.now();
        Locale userLocale = Locale.forLanguageTag("fr");
        String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle.LONG, null,
                IsoChronology.INSTANCE, userLocale);
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH);
        System.out.println(instant.atZone(timezoneId).format(dtf) + " "
                + timezoneId.getDisplayName(TextStyle.SHORT_STANDALONE, Locale.ENGLISH));
    }
}

输出:

18 January 2021 PT

用于测试:userLocale将上述代码中的to值更改,Locale.US输出将更改如下:

January 18, 2021 PT

原始答案

请注意Time4J 的作者 Meno Hochschild关于原始答案的以下评论:

基于样式的版本(您的前部分)和基于模式的版本(您的后部分)对于任何语言环境都不等效。

Java-10 之前:

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
        Locale locale = Locale.forLanguageTag("fr");
        DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder()
                                            .appendLocalized(FormatStyle.LONG, null)
                                            .appendLiteral(", ")
                                            .appendZoneText(TextStyle.SHORT)                                            
                                            .toFormatter(locale);                                               
                                        
        String strLocalizedDate = dateTime.format(dateFormatter);
        System.out.println(strLocalizedDate);
    }
}

输出:

18 janvier 2021, PST

从 Java-10 开始:

你可以使用DateTimeFormatter#localizedBy​(Locale locale)如下图:

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) {
        ZonedDateTime dateTime = ZonedDateTime.now(ZoneId.of("America/Los_Angeles"));
        Locale locale = Locale.forLanguageTag("fr");
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd MMMM uuuu, z")
                                                            .localizedBy(locale);

        String strLocalizedDate = dateTime.format(dateFormatter);
        System.out.println(strLocalizedDate);
    }
}

输出:

18 janvier 2021, PST
于 2021-01-18T11:57:48.570 回答