• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Query about lastModified()  RSS feed

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using Java.IO.File 's lastModified() method.

As the reur type is long its giving value like this--

1248362044000.

What is the meaning of this?

Is there any way to parse it into simple date format like yyyy-MM-dd hh-mm?


Thanks,
Kaya.
 
Sheriff
Posts: 21461
96
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, all long values that represent a date/time are the number of milliseconds since the epoch - January 1st 1970 at 00:00:00.

If you need a date, you can use java.util.Date or java.util.Calendar. The first has a constructor that can take such a long, the second has a method called setTimeInMillis.

With those two you don't have any proper formatting though. That's where java.text.DateFormat (and its most popular subclass java.text.SimpleDateFormat) come in. You construct a DateFormat, then pass a Date object to its format method to retrieve a string.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the API documentation for the method lastModified() in class java.io.File exactly describes what the return value means:

Returns:
A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs


 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you take a look at java.io.File's API documentation for the lastModified method it says:

API documentation wrote:
Returns:
A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs.



So that's the meaning of the long value returned from this method.

Now if you take a look at the API documentation of the java.util.Date class, you'll notice that in the section about its constructors there's only one overloaded variant that hasn't been deprecated:

API documentation wrote:
public Date(long date)Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as "the epoch", namely January 1, 1970, 00:00:00 GMT.

Parameters:
date - the milliseconds since January 1, 1970, 00:00:00 GMT.



Edit: Aw crap, too slow...
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob. Now i am able to get my desired result.

Also Thanks to Jelle Klap and Jesper Young for giving me such a nice definition.


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