Solved

Date/Time question

Posted on 1998-11-19
5
204 Views
Last Modified: 2010-03-30
Hello,
 I have been using the following code to print out the
date & time as part of a trace to a file.
The time being printed in Pacific Standard Time. Even
though the host computer time is Eastern Standard time.
Why is this ?

            //format date & time
            Date current_time = new Date();

            SimpleDateFormat formatter = new SimpleDateFormat("dd'/'MM'/'yyyy HH':'mm':'ss'.'SSS");

            //populate log_string with formatted trace message
            log_string = formatter.format(current_time) + "|" +
                        component_name +       "|" + log_message + "\n";

thanks.
0
Comment
Question by:kkandasa
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1227883
Do

formatter.setTimeZone(new SimpleTimeZone("EST");

to switch to the Eastern Strandard Time. Look at TimeZone class for other examples of use.
0
 

Author Comment

by:kkandasa
ID: 1227884
Hello,

I don't understand.
I did System.out.println( TimeZone.getDefault().getID() );
and my system is using EST. Why is SimpleDateFormat class
does not look at the default timezone ?
How do I set the default timezone and SimpleDateFormat class
looks at ?

0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1227885
Why SimpleDateFormat is not using a default time zone I don't know (you may look in the source code to see what they do). Use the method stated in the answer to change the time zone of the data formatter.
0
 
LVL 5

Accepted Solution

by:
fontaine earned 20 total points
ID: 1227886
In JDK 1.1, the SimpleDateFormat uses the first timezone defined for the locale.
In JDK 1.2, it uses the default timezone.

So, if you use JDK1.1, you have to write:

SimpleDateFormat df = new SimpleDateFormat("dd'/'MM'/'yyyy HH':'mm':'ss'.'SSS" );
df.setTimeZone( TimeZone.getDefault() ); // JDK 1.1

to have SimpleDateFormat using the default timezone. With JDK1.2, the last line is not
needed: the default... is to use the default timezone.

This topic is discussed in the Java Programming FAQ available here:

ftp://rtfm.mit.edu/pub/usenet-by-hierarchy/comp/lang/java/programmer/Java_Programmers_FAQ
0
 

Author Comment

by:kkandasa
ID: 1227887
thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
Opening PDF on button click and fill new document 2 47
MySqlDump not dumping triggers 1 43
Running JavaFX on JDeveloper 12C 1 51
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question