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.
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.