Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Time wrong in java programs

 
Eric Michael
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm running Java on a Win 98 machine. When I run a Java application or view a JSP page, Java thinks that the time is one hour in the past. What could be causing this? How can I fix it?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Present us with a small test program (source code) which gives the wrong time on your system, and we'll take a look. I can at least check it on Me/2000/NT.
I noticed you said JSP. That's running on a server
somewhere - is its time set correctly?
Is it a daylight-saving problem?
Dunno...
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has no idea what time it is. It asks the machine it is running on for the time. So check that first.
 
Eric Michael
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of a stand-alone Java program that prints the time as one hour in the past on my Win 98 system:
import java.util.*;
public class TimeTest {
public static void main(String args[])
{
Date a = new Date();
System.out.println(a);
}
}
The clock on my Win 98 toolbar gives the correct time. I also see this error when I run a JSP page with JRun or Tomcat, and also when I run the test simulator that comes with the Sybex book.
 
Neil Laurance
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is most likely timezone / daylight savings related. Have a look at the following classes for more information on how to influence the timezone for output:
http://java.sun.com/j2se/1.4.1/docs/api/java/util/Calendar.html
http://java.sun.com/j2se/1.4.1/docs/api/java/util/TimeZone.html
http://java.sun.com/j2se/1.4.1/docs/api/java/text/DateFormat.html
http://java.sun.com/j2se/1.4.1/docs/api/java/text/SimpleDateFormat.html
I would suggest modifying the time output format to include the timezone being used.
Cheers, Neil
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic