This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract method TimeZone.getRawOffset  RSS feed

 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
looks getRawOffset() of TimeZone is abstract method. I thought it must be extended by subclass to implement the abstract method to use the function getRawOffset.

getRawOffset
public abstract int getRawOffset()

but it can be done TimeZone.getDefault().getRawOffset() directly.

System.out.println(" Tiemzoen Rawoffset is " + TimeZone.getDefault().getRawOffset() + ".\n");

What is wrong with my thinking?

Thanks,
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to look at a concrete subclass of TimeZone like SimpleTimeZone.
 
Steve Jiang
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, SimpleTimeZone is concrete class. but TimeZone getDefault() returns the instance of timezone, not SimpleTimeZone.

As the code works fine, I know somthing wrong about my thinking, and hope to make it clear.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if this will give you what you are looking for but you can try to retrieve the offset for your TimeZone with the Calendar.ZONE_OFFSET field number. Create a Calendar instance and then try retrieving the ZONE_OFFSET using that field number.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Jiang:
Yes, SimpleTimeZone is concrete class. but TimeZone getDefault() returns the instance of timezone, not SimpleTimeZone.

As the code works fine, I know somthing wrong about my thinking, and hope to make it clear.

You can indeed not instantiate an abstract class or an interface. But you can have a variable of which the type is an abstract class or interface and have it refer to an instance of a conrete class that extends the abstract class or implements the interface, and then you can call any method in the abstract class or interface. For example:

In fact, coding to the interface and not to a specific implementation, like the example above shows, is good practice.

TimeZone.getDefault() indeed returns a TimeZone and not a SimpleTimeZone. That's good - your program doesn't need to know what concrete implementation of TimeZone is provided. If you try this, you'll see that it returns a sun.util.calendar.ZoneInfo (at least, in Java 6.0 - in other versions is might return something else):

Ofcourse, sun.util.calendar.ZoneInfo is a concrete subclass of java.util.TimeZone.
[ November 22, 2006: Message edited by: Jesper Young ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!