Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

comparing two different dates in java

 
Yotam Ohayon
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try the java.util.Calendar class?
 
Yotam Ohayon
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
David Byron
Rancher
Posts: 175
Clojure Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
ashirvad jain
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic