Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3438502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:11:44+00:00 2026-05-18T08:11:44+00:00

There are two parts to this: first compute the start date. Then compute the

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:11:44+00:00Added an answer on May 18, 2026 at 8:11 am

    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());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this question is two parts. The first is seeing whether there is a
I came across these two methods to concatenate strings: Common part: char* first= First;
There are two questions to this: How can I best spread the word about
I have a window that displays text. There are two parts to the text:
My task consists of two parts. First I have to create globbal char array
I would like to validate some objects. The validation has two parts: validation whether
The question is in two parts. The first is conceptual. The next looks at
I'm creating a mobile game that that has two parts. The first is slow,
First time posting in Stack Overflow, hope I'm doing this right. I'm writing an
I am coding a small Python module composed of two parts: some functions defining

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.