Jar*_*ede 0 java coldfusion jodatime
我在Coldfusion中使用Java对象,所以我的代码有点偏.
我有一个看起来像这样的函数:
function getJODAOffset(sTimezone){
local.oDateTimeZone = createObject('java','org.joda.time.DateTimeZone');
local.oInstant = createObject('Java','org.joda.time.Instant');
local.oFormatter = createObject("Java",'org.joda.time.format.DateTimeFormat');
local.oFormatter = local.oFormatter.forPattern('ZZ');
local.tTime = local.oDateTimeZone.forID(arguments.sTimezone).getStandardOffset(local.oInstant); //sTimezone = 'Europe/London';
return local.oFormatter.withZone(local.oDateTimeZone.forID(arguments.sTimezone)).print(local.tTime);
}
Run Code Online (Sandbox Code Playgroud)
当我期待" +00:00 "时,这给了我" +01:00 " 的输出,我不知道为什么.
好的,我想我现在已经有了.
首先,我不知道你的代码是如何工作的,在所有 -有没有getStandardOffset(Instant)方法,只要我能看见-只getStandardOffset(long).我们可以通过调用解决这个问题getMillis(),但我不知道Coldfusion在做什么.
无论如何,我可以在这里重现这个问题:
import org.joda.time.*;
import org.joda.time.format.*;
public class Test {
public static void main(String[] args) {
DateTimeZone zone = DateTimeZone.forID("Europe/London");
Instant now = new Instant();
long offset = zone.getStandardOffset(now.getMillis());
System.out.println("Offset = " + offset);
DateTimeFormatter format = DateTimeFormat.forPattern("ZZ")
.withZone(zone);
System.out.println(format.print(offset));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
Offset = 0
+01:00
Run Code Online (Sandbox Code Playgroud)
问题是,你传递一个偏移到DateTimeFormatter.print,"因为时代米利斯"值的期待-一个瞬间.因此,它将其视为等同于:
format.print(new Instant(0))
Run Code Online (Sandbox Code Playgroud)
现在new Instant(0)代表1970年1月1日午夜时分 - 但欧洲/伦敦时区当时正好在+01:00 ......这就是你所看到的偏移.所以这不是Joda Time中的一个错误 - 这是你如何使用它的一个错误.
一种选择是,而不是创建一个DateTimeZone被固定在你找到了偏移,并格式化任何使用即时区:
DateTimeZone fixedZone = DateTimeZone.forOffsetMillis(offset);
DateTimeFormatter format = DateTimeFormat.forPattern("ZZ")
.withZone(fixedZone);
System.out.println(format.print(0L)); // Value won't affect offset
Run Code Online (Sandbox Code Playgroud)