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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:37:41+00:00 2026-05-15T21:37:41+00:00

I have a JodaTime Period that I’ve created from two DateTime instants. Is there

  • 0

I have a JodaTime Period that I’ve created from two DateTime instants. Is there a good way to convert that Period into a decimal number of hours?

For instance, I have a Period that goes from 1pm to 1:30pm on Jan 1, 2010. How can I get that Period as 1.5 hours.

In the past I’ve manually converted using seconds and BigDecimals such as this:

int seconds = myPeriod.toStandardSeconds().getSeconds();
BigDecimal d = new BigDecimal((double) seconds / 3600);
// Round to two decimals
BigDecimal correctResult = d.setScale(2, RoundingMode.HALF_DOWN);

This kind of feels like a hack, not to mention awkward when I start adding Periods together. It seems like there should be a better way.

Any ideas?
Thanks

  • 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-15T21:37:41+00:00Added an answer on May 15, 2026 at 9:37 pm

    If you’ve got two DateTimes, I’d expect you to have a Duration between them rather than a Period… but other than that and Mike’s comments, that looks correct:

    private static final BigDecimal SECONDS_PER_HOUR = 
        BigDecimal.valueOf(DateTimeConstants.SECONDS_PER_HOUR);
    ...
    DateTime dt1 = ...;
    DateTime dt2 = ...;
    Duration duration = new Duration(dt1, dt2);
    BigDecimal result = BigDecimal.valueOf(duration.toStandardSeconds().getSeconds())
                                  .divide(SECONDS_PER_HOUR)
                                  .setScale(2, RoundingMode.HALF_DOWN);
    

    Note that you can avoid the toStandardSeconds().getSeconds() using Duration.getMillis() instead:

    private static final BigDecimal MILLIS_PER_HOUR = 
        BigDecimal.valueOf(DateTimeConstants.MILLIS_PER_HOUR);
    ...
    DateTime dt1 = ...;
    DateTime dt2 = ...;
    Duration duration = new Duration(dt1, dt2);
    BigDecimal result = BigDecimal.valueOf(duration.getMillis())
                                  .divide(MILLIS_PER_HOUR)
                                  .setScale(2, RoundingMode.HALF_DOWN);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JodaTime 1.6.2. I have a LocalDate that I need to convert
I have two strings from an android application in ISO 8601 format that I
I have a DateTime object that I need to see if it's 10 years
I have the following date: 2011-05-24T11:40:41Z How can I convert this into a Joda
I've created a method that takes two dates (in milliseconds) and returns a sentence
I have a model class with a persisted DateTime field that is only interacted
Is there a good, strict date parser for Java? I have access to Joda-Time
I have a class DurationFormatter as below: import java.util.Date; import org.joda.time.DateTime; import org.joda.time.Period; import
I have below Serializer for JodaTime handling: public class JodaDateTimeJsonSerializer extends JsonSerializer<DateTime> { private
I have tried this using Jodatime DateTime , DateTime dateTime = DateTime .parse(1-JAN-1900, DateTimeFormat.forPattern(dd-MMM-yyyy))

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.