在有年、月、日、时、分的情况下,如何根据设备配置的日期和时间正确格式化?


当前回答

简单日期格式

我使用SimpleDateFormat没有自定义模式,以设备的预选格式从系统中获得实际的日期和时间:

public static String getFormattedDate() {
    //SimpleDateFormat called without pattern
    return new SimpleDateFormat().format(Calendar.getInstance().getTime());
}

返回:

13.01.15 11 1/13/15上午10:45 ...

其他回答

使用SimpleDateFormat

是这样的:

event.putExtra("starttime", "12/18/2012");

SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date date = format.parse(bundle.getString("starttime"));

地方

以毫秒格式获取日期或时间,我使用了这个:

日期和时间

Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault());
dateFormat.format(date);

Date

Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
dateFormat.format(date);

Time

Date date = new Date(milliseconds);
DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
dateFormat.format(date);

您可以使用其他日期样式和时间样式。更多关于样式的信息。

Try:

event.putExtra("startTime", "10/05/2012");

当你访问传递的变量时:

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(bundle.getString("startTime"));

简单日期格式

我使用SimpleDateFormat没有自定义模式,以设备的预选格式从系统中获得实际的日期和时间:

public static String getFormattedDate() {
    //SimpleDateFormat called without pattern
    return new SimpleDateFormat().format(Calendar.getInstance().getTime());
}

返回:

13.01.15 11 1/13/15上午10:45 ...

使用Time类中的构建!

Time time = new Time();
time.set(0, 0, 17, 4, 5, 1999);
Log.i("DateTime", time.format("%d.%m.%Y %H:%M:%S"));