Re: Wrong result from System.currentTimeMillis()



On 7 Gen, 20:53, da...@xxxxxxxxx (Mark Rafn) wrote:
<alberto....@xxxxxxxxx> wrote:
As I said both system clock and TZ appear to be correct .

That's what this code fragment produces:

TimeZone tz1 = TimeZone.getDefault();
System.out.println(tz1.getDisplayName());
System.out.println(System.getProperty( "user.timezone" ));
System.out.println(new Date());

Central European Time
Europe/Rome
Fri Jan 04 09:22:36 CET 2008

While the OS date command produces:

Fri Jan 4 08:22:48 Europe/Rome 2008

Let's run a more complete test - I want to see TZ offsets and actual
epoch values (which are timezone-agnostic). I suspect something's
wrong with your timezone definition either in the VM or in the OS.

For the OS (assuming GNU date):

date +%s # seconds since epoch
date +%c # formatted local date
date "+%Z (%z)" # timezone info

for the VM:
import java.util.*;
import java.text.*;
import java.io.*;

public class PrintTime {
public static void main(String[] args) {
PrintStream out = System.out;
long now = System.currentTimeMillis();
Date nowD = new Date(now);
TimeZone tz = TimeZone.getDefault();

out.println("TZ id: " + tz.getID());
out.println("TZ name: " + tz.getDisplayName());
out.println("TZ short no-DST name: " + tz.getDisplayName(false,
TimeZone.SHORT));
out.println("TZ short DST name: " + tz.getDisplayName(true,
TimeZone.SHORT));
out.println("TZ offset (mins): " + tz.getOffset(now) / (60 * 1000));
out.println("TZ uses DST: " + tz.useDaylightTime());
out.println("TZ in DST now: " + tz.inDaylightTime(nowD));
out.println("TZ DST savings (mins): " +
tz.getDSTSavings() / (60 * 1000));
out.println();

out.println("seconds since epoch: " + now / 1000);
out.println("Date toString: " + nowD);
out.println("SDF: " + new SimpleDateFormat("d MMM yyyy HH:mm:ss z(Z)").
format(nowD));
}}

--
Mark Rafn da...@xxxxxxxxx <http://www.dagon.net/>

Here are the results:

# date +%s
1199782449

# date +%c
Tue 08 Jan 2008 08:54:14 AM Europe/Rome

# date "+%Z (%z)"
Europe/Rome (+0000)

# java PrintTime
TZ id: Europe/Rome
TZ name: Central European Time
TZ short no-DST name: CET
TZ short DST name: CEST
TZ offset (mins): 60
TZ uses DST: true
TZ in DST now: false
TZ DST savings (mins): 60

seconds since epoch: 1199782558
Date toString: Tue Jan 08 09:55:58 CET 2008
SDF: 8 Jan 2008 09:55:58 CET(+0100)



.



Relevant Pages

  • Re: Wrong result from System.currentTimeMillis()
    ... Fri Jan 04 09:22:36 CET 2008 ... Tue 08 Jan 2008 08:54:14 AM Europe/Rome ... TZ short DST name: CEST ... So the Java timezone is correct. ...
    (comp.lang.java.programmer)
  • Re: Wrong result from System.currentTimeMillis()
    ... Fri Jan 04 09:22:36 CET 2008 ... epoch values. ... Tue 08 Jan 2008 08:54:14 AM Europe/Rome ... TZ short DST name: CEST ...
    (comp.lang.java.programmer)
  • Summary: Problems with daylight savings change
    ... I then tried compiling both the new timezone code and data, ... The HP DST file ... Now use zdump to show the DST transitions for this time zone in 2006-2007: ... first pair shows the spring transition to DST on the first Sunday in April, ...
    (Tru64-UNIX-Managers)
  • Re: Linux
    ... timezone and hemisphere etc) ... Windows "Time Zone Editor" than with regedit. ... of minutes to apply at DST ... and running zic on them to format them ...
    (rec.crafts.metalworking)
  • Re: Clock did not adjust for daylight saving
    ... Here, my hardware clock is set to UTC, and my local timezone is 5 hours ... much less when DST may or may not change things. ... Nation boundries, within the Navaho Nation boundries, within Arizona) ...
    (alt.os.linux.suse)