Date/Time question

Posted on 1998-11-19
Last Modified: 2010-03-30
 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";

Question by:kkandasa
  • 2
  • 2

Expert Comment

ID: 1227883

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

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

Author Comment

ID: 1227884

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 ?


Expert Comment

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.

Accepted Solution

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:

Author Comment

ID: 1227887

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 39
mockito example issue 8 62
oracle 11g 23 48
java jdbc batch example 8 21
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

910 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

19 Experts available now in Live!

Get 1:1 Help Now