简体   繁体   中英

Converting time using Timezone with JodaTime / scala

I'm trying to utilise some of the JodaTime methods to improve a basic function I was working on.

The premise is that I need to convert times from one to another based on the timezone given.

I have a list of times as a son:

  "name": "A",
  "timezone": "UTC",
  "jodaDate": "2016-01-12T14:33:37.533"
},
{
  "name": "C",
  "timezone": "UTC",
  "jodaDate": "2016-01-21T10:33:37.533"
}

I'm then running these through a function that takes the object that represents each of the above and a 'to' for converting it to another timezone.

Currently I'm only focusing on UTC to ECT

Coordinated Universal Time is 5 hours ahead of Ecuador Time
12:33 Sunday, Coordinated Universal Time (UTC) is
07:33 Sunday, Ecuador Time (ECT)

I pass in "ECT" as the argument and then convert it. The problem is that somehow, someway the ECT is turning into a 'CET' instead. I'm baffled. I'm sure it's something simple but I just cannot locate the error.

The method (including the wonderful printlns...)

val currentTimezone = DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezoneDetails.timezone))
println("Current Timezone from object: " + currentTimezone)

println("arg for converting to: "+to)
val s = DateTimeZone.forTimeZone(TimeZone.getTimeZone(to))
println("arg when converted to timezone: " + s)

val y = timezoneDetails.jodaDate.withZone(s)
println("Was: " + timezoneDetails.jodaDate)
println("Now: " +y)
y

which will yield:

    Current Timezone from object: UTC
arg for converting to: ECT
arg when converted to timezone: CET
Was: 2016-01-12T14:33:37.533Z
Now: 2016-01-12T15:33:37.533+01:00
Current Timezone from object: UTC
arg for converting to: ECT
arg when converted to timezone: CET
Was: 2016-01-21T10:33:37.533Z
Now: 2016-01-21T11:33:37.533+01:00
Current Timezone from object: UTC
arg for converting to: ECT
arg when converted to timezone: CET
Was: 2015-12-27T10:33:37.533Z
Now: 2015-12-27T11:33:37.533+01:00
Current Timezone from object: UTC
arg for converting to: ECT
arg when converted to timezone: CET
Was: 2015-11-23T14:33:37.533Z
Now: 2015-11-23T15:33:37.533+01:00

I imagine the problem to be with val s = DateTimeZone.forTimeZone(TimeZone.getTimeZone(to)) but I don't understand why

any ideas? Thanks in advance!

The problem is with TimeZone.getTimeZone(name) :

public static void main(String[] args) {
    TimeZone tz = TimeZone.getTimeZone("ECT");
    System.out.println("Zone: " + tz.getDisplayName(Locale.US));
    }

will print out Zone: Central European Time

The documentation says:

Three-letter time zone IDs

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be US "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

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