简体   繁体   中英

Call method of class from another package without instantiating the class

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM