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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:14:08+00:00 2026-05-16T12:14:08+00:00

I wanted to compare the current system date to a fix date, but get

  • 0

I wanted to compare the current system date to a fix date, but get the wrong result. By trying to find out what’s wrong, I ended with the following Java code:

    // today, 18th of August 2010
    long currSystem = System.currentTimeMillis();
    Calendar calToday = Calendar.getInstance();
    Calendar calFix = Calendar.getInstance();
    long milliToday = calToday.getTimeInMillis();
    long milliOlder = calFix.getTimeInMillis();
    long deltaOlder = milliOlder - milliToday;
    long deltaSystem = currSystem - milliToday;

    // 1st of August 2010
    calFix.set( 2010, 8, 1 );
    long milliChanged1 = calFix.getTimeInMillis();
    long deltaChanged1 = milliChanged1 - milliToday;
    boolean isAfter1 = calToday.after( calFix );
    boolean isBefore1 = calToday.before( calFix );

    // 1st of October 2010
    calFix.set( 2010, 10, 1 ); 
    long milliChanged2 = calFix.getTimeInMillis();
    long deltaChanged2 = milliChanged2 - milliToday;
    boolean isAfter2 = calToday.after( calFix );
    boolean isBefore2 = calToday.before( calFix );

The results in the debugger are:

currSystem: 1282160371775
deltaChanged1: 1209600009
deltaChanged2: 6480000009
deltaOlder: 9
deltaSystem: -25
isAfter1: false
isAfter2: false
isBefore1: true
isBefore2: true
milliChanged1: 1283369971809
milliChanged2: 1288640371809
milliOlder: 1282160371809
milliToday: 1282160371800

The small differences for deltaOlder and deltaSystems are due to the execution time, that’s no problem.
But the results of the comparison of today (18th of August) and the first set date (1st of August) is wrong IMO. isAfter1 should be true and isBefore1 should be false;
The comparison with the second set date (1st of October) is ok.

Any suggenstion what I’m doing wrong in the simple thing to compare two dates?

  • 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-16T12:14:08+00:00Added an answer on May 16, 2026 at 12:14 pm

    In a Calendar object, month is 0-based, so August should be 7 and October should be 9.

    Therefore, you just need to change your “sets” to have the correct numbers and it should work fine.

    // 1st of August 2010
    calFix.set( 2010, 7, 1 );
    
    // 1st of October 2010
    calFix.set( 2010, 9, 1 ); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to compare 2 XMLs, which have the same data but the tag
Out of curiosity I wanted to test the number of ticks to compare a
i wanted a small logic to compare contents of two arrays & get the
I want to be able to figure out the month of the current date
I wanted to do some research but i could not find any information about
I wanted to compare the performance characteristics of immutable.Map and mutable.Map in Scala for
I just wanted to compare a double number saved in a double variable in
Lets say i wanted to compare a variable in the sqlite database and if
hi I wanted to have some keys and values filtered out when compared with
So I wanted to compare the performance of python between 2.6 and 3.1, so

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.