简体   繁体   中英

How to get the date-time format string from current Locale, as a parameter for SimpleDateFormat

My Android app wants to display a date-time string and the corresponding format string. It uses the SimpleDateFormat because it is compatible with old Android API levels.

Calendar calendar=Calendar.getInstance();
formatString=getTheCurrentLocaleDateTimeFormatString();
SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
localeTimeString= dateFormat.format(calendar.getTimeInMillis());
displayToTheUser(localeTimeString);
displayToTheUser(formatString);

for example the user gets: "Wed, 4 Jul 2001 12:08:56" and "EEE, d MMM yyyy HH:mm:ss"

The provided code snippet is intended to get the date time form according to the current Locale but I do not know how to get it also as a format string. It should be calculated by the getTheCurrentLocaleDateTimeFormatString() method above.

I see that many format string patterns are available.

This is the relevant documentation page:

https://developer.android.com/reference/java/text/SimpleDateFormat#examples

Being that I want to display that format string to the user as a template and use it as a parameter for SimpleDateFormat my question is: how do I obtain the date time pattern format string of the current Locale?

You can do it as follows:

SimpleDateFormat getTheCurrentLocaleDateTimeFormatString() {
    return (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault());
}

A test program:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = getTheCurrentLocaleDateTimeFormatString();
        String localeTimeString = dateFormat.format(calendar.getTimeInMillis());
        System.out.println(localeTimeString);
    }

    static SimpleDateFormat getTheCurrentLocaleDateTimeFormatString() {
        return (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault());
    }
}

Output:

Sunday, 12 January 2020

[Update]

Posting the following code based on your comment:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Calendar calendar=Calendar.getInstance();
        String formatString=getTheCurrentLocaleDateTimeFormatString();
        System.out.println(formatString);
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
        String localeTimeString= dateFormat.format(calendar.getTimeInMillis());
        System.out.println(localeTimeString);
    }

    static String getTheCurrentLocaleDateTimeFormatString() {
        return ((SimpleDateFormat) DateFormat.getDateInstance(DateFormat.FULL, Locale.getDefault())).toLocalizedPattern();
    }
}

Output:

EEEE, d MMMM y
Sunday, 12 January 2020

[Another update]

Posting the following code based on your another comment:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        String formatString = getTheCurrentLocaleDateTimeFormatString();
        System.out.println(formatString);
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatString);
        String localeTimeString = dateFormat.format(calendar.getTimeInMillis());
        System.out.println(localeTimeString);
    }

    static String getTheCurrentLocaleDateTimeFormatString() {
        return ((SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL,
                Locale.getDefault())).toLocalizedPattern();
    }
}

Output:

EEEE, d MMMM y 'at' HH:mm:ss zzzz
Sunday, 12 January 2020 at 20:28:05 Greenwich Mean Time

java.time and ThreeTenABP

Code using java.time. the modern Java date and time API, can be made to work on old Android API levels too. DateTimeFormatterBuilder.getLocalizedDateTimePattern() gives you the format pattern string that you asked for. For example:

    ZonedDateTime dateTIme = ZonedDateTime.now(ZoneId.of("America/Recife"));

    Locale userLocale = Locale.forLanguageTag("pt-BR");
    String formatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
            FormatStyle.LONG, FormatStyle.LONG,
            IsoChronology.INSTANCE, userLocale);
    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern(formatPattern, userLocale);
    String localeTimeString = dateTIme.format(formatter);

    System.out.println(localeTimeString);
    System.out.println(formatPattern);

Just now the output from this snippet was:

 12 de Janeiro de 2020 17h7min52s BRT d' de 'MMMM' de 'yyyy H'h'm'min's's' z

The apostrophes in the format pattern enclose literal parts so de , h , min and s are not taken to be format pattern letters, but are output literally in the formatted date and time.

Question: Doesn't java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6 .

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It's called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM