How is it possible in Java to convert an instance of java.util.TimeZone
to org.joda.DateTimeZone
and keeping the daylight saving time?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Joda-Time in maintenance-mode
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
java.time.ZoneId
The modern replacement for
java.util.TimeZone
isjava.time.ZoneId
&java.time.ZoneOffset
.You should avoid the old legacy date-time classes. But if necessary, you can convert to/from the java.time types. Look to new methods added to the old classes. You can move between
TimeZone
andZoneId
.…and…
If you are looking for the offset-from-UTC or Daylight Saving Time (DST) info for the zone, look at the
ZoneRules
class. Search Stack Overflow for more discussion and examples on that, or edit your Question to describe more about your goal.When you call
DateTimeZone.forTimeZone
, it uses the timezone ID and Joda creates the equivalent object using its own DST rules - it doesn't import the rules from theTimeZone
object.Another detail is that you're using "GMT", which is a zone without DST rules (just check the value for
DateTimeZone.forID("GMT").isFixed()
- it returnstrue
meaning that the zone is fixed, AKA it doesn't have offset changes, AKA it has no DST).It seems that you want a zone with London DST rules (based on the dates you've chosen to DST start and end). If that's the case, you can just create a zone using
DateTimeZone.forID("Europe/London")
.But if you want to create a custom zone with specific DST rules, you have to do it manually by extending
DateTimeZone
class. One example of a zone with DST starting at 27/Mar/2013 and ending at 31/Oct/2013, both at midnight (you can change the values according to your needs though).Testing this zone:
I've created just a simple case with just one DST transition, but you can make the class as complex as you want, storing all the DST changes you need and adjusting the start and end dates (and the offsets as well) accordingly.