Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File.lastModified() problem

Status
Not open for further replies.

idarke

Programmer
Jul 23, 2001
420
US
I have a file on my local drive (Windows 2000) with a last modified date/time of Jan 30, 2005 12:09:34 AM.

When I create a File object for that file today, call lastModified() and make a Date object from it, the result is Jan 29, 2005 23:09:34 PM - one hour earlier.

It seems to be making some sort of unnecessary adjustment for daylight savings time. I tried creating a TimeZone object for "US/Eastern" and using a Calendar object set to that time zone to create the date, but it didn't help.

I tried it on a file created today (we are on daylight time now), and it gets the correct date/time with no problem.

Does anyone have any experience with this bug? How can I get the actual date/time from the File object?
 
maybe Calendar.setRawOffset?
abstract void setRawOffset(int offsetMillis)
Sets the base time zone offset to GMT.

_________________
Bob Rashkin
rrashkin@csc.com
 
Thanks for the suggestion. I didn't find setRawOffset on the Calendar object, but I did find one on the TimeZone object. That led me to try creating a SimpleTimeZone with no daylight savings time settings, but that had no effect.

Here's what I tried:

Code:
long tm = f.lastModified();
TimeZone tz = TimeZone.getTimeZone("US/Eastern");
SimpleTimeZone stz = new SimpleTimeZone(tz.getRawOffset(), "US/Eastern");    
GregorianCalendar cal = new GregorianCalendar(stz);
cal.setTimeInMillis(tm);
jav.util.Date timeStamp = cal.getTime();

I'm beginning to think this is a bug in the interface between the JRE and the windows file system.
 
I figured out a workaround for the problem. After getting the Date object as above, I check to see if that date is within daylight savings time, and if it isn't, I add an hour to it.

Here's the full handling:

Code:
long tm = f.lastModified();
TimeZone tz = TimeZone.getTimeZone("US/Eastern");
GregorianCalendar cal = new GregorianCalendar(tz);
cal.setTimeInMillis(tm);
java.util.Date timeStamp = cal.getTime();
if (!tz.inDaylightTime(timeStamp))
{
   cal.add(Calendar.HOUR,1);
   timeStamp = cal.getTime();         
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top