Solved

Date/Time question

Posted on 1998-11-19
5
179 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now