Friday, April 25, 2008

Oracle 10g timezones and setting the Java timezone

To see your session and database timezones:
SELECT sessiontimezone, dbtimezone FROM dual;
To set them:
ALTER database SET TIME_ZONE = '-07:00';
ALTER session SET TIME_ZONE = '-07:00';
Alternatively you can set it by supplying a region which exists in the following list:
SELECT tzname, tzabbrev FROM V$TIMEZONE_NAMES;
To set the JVM timezone append the following option when calling java:
-Duser.timezone=Asia/Japan
Java stores the supported timezones in files located at:
c:\path\to\jdk1.5.0_12\jre\lib\zi\
To set the timezone within your code, which is sometimes neccessary because OC4J seems to ignore my -Duser.timezone option:
java.util.TimeZone tz = java.util.TimeZone.getTimeZone("America/Vancouver");
java.util.TimeZone.setDefault(tz);

Here is a JSP page to display some time and timezone related information:

<%@ page language=\"java\" contentType=\"text/html; charset=ISO-8859-1\"
    pageEncoding=\"ISO-8859-1\"%>
<%@ page import=\"
  java.util.*,
  java.text.DateFormat\" %>
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">
<title>TimeZone Test</title>
</head>
<body>
Your current timezone is: <%= java.util.TimeZone.getDefault().toString() %>
<p>
The current time is: <%= (new java.util.Date()).toString() %>
<p>
The value of the user.timezone property is: <%= System.getProperty(\"user.timezone\") %>
<p>
The date using DateFormat is: <%= DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(new Date()) %>
</body>
</html>

No comments: