Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable way to guarantee that the year field of a DateFormat is only two digits

Tags:

java

android

Currently, I'm having

private ThreadLocal<DateFormat> shortDateFormat = new ThreadLocal<DateFormat>() {
    @Override protected DateFormat initialValue() {
        final DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT);
        return format;
    }
};

Using my Android 4.1, this provides me date in format (In my localization. It may look different for other countries)

19/07/2013

However, sometimes I would like to have a much shorter version like 19/07/13

I do not want to hard code as

dd/MM/yy

As the above way would not portable across different countries. Some countries, their month come before date.

Is there any portable way to achieve so?

p/s Not only month/date order. There might be other problem as well. For instance, China is using 19-07-13 or 19-07-2013. There might be more edge cases for other countries, but I don't know.

like image 569
Cheok Yan Cheng Avatar asked Oct 03 '22 12:10

Cheok Yan Cheng


2 Answers

SimpleDateFormat dateFormat= (SimpleDateFormat) DateFormat.getDateInstance();
dateFormat.applyPattern(dateFormat.toPattern().replaceAll("y{4}", "yy"));

Explanation:

applyPattern(String pattern) applies the given pattern string to this date format.

dateFormat.toPattern() gets the current pattern

dateFormat.toPattern().replaceAll(String regex, String replacement) returns the current pattern, with regex replaced by replacement.

"y{4}" looks through the date format pattern for a series of 4 y's, and
"yy" says that if you see 4 y's, replace them with 2 instead.

Hope that helped. Good luck.

EDIT:
As MH pointed out, since this is for android, it is probably more appropriate to use:

SimpleDateFormat dateFormat = (SimpleDateFormat)     
                 android.text.format.DateFormat.getDateFormat(getApplicationContext());

This should work fine, since the above method call returns a DateFormat of java.text.DateFormat, not of android.text.format.DateFormat.

like image 195
Steve P. Avatar answered Oct 07 '22 04:10

Steve P.


You should take a look at the functionality android.text.format.DateFormat provides, on top of the java.text.DateFormat.

In particular, the following method will be of interest:

  • getDateFormatOrder(Context context)

Javadoc:

Gets the current date format stored as a char array. The array will contain 3 elements (DATE, MONTH, and YEAR) in the order specified by the user's format preference. Note that this order is only appropriate for all-numeric dates; spelled-out (MEDIUM and LONG) dates will generally contain other punctuation, spaces, or words, not just the day, month, and year, and not necessarily in the same order returned here.

In other words, the method allows you to determine what order the day, month and year fields are in, according to the user's preference (which triumphs the user's locale, if you ask me). From there it should easy enough to figure out what 'short' format to use; i.e. dd/MM/yy or MM/dd/yy.

As pointed out by the documentation, the return value of the method is only useful in the context of all-numeric date representations. That should be fine in your case.

like image 34
MH. Avatar answered Oct 07 '22 03:10

MH.