Marcus Biel
Marcus Biel

Reputation: 440

Print out localized date with timezone in Java

I want to create a localized version of the current date without time, but with the user's time zone, in Java.

java.time.format.FormatStyle has the values - FULL:

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

and LONG.

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

I basically want a mix of both - I just want to display the localized long date, like: January 12, 1952 - but aditionally, I want to add the time zone of this date, January 12, 1952 PST or January 12, 1952 MEZ and so on.

I tried:

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

But this did not add the timezone to the date. When I also print out the time, it works, but that's not what I want here in this case.

Upvotes: 1

Views: 655

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79085

Update

The OP has mentioned that he wants to use a locale-specific pattern but output in English. For this, DateTimeFormatterBuilder.getLocalizedDateTimePattern can be used to get the locale-specific pattern and then the same can be used in the DateTimeFormatter with Locale.ENGLISH.

Demo:

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));
    }
}

Output:

18 January 2021 PT

For testing: Change the value of userLocale to Locale.US in the above code and the output will change as follows:

January 18, 2021 PT

Original answer

Please note the following comment from Meno Hochschild, the Author of Time4J regarding the original answer:

A style-based version (your before-part) and a pattern-based version (your since-part) are not equivalent for any locale.

Before 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);
    }
}

Output:

18 janvier 2021, PST

Since Java-10:

You can use DateTimeFormatter#localizedBy​(Locale locale) as shown below:

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);
    }
}

Output:

18 janvier 2021, PST

Upvotes: 2

Related Questions