开发者

How to calculate elasped time in Java from a provided hour ONLY

开发者 https://www.devze.com 2023-01-27 17:36 出处:网络
There are two parts to this: first compute the start date. Then compute the elapsed time. For the second part, I found some good advice here: Calculate elapsed time in Java / Groovy

There are two parts to this: first compute the start date. Then compute the elapsed time. For the second part, I found some good advice here: Calculate elapsed time in Java / Groovy

However, the first part is a problem for me. Here's how it works: The user indicates that the time span extends from an hour value (00 to 23). I do not have a starting date/time object of any sort - just an integer hour. From that I need to figure the start date (and then the elapsed time).

If the start hour is greater than the now hour, it was the prior day. In order to get an actual start date from that however, I need to potentially consider month and year boundaries as well as things like leap years and daylight savings time changes. Surely someone has solved a problem like this already. (I believe it can be quite complex.) Is there a proven solution that will let me compute how开发者_开发百科 much time (in seconds) has actually elapsed from the given hour of the day (00 to 24) to now? (The start time will always be assumed to be on the hour.)


Firstly, I'd suggest using the Joda Time API. It's the best date/time API available for Java, in my opinion.

Next you need to work out exactly what to do in various corner cases. In particular, suppose the user enters "1" and you're near a daylight saving transition. It's possible that 1am happened twice (if the the time went 1:58, 1:59, 1:00, 1:01 because of a transition back away from DST) or that it didn't happen at all (if the time went 12:58, 12:59, 2:00 because of a transition forward into DST). You need to work out what to do in each of those situations - and bear in mind that this means knowing the time zone too.

Once you've worked that out, it may not be too hard. With Joda Time you can use withHourOfDay method to get from one time to another having set one component of the time - and likewise there are simple APIs for adding or subtracting a day, if you need to. You can then work out the time between two DateTime values very easily - again, Joda Time provides everything you need.

Here's an example which doesn't try to do anything with DST transitions, but it's a good starting point:

import org.joda.time.*;

public class Test
{
    public static void main(String[] args)
    {
        // Defaults to current time and time zone
        DateTime now = new DateTime();
        int hour = Integer.parseInt(args[0]);

        DateTime then = now
            .withHourOfDay(hour)
            .withMinuteOfHour(0)
            .withSecondOfMinute(0);
        if (then.isAfter(now))
        {
            then = then.minusDays(1);
        }
        Period period = new Period(then, now, PeriodType.seconds());

        System.out.println("Difference in seconds: " + period.getSeconds());
    }
}


java.time

The accepted answer has provided a solution using Joda-Time API which was probably the best 3rd party date-time API in Java at that time. In Mar 2014, the modern date-time API was released as part of the Java 8 standard library. Notice the following message on the Home Page of Joda-Time:

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Solution using java.time, the modern date-time API:

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

class Main {
    public static void main(String[] args) {
        // A sample hour indicated by the user
        int hour = 16;

        // Replace the ZoneId with the applicable one
        ZoneId zone = ZoneId.of("America/New_York");
        ZonedDateTime then = LocalDate.now(zone)
                                      .atStartOfDay(zone)
                                      .withHour(hour);

        ZonedDateTime now = ZonedDateTime.now(zone);
        System.out.println(now);

        if (then.isAfter(now))
            then = then.minusDays(1).withHour(hour);

        long seconds = ChronoUnit.SECONDS.between(then, now);
        System.out.println(seconds);
    }
}

Output from a sample run:

2023-01-08T09:31:28.040819-05:00[America/New_York]
63088

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


Let's say startHour is given by the user (assume, that's in the 0-23 range). Now you may start with this:

import java.util.Calendar;
import static java.util.Calendar.*;
...
Calendar start = Calendar.getInstance();
if (start.get(HOUR_OF_DAY) < startHour) {
start.add(DAY_OF_MONTH, -1);


Use Joda-Time classes. You can do something like this:

DateTime now = new DateTime();
DateTime start;
int startHour = getStartHour();// user's choice
int nowHour = now.getHourOfDay();
if (nowHour < startHour) {
  start = now.minusHours(24 - startHour + nowHour);
} else {
  start = now.minusHours(nowHour - startHour);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消