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

P5music :

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?

Arvind Kumar Avinash :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=358321&siteId=1