java - Portable way to guarantee that the year field of a DateFormat is only two digits -
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 android 4.1, provides me date in format (in localization. may different other countries)
19/07/2013
however, have shorter version 19/07/13
i not want hard code as
dd/mm/yy
as above way not portable across different countries. countries, month come before date.
is there portable way achieve so?
p/s not month/date order. there might other problem well. instance, china using 19-07-13 or 19-07-2013. there might more edge cases other countries, don't know.
simpledateformat dateformat= (simpledateformat) dateformat.getdateinstance(); dateformat.applypattern(dateformat.topattern().replaceall("y{4}", "yy")); explanation:
applypattern(string pattern) applies given pattern string date format.
dateformat.topattern() gets current pattern
dateformat.topattern().replaceall(string regex, string replacement) returns current pattern, regex replaced replacement.
"y{4}" looks through date format pattern series of 4 y's, ,
"yy" says if see 4 y's, replace them 2 instead.
hope helped. luck.
edit:
mh pointed out, since android, more appropriate use:
simpledateformat dateformat = (simpledateformat) android.text.format.dateformat.getdateformat(getapplicationcontext()); this should work fine, since above method call returns dateformat of java.text.dateformat, not of android.text.format.dateformat.
Comments
Post a Comment