• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date Comparision  RSS feed

 
sreedhar Lackka
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.

  •  
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!