4

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?

P5music
  • 3,197
  • 2
  • 32
  • 81
  • Does [his](https://stackoverflow.com/a/1661389/3636601) answer your question? – Jens Jan 12 '20 at 19:12
  • 1
    @Jens I edited the question, I need to show the format string as a template to the user – P5music Jan 12 '20 at 19:38
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 12 '20 at 19:39

2 Answers2

3

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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • I edited the question, I need to show the format string as a template to the user – P5music Jan 12 '20 at 19:38
  • Thank you for your patience, but I need also the current time, so I need a complete date&time string. Is it possible? – P5music Jan 12 '20 at 20:08
1

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161