DateFormat helps you to format and parse dates for any locale. Your code can be completely independent of the locale conventions for months, days of the week, or even the calendar format: lunar vs. solar.
To format a date for the current Locale, use one of the static factory methods:
代码如下 | 复制代码 |
myString = DateFormat.getDateInstance().format(myDate); |
If you are formatting multiple dates, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.
代码如下 | 复制代码 |
DateFormat df = DateFormat.getDateInstance(); for (int i = 0; i < a.length; ++i) { output.println(df.format(myDate[i]) + "; "); } |
To format a number for a different locale, specify it in the call to getDateInstance:
代码如下 | 复制代码 |
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE); |
DateFormat can also be used to parse strings:
代码如下 | 复制代码 |
myDate = df.parse(myString); |
例子
代码如下 | 复制代码 |
public static CharSequence formatTimeInListForOverSeaUser( final Context context, final long time, final boolean simple, Locale locale) { final GregorianCalendar now = new GregorianCalendar(); // special time if (time < MILLSECONDS_OF_HOUR) { return ""; } // today final GregorianCalendar today = new GregorianCalendar( now.get(GregorianCalendar.YEAR), now.get(GregorianCalendar.MONTH), now.get(GregorianCalendar.DAY_OF_MONTH)); final long in24h = time - today.getTimeInMillis(); if (in24h > 0 && in24h <= MILLSECONDS_OF_DAY) { java.text.DateFormat df = java.text.DateFormat.getTimeInstance( java.text.DateFormat.SHORT, locale); return "" + df.format(time); } // yesterday final long in48h = time - today.getTimeInMillis() + MILLSECONDS_OF_DAY; if (in48h > 0 && in48h <= MILLSECONDS_OF_DAY) { return simple ? context.getString(R.string.fmt_pre_yesterday) : context.getString(R.string.fmt_pre_yesterday) + " " + java.text.DateFormat.getTimeInstance( java.text.DateFormat.SHORT, locale).format( time); } final GregorianCalendar target = new GregorianCalendar(); target.setTimeInMillis(time); // same week if (now.get(GregorianCalendar.YEAR) == target .get(GregorianCalendar.YEAR) && now.get(GregorianCalendar.WEEK_OF_YEAR) == target .get(GregorianCalendar.WEEK_OF_YEAR)) { java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("E", locale); final String dow = "" + sdf.format(time); return simple ? dow : dow + java.text.DateFormat.getTimeInstance( java.text.DateFormat.SHORT, locale).format(time); } // same year if (now.get(GregorianCalendar.YEAR) == target .get(GregorianCalendar.YEAR)) { return simple ? java.text.DateFormat.getDateInstance( java.text.DateFormat.SHORT, locale).format(time) : java.text.DateFormat.getDateTimeInstance( java.text.DateFormat.SHORT, java.text.DateFormat.SHORT, locale).format(time); } return simple ? java.text.DateFormat.getDateInstance( java.text.DateFormat.SHORT, locale).format(time) : java.text.DateFormat.getDateTimeInstance( java.text.DateFormat.SHORT, java.text.DateFormat.SHORT, locale).format(time); } |
时间: 2024-09-20 17:32:15