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

  • Home
  • SEARCH
  • 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 157249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:28:14+00:00 2026-05-11T10:28:14+00:00

Ok, I’m officially stumped on this one. I have a GregorianCalendar object that I

  • 0

Ok, I’m officially stumped on this one. I have a GregorianCalendar object that I would like to determine if it is in the past, present, or future. So far, the Calendar#compareTo docs are confusing to me, in that I am getting erratic results. A simple test class will illustrate my problem:

import java.util.Calendar; import java.util.GregorianCalendar; public class MyCal {          public static void main( String[] args )         {                 GregorianCalendar datePast = new GregorianCalendar();                 datePast.add(Calendar.MONTH, -6); // subtract 6 months                 GregorianCalendar datePresent = new GregorianCalendar();                 GregorianCalendar dateFuture = new GregorianCalendar();                 datePast.add(Calendar.MONTH, 6); // add 6 months                  System.out.println('compare datePresent to datePast: '+datePresent.compareTo(datePast));                 System.out.println('compare datePresent to datePresent: '+datePresent.compareTo(datePresent));                 System.out.println('compare datePresent to dateFuture: '+datePresent.compareTo(dateFuture));         } } 

And the output:

compare datePresent to datePast: 1 compare datePresent to datePresent: 0 compare datePresent to dateFuture: 0 

My understanding of compareTo is that the last line should be a -1. Can anyone tell me what I’m doing wrong?

  • 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. 2026-05-11T10:28:15+00:00Added an answer on May 11, 2026 at 10:28 am

    You’ve added 6 months to datePast, not dateFuture. Here’s the working code:

    import java.util.Calendar; import java.util.GregorianCalendar; public class MyCal {      public static void main( String[] args )     {         GregorianCalendar datePast = new GregorianCalendar();         datePast.add(Calendar.MONTH, -6); // subtract 6 months         GregorianCalendar datePresent = new GregorianCalendar();         GregorianCalendar dateFuture = new GregorianCalendar();         dateFuture.add(Calendar.MONTH, 6); // add 6 months          System.out.println('compare datePresent to datePast: '+datePresent.compareTo(datePast));         System.out.println('compare datePresent to datePresent: '+datePresent.compareTo(datePresent));         System.out.println('compare datePresent to dateFuture: '+datePresent.compareTo(dateFuture));     } } 

    Your results would have been inconsistent because sometimes dateFuture would be later than datePresent, depending on when the internal clock ‘ticked’.

    In addition to all of this, I can’t help but give my standard Java date and time advice: use Joda Time instead. It’s not responsible for this particular problem, although the fact that many of the types are immutable would have helped. But in general it’s a much better library.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.