aspose file tools*
The moose likes Beginning Java and the fly likes Query about lastModified() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query about lastModified()" Watch "Query about lastModified()" New topic
Author

Query about lastModified()

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
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

Joined: Oct 27, 2005
Posts: 19718
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1770
    
    7

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


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
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.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about lastModified()