日历无法正确设置小时

时间:2014-05-15 11:20:40

标签: java datetime calendar

如果我想将时间设置为00:00:00,则日历始终为12:00:00

Calendar currentDate = Calendar.getInstance();
// even set AM_PM it was still 12:00:00
// currentDate.set(Calendar.AM_PM, Calendar.AM);
// Calendar.HOUR still not work
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 0);
currentDate.set(Calendar.MILLISECOND, 0); 

我怎么能做到?提前谢谢。

enter image description here

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:2)

但是到了00:00:00,真的,当你到达它的时候?是今天吗?还是昨天?

要消除混淆,请尝试

之类的内容
Calendar currentDate = Calendar.getInstance();
// even set AM_PM it was still 12:00:00
// currentDate.set(Calendar.AM_PM, Calendar.AM);
// Calendar.HOUR still not work
currentDate.set(Calendar.HOUR_OF_DAY, 0);
currentDate.set(Calendar.MINUTE, 0);
currentDate.set(Calendar.SECOND, 1);
currentDate.set(Calendar.MILLISECOND, 0); 

答案 1 :(得分:1)

您可以像这样设置小时:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");

Calendar calendar = new GregorianCalendar(2014, 4, 15, 0, 0, 0);

System.out.println(sdf.format(calendar.getTime()));

<强>输出

2014 mai 15 00:00:00

mai表示法语may个月。