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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:55:09+00:00 2026-05-28T05:55:09+00:00

I am experiencing a problem with the code below. It’s converting milliseconds to months,

  • 0

I am experiencing a problem with the code below. It’s converting milliseconds to months, days, hours, and minutes.

long diffms = date2l - date1l; //The result here is in milliseconds; The value of date2l - date1l are different
long diff_minute = diffms / 60000;

long diff_hour = diff_minute / 60; float diff_minute_now = (diff_minute % 1) * 60; int dmn = (int) diff_minute_now;
long diff_day = diff_hour / 24; float diff_hour_now = (diff_hour % 1) * 24; int dhn = (int) diff_hour_now;
long diff_month = diff_day / 30; float diff_day_now = (diff_day % 1) * 30;  int ddn = (int) diff_day_now;   

diffe = new LabelField
("Remaining Time : " + Long.toString(diff_month) + " month(s) " 
                    + Integer.toString(ddn) + " day(s) " 
                    + Integer.toString(dhn) + " hour(s) "
                    + Integer.toString(dmn) + " minute(s)");
add(diffe);

Why are the result values all zeroes?

EDIT:
@BicycleDude I modify your code into:

long diffms = date2l - date1l;
long ts = diffms / 1000;

long mo = ts / 60 / 60 / 24 / 30;
long d = (ts - mo * 30 * 24 * 60 * 60) / (60 * 60 * 24);
long h = (ts - d * 24 * 60 * 60) / (60 * 60);
long m = (ts - h * 60 * 60) / 60;

But the hours doesn’t work

  • 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-28T05:55:10+00:00Added an answer on May 28, 2026 at 5:55 am

    I’m going to base my answer on the edited part of the code, since BicycleDude has already pointed out what was wrong with the modulo operations.

    long diffms = date2l - date1l; //difference in ms
    long ts = diffms / 1000; //total difference in seconds
    
    long mo = ts / 60 / 60 / 24 / 30; //(1)
    long d = (ts - mo * 30 * 24 * 60 * 60) / (60 * 60 * 24); //follows on because of (1)
    long h = (ts - d * 24 * 60 * 60) / (60 * 60); //follows on because of (1)
    long m = (ts - h * 60 * 60) / 60; //follows on because of (1)
    

    Alright, I thought there would be multiple problems, but I think (think, I’m probably missing something…) that the main error is coming from your calculation of months, which I denoted in the code by the comment (1). You can’t calculate the difference in months like that.

    Why? What about if there was more than one month in differences? Wouldn’t you need to divide by 31, instead? Or if it was a leap year? You’d need to divide by 29 if it was February. Since integer division doesn’t round or account for decimals, you can get inaccuracies in your difference-in-months calculation. It’s probably best if you instead use the differences in hours to calculate the difference in days, and from there you can figure out the difference in months. (Edit: I think you’d also need to take into account the factors I mentioned above when calculating the difference in months from the difference in days, by checking what your “origin” and “target” dates are, though I wouldn’t be too sure of writing it myself at thie stage…)

    Since you have a problem with the way you calculate the difference in months, and you use the erroneous value to calculate the difference in days, I think this results in the error propagating down to some of your other values (eg. the difference in hours).

    EDIT

    Okay, I mucked around with the code a bit more. As I noted earlier, your months calculation was dangerous and affected your days calculation, which potentially introduced errors later on.

    With the example you provided in the comments, your code had a discrepancy of 3 days in the number of days. (The example was 19 January 2012 to 3 May 2012). I ran this against BicycleDude’s code and it was fine.

    I’ll repost the code, and I’ve just added one line to find the number of days, based on how many hours have passed.

    long h = ts / 60 / 60; // hour part
    long m = (ts - h * 60 * 60) / 60; // minute part
    long s = (ts - h * 60 * 60 - m * 60); // second part
    long d = h / 24;
    

    If you like to make it so that you can read that “there are w days, x hours, y minutes and z seconds between date2l and date1l”, you could do something like this:

    long date2l = Timestamp.valueOf("2012-05-03 05:30:10").getTime();
    long date1l = Timestamp.valueOf("2012-01-19 00:00:00").getTime();
    long diffms = date2l - date1l; //difference in ms
    long diff_seconds = diffms / 1000; //total difference in seconds
    long diff_mins = diff_seconds / 60; //total difference in minutes
    long diff_hours = diff_mins / 60; //total difference in hours
    long diff_days = diff_hours / 24; //total difference in days
    
    long x = (diff_seconds - diff_days * 60 * 60 * 24) / (60 * 60);
    long y = ((diff_seconds - (diff_hours * 60 * 60))) / 60;
    long z = ((diff_seconds - (diff_mins * 60)));
    long w = diff_days;
    
    System.out.println(w + " " + x + " " + y + " " + z);
    

    And it appears to work.

    I haven’t figured out the months part because that’s a lot more non-trivial, but yeah. This kinda works?

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

Sidebar

Related Questions

I am experiencing a problem with the following code in some versions of Internet
I am experiencing this problem for few hours now and really not sure how
I'm experiencing a very strange problem... The following trivial test code works as it
I'm experiencing the problem described in this Android issue: http://code.google.com/p/android/issues/detail?id=4536 Simply put, after pressing
I am experiencing a weird problem with the Java ProcessBuilder . The code is
I am experiencing a memory leak with a code similar to the one below
I'm experiencing some problems with breaking my code to reusable parts using templates and
I'm experiencing this problem: I want to add the Google Maps API to my
I have a Windows Server 2003 64-Bit VPS, and I'm experiencing a problem with
I'm new to web-development in ASP, and I'm experiencing a problem where I try

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.