• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Date Comparision

 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Thanks in advance.

I would like to do the date comparision in Java without any caluculations like days*hours....

Just want to know whether the two selected days difference is below one year.
Please advice and give me some example code.



Thanks & Regards,
Sreedhar
 
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
So, you have two Date objects, let's say date1 and date2, where date1 is before date2, and you want to know if the time between them is less than one year.

One way to do this with Java's standard date and time API is like this:

  • Get a Calendar object and set it to the value of date2.
  • Use Calendar's methods to subtract one year.
  • Check if the date that the calendar is set to is before date1. If this is true, then the time between date1 and date2 is less than one year.

  •  
    Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic