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 8669109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:23:22+00:00 2026-06-12T18:23:22+00:00

I have the following code below, I’m trying to compare two dates using Joda

  • 0

I have the following code below, I’m trying to compare two dates using Joda, so I can get the difference in hours. This works fine for two dates on the same day (see test 1 below). However, if I’m comparing two dates that are not on the same day it just gives the wrong result (see test 2 below)

DateTime end = new DateTime(event.endTime)
DateTime start = new DateTime(event.startTime)

Period p = new Period(start, end);

long hours = p.getHours();
long minutes = p.getMinutes();

System.out.println("Task title :"+ event.getTitle()+ " ...The differnce in hours is : "+hours+"  The differnce in minutes is : "+minutes)

Printed Results from two tests.

The first being a task that’s on the same day, with 3 hours difference.

The second being a task that ends the day after … which doesn’t give the difference properly like the previous task test

This is the END TIME :2012-10-12 14:15:00.0... This is the START TIME :2012-10-12 10:30:00.0
Task title :Test 1 ...The differnce in hours is : 3  The differnce in minutes is : 45

This is the END TIME :2012-10-12 22:00:00.0... This is the START TIME :2012-10-11 22:00:00.0
Task title :Test 999 ...The differnce in hours is : 0  The differnce in minutes is : 0

Any assistance is appreciated

  • 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-06-12T18:23:23+00:00Added an answer on June 12, 2026 at 6:23 pm

    Never used Jodatime before, and haven’t tested this, but I did find a couple things:

    First, a Jodatime Period has a method toStandardHours(), which is probably what you want, instead of .getHours(). Whereas .getHours() returns 0 for your 2nd test, because .getDays() would return 1, .toStandardHours() should return 24.
    http://joda-time.sourceforge.net/apidocs/org/joda/time/Period.html

    Also, you could use the Hours function “hoursBetween”:

     static Hours   hoursBetween(ReadableInstant start, ReadableInstant end)
    

    Check the API:
    http://joda-time.sourceforge.net/apidocs/org/joda/time/Hours.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code below and I'm trying to get an AVG for
Given the following code below, how can I have it modified such that it
See i have this following code below, at line no.9 i'm calculating some dates
hi i have the following code below, where i try to get all the
I have the following code below that I'm trying to set a background color.
I am using PHP 5.3.6 I have the following code below. Everything works fine
I have the following code example below. Whereby you can enter a command to
I have the following code below however am confused as to why the div
I have the following code below for list into master page <div id=header> <ul>
I have the following code below which works, but I want to insert values

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.