• 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

comparing two different dates in java

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i have two date, one of type long (retrieved from file.lastModified()) and the other date is a String object.
i was trying to figure out exactly how to parse them to the same form and compre them. i need to know which is after the other.
i was looking at SimpleDateFormat, DateFormat and Date and yet i still didn't manage to do it.
any suggestions?

thanks,
Yotam
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try the java.util.Calendar class?
 
Yotam Ohayon
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i took a brief look at the API and it seems like it would do the job, only i now know we are instructed to use SimpleDateFormat
 
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
DateFormat and SimpleDateFormat are for formatting java.util.Date objects into strings, or parsing strings back to Date objects. Those classes don't contain methods to compare dates.
 
Yotam Ohayon
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you use a file.lastModified() method it returns a long value counting the time from 1970 or someting like that. is there any method you know
that can parse this long value into a date? i was looking for one but couldn't find it
 
Rancher
Posts: 175
Clojure Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yotam Ohayon wrote:when you use a file.lastModified() method it returns a long value counting the time from 1970 or someting like that. is there any method you know
that can parse this long value into a date? i was looking for one but couldn't find it


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

As david suggested,

# long millis = file.lastModified();
# Date touchedOn = new Date(millis);

Further more , you can use DateFormat, like below,

DateFormat format=DateFormat.getDateInstance(STYLE); // STYLE="HH:MM:SS " based on your requirement.
String fileDate=format.format(touchedOn );
fileDate.equals(DateToCompareWith);

I hope this will help you.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic