I'm using foll开发者_运维百科owing code in my app:
Calendar tmpCalendar = Calendar.getInstance(Locale.getDefault());
String itemTime = String.format(
Locale.getDefault(),
"%1$tA, %1$te. %1$tB %1$tY",
tmpCalendar);
German is the default language on my device, so I'd expect to get something like:
Dienstag, 7. Juni 2011
Instead I'm getting:
3, 7. 6 2011
If I use Locale.US
instead of Locale.getDefault()
everything works fine. Am I doing something wrong?
Oddly, it works in an emulator running Android 2.2 in German, but not on a HTC Desire, also running 2.2. Why?
It seems to be a bug of some vendors and is issue #9453 in the Android issue tracker.
SimpleDateFormat sdf = new SimpleDateFormat("EE/MM/yyyy");
Date date = new Date();
String sDate= sdf.format(date);
Read more on it here
It does exactly what you ask it to do. If you want to have fine-grain control over the process, you can always use SimpleDateFormat. However, I would rather recommend this method:
Calendar tmpCalendar = Calendar.getInstance(Locale.getDefault());
DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
formatter.setTimeZone(TimeZone.getDefault());
String itemTime = formatter.format(tmpCalendar.getTime());
Also, I would recommend using DateFormat.DEFAULT but you expect long date format, so...
精彩评论