3

Am new to Java and did not understand following piece of code from here

SimpleDateFormat format = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));

After creating and instance of class SimpleDateFormat, which is from the java.text package, the setTomeZone method of the java.util package is being used.

Can any one please help me understand why we used setTimeZone method with instance of SimpleDateFormat class and NOT with instance of Calendar class?

Note: I went through a couple of articles that tell me how to call a method from another Java class or Java package. However this seemed different to me. I also noticed Calendar is an abstract class but unable to understand here.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Mihir
  • 95
  • 2
  • 11
  • You found the wrong piece of code. :-) At least a poor one. You shouldn’t use `SimpleDateFormat` and `TimeZone`, those classes are long outdated and the former in particular notoriously troubleseome. Next you shouldn’t quote the `Z` in the format pattern string. It’s an offset and should be parsed as such, but your code just sees it as a literal. To make up for the fallacies the code does one thing right, though, setting the time zone to UTC so it matches the offset of `Z`. Instead use [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 16 '18 at 13:17
  • Look at [the second answer under the question you referred to](https://stackoverflow.com/a/36798830/5772882) instead, it’s the modern one. There you’ll find what you need. And you will no longer need an answer to the question you asked here. – Ole V.V. Sep 16 '18 at 13:18
  • Could you please explain what you meant by “without instantiating the class” in the title? You just instantiated the `SimpleDateFormat` before calling its `setTimeZone` method. – Ole V.V. Sep 16 '18 at 13:26

3 Answers3

0

setTimeZone

public void setTimeZone(TimeZone zone)

Sets the time zone for the calendar of this DateFormat object. This method is equivalent to the following call.

getCalendar().setTimeZone(zone)

The TimeZone set by this method is overwritten by a setCalendar call.

The TimeZone set by this method may be overwritten as a result of a call to the parse method.

Parameters: zone - the given new time zone.

https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html

https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Vipul Gulhane
  • 761
  • 11
  • 16
0

A package contain classes and a class contains methods. In java.text we have SimpleDateFormat class. If you go to its public api, you can see that this class has a setTimeZone method (which it inherits from java.text.DateFormat class). So this method does belong to SimpleDateFormat class's API. Therefore it's wrong to say that setTimeZone method belongs to java.util package. The latter may contain some class that has a method with the same name, but those methods are not related.

curlyBraces
  • 1,095
  • 8
  • 12
0

After creating and instance of Class SimpleDateFormat which is from java.text package, setTomeZone method of Java.util package is being used.

In this particular case, the classes are all declared public, so they are all visible even if in a different package. The package does not matter here. SimpleDateFormat.setTimeZone() takes a java.util.TimeZone as parameter is not unexpected at all. Packages are just folders and sometimes there is the need to access something from another folder. That's all.

Can any one please help me understand why we used setTimeZone method with instance of SimpleDateFormat class and NOT with instance of Calendar class?

Because the aim of the code is to parse a date string. Calendar does not provide such capabilities. SimpleDateFormat needs its time zone be set to UTC, so that the date string can be parsed to the same instant in time, regardless of the user's local time zone.

Sweeper
  • 213,210
  • 22
  • 193
  • 313