2

This question is rather complicated and I don't know if it's been asked before because I don't know how to phrase the problem in the search box.

Here's the code:

public class SomeClass 
{
  private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

  public static String toUTCDateString(Date date)
  {
    df.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
    return df.format(date);
  }

  /* more static methods */
}

The static member df will be re-used again in more static methods, but I need to have its time zone set to "UTC" first. Is there a way to call .setTimeZone("UTC") just once and for all? Or do I have to call .setTimeZone("UTC") in each static method?

1
  • 2
    Note that SimpleDateFormat is not thread-safe. If multiple threads use the same static SimpleDateFormat object concurrently, you will get unexpected results. Commented Jun 7, 2014 at 8:37

2 Answers 2

7

Use Static Initialization Blocks

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. Here is an example:

static {
    // whatever code is needed for initialization goes here
}

A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

Sample code:

public class SomeClass 
{
      private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

      //Static Initialization Blocks
      static{
           df.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
      }

      public static String toUTCDateString(Date date)
      {     
          return df.format(date);
      }

     /* more static methods */
}
Sign up to request clarification or add additional context in comments.

5 Comments

Wow, it's that simple? I never knew of static initialization blocks in java. >_<;;
Yes, I read up on the link. Alternatively, I can also use private static initialize-methods to initialize my static member. Thanks! I'll accept the answer when 5 minutes is up!
By using the code above you have to synchronize the access to the SimpleDateFormat as it is not threadsafe and will likely lead to unexpected results if called simultaneously
As an alternative, use the Joda-Time library for thread-safety.
OP is not talking about Thread safe at all. Might be OP is not using the code in a multi-threaded environment.
3

Unfortunately! SimpleDateFormat is not trread-safe: it keeps an internal state, and used at the same time havoc arises.

This "solves" your problem as you have to change the API.

public static DateFormat df()
{
    DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    df.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
    return df;
}

In Java 8 using other, nicer classes this issue is solved.

By the way yyyy-MM-dd is the ISO standard.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.