Solved

Date/Time question

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 48
servlet example 17 59
servlet and mdb, jms error 1 57
Html split(text) 2 31
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

733 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