getActualMinimum with HOUR as argument of Calendar returns 12 in Java

时光毁灭记忆、已成空白 提交于 2019-11-28 14:51:39

Try using Calendar.HOUR_OF_DAY .

tl;dr

Use java.time classes rather than the troubled legacy classes.

To get the first moment of a day, call LocalDate.atStartOfDay( ZoneId ).

LocalDate.now(                       // Represent the date alone, without time-of-day and without time zone.
    ZoneId.of( "Africa/Tunis" )      // Specify the time zone in which to determine the current date.
)
.withDayOfYear( 1 )                  // Or `.withDayOfMonth(1)` or `.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY) )`.
.atStartOfDay(                       // Determine the first moment of the day on that particular date in that particular zone.
    ZoneId.of( "Africa/Tunis" )
)                                    // Returns a `ZonedDateTime` object.
.toInstant()                         // Same moment, as seen in UTC.
.toEpochMilli()                      // Extract a count of milliseconds since the epoch reference of 1970-01-01T00:00:00Z. 

java.time

You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

Getting the current date and time requires a time zone. For any given moment, the date and time vary around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Extract the date-only portion of that date-with-time.

LocalDate ld = zdt.toLocalDate() ;

From that date, let java.time determine the first moment of the day. Never assume the day starts at 00:00:00. Anomalies such as Daylight Saving Time (DST) mean the day may start at another time such as 01:00:00.

The java.time classes use immutable objects. Rather than alter a property on an object (“mutate”), a fresh new object is created based on values of the original.

ZonedDateTime zdtResult = null ;

For your switch statement, use existing ChronoUnit enum. Because of an odd technical issue, the switch statement cannot use a qualified enum name. So we must use YEARS rather than ChronoUnit.YEARS, for example, as the switch variable. Use a static import to access the enum.

import static java.time.temporal.ChronoUnit ;
…

switch ( unit ) {
    case YEARS : 
        zdtResult = ld.withDayOfYear( 1 )
                      .atStartOfDay( z ) ;
        break;

    case MONTHS : 
        zdtResult = ld.withDayOfMonth( 1 )
                      .atStartOfDay( z ) ;
        break;

    case WEEKS : 
        zdtResult = ld.with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY) )
                      .atStartOfDay( z ) ;
        break;

    default: …
}

From the resulting ZonedDateTime, extract an Instant object to see that same moment in UTC.

Instant instant = zdtResult.toInstant() ;

Your Question’s goal seems to be a count of milliseconds from the epoch reference of first moment of 1970 in UTC. I strongly suggest against using a mere long integer to track date-time values. Makes debugging and tracing troublesome, with bugs likely to escape notice. Pass around and store Instant objects instead.

But if you insist on the count, here's the code. Beware of data loss, as an Instant has a resolution of nanoseconds, so this call ignores any existing microseconds or nanoseconds.

long millisSinceEpoch = instant.toEpochMillis() ;

Notice how using java.time makes for code that is shorter, neater, and much more readable.


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!