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?
SimpleDateFormatis not thread-safe. If multiple threads use the same staticSimpleDateFormatobject concurrently, you will get unexpected results.