Reputation: 4328
I'm trying to get a date string with the shortened form of the day of week and month, in addition to the day of month.
For example, a user with an English locale would see:
Tue Jun 17
and a user with a German locale would see:
Di. 17 Juni
I've been looking at the android.text.format.DateFormat
docs and the getBestDateTimePattern(Locale locale, String skeleton)
looked like it might work, but it requires API 18+, so I can't use it.
Is there a way to get this kind of short format that is based on the user's current locale?
Upvotes: 3
Views: 1455
Reputation: 338326
Use the Joda-Time library.
DateTimeFormatter formatter = DateTimeFormat.forStyle( "S-" ).withLocale( java.util.Locale.getDefault() ); // "S" for short date format. "-" to suppress the time portion. Specify locale for cultural rules about how to format a String representation.
DateTime dateTime = new DateTime( someDateObject, DateTimeZone.getDefault() ); // Convert a java.util.Date object to an org.joda.time.DateTime object. Specify time zone to assign to DateTime.
String output = formatter.print( dateTime ); // Generate String representation of date-time value.
Upvotes: 1
Reputation: 4328
Okay, I think I finally figured this out:
int flags = DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_NO_YEAR |
DateUtils.FORMAT_ABBREV_ALL |
DateUtils.FORMAT_SHOW_WEEKDAY;
dateTextView.setText(DateUtils.formatDateTime(this, millis, flags));
For an English locale, you get:
Wed, Jun 18
and for a German locale, you get:
Mi., 18. Juni
and for a French locale, you get:
mer. 18 juin
Upvotes: 9
Reputation: 38409
try below code:-
public class DateFormatDemoSO {
public static void main(String args[]) {
int style = DateFormat.MEDIUM;
//Also try with style = DateFormat.FULL and DateFormat.SHORT
Date date = new Date();
DateFormat df;
df = DateFormat.getDateInstance(style, Locale.UK);
System.out.println("United Kingdom: " + df.format(date));
df = DateFormat.getDateInstance(style, Locale.US);
System.out.println("USA: " + df.format(date));
df = DateFormat.getDateInstance(style, Locale.FRANCE);
System.out.println("France: " + df.format(date));
df = DateFormat.getDateInstance(style, Locale.ITALY);
System.out.println("Italy: " + df.format(date));
df = DateFormat.getDateInstance(style, Locale.JAPAN);
System.out.println("Japan: " + df.format(date));
}
}
for more info see the below link :-
http://www.java2s.com/Code/Java/Data-Type/DateFormatwithLocale.htm
Upvotes: 1
Reputation: 6409
You should use getMeduimDateFormat(Context)
, this obeys the current locale and the user's preferences.
Upvotes: 2