How to get a datetime format string from the current locale as a parameter to SimpleDateFormat
My Android application wants to display a datetime string and the corresponding format string.
It uses SimpleDateFormat because it is compatible with older 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 code provided fragment is designed to get the datetime form based on the current locale, but I don’t know how to get it as a format string. It should be calculated by the getTheCurrentLocaleDateTimeFormatString() method above.
I see that there are many format string patterns available.
Here is the relevant documentation page:
https://developer.android.com/reference/java/text/SimpleDateFormat#examples
Since I want to display that format string as a template to the user and use as an argument to SimpleDateFormat, my question is: how do I get the datetime pattern format string for the current locale?
Solution
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].
Post the following code based on your comments:
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].
Post the following code based on your other 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