Java calendar get the current date, without hours,

2020-02-08 15:26发布

问题:

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?

回答1:

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

Be carefull on the timezone of your Calendar.



回答2:

If you only want this relative to GMT and explicitly just want milliseconds, you can use this:

long now = System.currentTimeMillis();
long today = now - now % 86400000;


回答3:

from the javadoc: The HOUR_OF_DAY, HOUR and AM_PM fields are handled independently and the the resolution rule for the time of day is applied. Clearing one of the fields doesn't reset the hour of day value of this Calendar. Use set(Calendar.HOUR_OF_DAY, 0) to reset the hour value.

cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

might need to set AM/PM as well



回答4:

tl;dr

LocalDate.now()

LocalDate

If what you want is actually just the date without the time, use LocalDate class.

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );

ZonedDateTime

If you want a date with the time-of-day set to the first moment of the day, ask java.time to determine that moment for you. Do not assume the first moment is at 00:00:00. Anomalies such as Daylight Saving Time means first moment may occur at some other time such as 01:00:00.

Use that time zone object, the ZoneId, to generate a ZonedDateTime object.

ZonedDateTime zdt = today.atStartOfDay( z );  // Determine the first moment of the day on this date for this time zone.

To view this same moment as a value in UTC, extract a Instant object.

Instant instant = zdt.toInstant();

Avoid legacy date-time classes

Avoid the troublesome old date-time classes such as Calendar. These are now legacy, supplanted by the java.time classes.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
    • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.



回答5:

If you want to use Joda time:

   long millis = new DateMidnight().getMillis();


标签: java calendar