Reputation: 73
I want to the milliseconds to the next hours. For example
Now time -> 10:01:23 2nd Oct, 2018, Want remaining milliseconds to 11:00:00 2nd Oct, 2018.
The Now time is dynamic, it can be 23:56:56 2nd Oct, 2018 and next hour is at 00:00:00 3rd Oct, 2018.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(startDate.getMillis());
calendar.add(Calendar.HOUR, 1);
I was trying something like this, but it is adding 1 hour to the startDate. But I want exact next hour.
Any help is welcomed.
Upvotes: 7
Views: 4665
Reputation: 5193
A java.time
solution using ZonedDateTime.until
:
ZonedDateTime startDate = ZonedDateTime.now();
LocalDateTime nextHour = startDate.plusHours(1).truncatedTo(ChronoUnit.HOURS);
long msUntillNextHour = startDate.until(nextHour, ChronoUnit.MILLIS);
Upvotes: 0
Reputation: 5193
A simple arithmetic approach:
long hourInMillis = 60 * 60 * 1000;
long startDateInMillis = startDate.getMillis();
long millisSinceLastHourChange = startDateInMillis % hourInMillis;
long millisToNextHourChange = hourInMillis - millisSinceLastHourChange;
works since Java 1 ;-)
EDIT
This approach doesn't take DST or similar changes into account.
Upvotes: 3
Reputation: 340230
Hours on the clock are not always an hour long.
Leverage a time zone to handle such cases.
Duration
.between(
zonedDateTime_Now ,
zonedDateTime_FirstMomentOfHourLater
)
.toMillis()
If you want to account for real-world anomalies in wall-clock time used by your intended time zone, you must specify a time zone. This means using the ZonedDateTime
class.
An hour is not always an hour. For example, North Korea recently adjusted its offset-from-UTC by half-an-hour. This means one hour will last only 30 minutes during the adjustment jump. This example of North Korea is only one of the most recent examples. Politicians around the world show a surprising penchant for redefining their time zone(s).
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-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" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;
Add an hour.
ZonedDateTime hourLater = now.plusHours( 1 ) ;
Truncate to the hour.
ZonedDateTime firstMomentOfNextHour = hourLate.truncatedTo( ChronoUnit.HOURS ) ;
Calculate elapsed time as a Duration
object.
Duration d = Duration.between( now , firstMomentOfNextHour ) ;
The java.time classes use nanosecond resolution, but you asked for milliseconds. So understand that there is data-loss involved in reporting milliseconds.
long milliseconds = d.toMillis() ;
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.
Upvotes: 0
Reputation: 28289
Since Java8, you can use java.time.LocalDateTime
:
LocalDateTime start = LocalDateTime.now();
// Hour + 1, set Minute and Second to 00
LocalDateTime end = start.plusHours(1).truncatedTo(ChronoUnit.HOURS);
// Get Duration
Duration duration = Duration.between(start, end);
long millis = duration.toMillis();
Running just now (2018-10-02T18:44:48.943070 Peking time) I got 911 056 milliseconds.
Upvotes: 14