• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compare two dates thru Calendar

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i compare two dates thru Calendar ? one is System date and another is Util date. Date format is MM-dd-yyyy:hh:mm:ss
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean with "one is System date and one is Util date"? There is a Date class in package java.util. Do you mean that you have a java.util.Date object and you want to compare it to the current system date?

You can do that like this:

- Get two Calender objects using the Calendar.getInstance() method.
- Set one of the two to the Date object that you have by using the setTime() method.
- Use the before() or after() methods to compare the Calendars.

Lookup the API documentation of java.util.Calendar for more details.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One common way to compare dates (not neccessarily the best but probably the most often used way) is to convert them into their long values after the epoch using such as:

Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
long time = date.getTime();

Once you have long value in milliseconds you can just compare/compute them as numbers.

As long its not the year 2038, you'll be fine.

-Scott
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic