基本答案是,你不能。返回的值Date#toString
是Date
对象的表示形式,除了它在方法内部使用的格式之外,它没有任何格式的概念toString
。
一般来说,这不应该用于显示目的(除了极少数情况)
相反,您应该使用某种DateFormat
例如...
Date date = new Date();
System.out.println(date);
System.out.println(DateFormat.getDateTimeInstance().format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(date));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG).format(date));
会输出类似...
Thu Jan 30 16:29:31 EST 2014
30/01/2014 4:29:31 PM
30/01/14 4:29 PM
30/01/2014 4:29:31 PM
30 January 2014 4:29:31 PM
如果你真的卡住了,你可以使用 a 进一步自定义它SimpleDateFormat
,但如果可以的话,我会避免这种情况,因为不是每个人都使用相同的日期/时间格式;)