Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query about lastModified()

 
kayanaat sidiqui
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
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

 
Jelle Klap
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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic