Java日历获取当前日期,不带小时、分钟、秒和毫秒,以毫秒为单位

2022-01-11 00:00:00 calendar java

我想以毫秒为单位获取当前日期,只有年、月和日期.但是当我使用这段代码时:

I would like to get the current date in milliseconds with only year, month and date. But when I use this code:

Calendar cal = Calendar.getInstance();
cal.clear(Calendar.HOUR);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
currentDate = cal.getTimeInMillis();

我仍然以毫秒为单位获得时间.我该如何解决这个问题?

I still get the time in milliseconds with the hour. How can I fix this?

推荐答案

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
currentDate = cal.getTimeInMillis();

请注意日历的时区.

相关文章