3

When I want to get a time set to 00:00:00, Calendar always get 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); 

How could I make it? Thanks in advance.

enter image description here

enter image description here

enter image description here

ngrashia
  • 9,869
  • 5
  • 43
  • 58
blackdog
  • 2,007
  • 3
  • 25
  • 43

2 Answers2

3

But when is 00:00:00, really, when you get down to it? Is it today? Or yesterday?

To eliminate confusion, try something like

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); 
Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
  • It's so strange that I syserr the date, it was 00:00:00 on the console. But I make a breakpoints to the line, then watch the date, it's 12:00:00. I'll paste a picture to you latter. – blackdog May 15 '14 at 11:30
1

You can set hour like this:

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()));

OUTPUT

2014 mai 15 00:00:00

mai means may month in french.

Stephan
  • 41,764
  • 65
  • 238
  • 329
  • The result output in console is 00:00:00. But when I set a breakpoints to watch the value it was 12:00:00. You can see it in the picture above. Or is it just a bug in eclipse? – blackdog May 16 '14 at 04:25