Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

comparing two different dates in java

 
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
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
 
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
 
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.

 
You can't expect to wield supreme executive power just because
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!