Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Date/Time question

Posted on 1998-11-19
5
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 80 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

Technology Partners: 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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

721 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