2014-05-15 32 views
0

当我希望得到一个时间为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

+0

http://stackoverflow.com/questions/17821601/set-time-to-000000 – Sanjeev

+0

@Sanjeev这就是我想要的。谢谢 – blackdog

+0

@Sanjeev,这是行不通的。我添加了一些图片来告诉你, – blackdog

回答

1

但是,当是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); 
+0

这很奇怪,我syserr的日期,这是在控制台00:00:00。但是,我在线上做了一个断点,然后看着日期,现在是12:00:00。后面我会贴一张照片给你。 – blackdog

0

您可以设置时间是这样的:

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个月法语。

+0

控制台输出的结果是00:00:00。但是当我设置断点来观察它的值是12:00:00。你可以在上面的图片中看到它。或者它只是一个日食中的bug? – blackdog

+0

@blackdog Eclipse以不同的方式格式化小时。 – Stephan