java 计算 时区_java – 切换时区进行计算

我们的应用程序以UTC时区存储所有日期.然而,我们的主要业务部门位于“欧洲/柏林”时区(2,1,取决于夏令时).因此,当我们确定某个Timespan应该等于什么“月”时,我们想要使用THAT Timezone.

即2013年10月31日星期四,UTC时间23:00:0和2013年11月29日星期五,格林威治标准时间23:00:00开始的时间段应该全球称为11月13日,对于我们的主要业务部门,时代将会请于2013年11月1日星期五00:00:00至2013年11月30日星期五00:00:00

服务器本身以UTC格式运行,因此我们必须在计算句点名时切换时区.我们在后端使用Joda Datetime,我总是可以这样做:

DateTime now = new DateTime();

now.withZone(DateTimeZone.forID("Europe/Berlin"));

然而,有很多计算正在进行,我想知道是否有更好的方法,在某个时区内进行所有计算?

如果它是一个独立的应用程序,可以使用类似的东西:

DateTimeZone old = DateTimeZone.getDefault();

DateTimeZone.setDefault(DateTimeZone.forID("Europe/Berlin"));

//do all work

DateTimeZone.setDefault(old);

但是,由于它是服务器环境,因此修改默认时区可能会导致其他线程上发生的其他计算出现错误结果.

所以,我想知道,如果没有像java中的c#using指令那么?除了错误的语法,我正在寻找这样的东西:

using(DateTimeZone.forID("Europe/Berlin")){

//do all work, where JUST all DateTime usings inside this using

//Statement are affected

}

编辑:一些小测试显示问题:即使在2个线程上运行,在两个线程中都使用“默认”TimeZone.因此,无论设置什么时区,LAST都将用于两个线程. (当然这就是“默认”时区的用途.)

@Test

public final void testTimeZoneThreaded() {

Thread EuropeThread = new Thread(new Runnable() {

@Override

public void run() {

DateTimeZone.setDefault(DateTimeZone.forID("Europe/Berlin"));

int i = 0;

while (i++ < 10) {

DateTime now = new DateTime();

System.out.println("It is now " + now + " in TimeZone " + DateTimeZone.getDefault().toString());

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

});

Thread UTCThread = new Thread(new Runnable() {

@Override

public void run() {

DateTimeZone.setDefault(DateTimeZone.forID("UTC"));

int i = 0;

while (i++ < 10) {

DateTime now = new DateTime();

System.out.println("It is now " + now + " in TimeZone " + DateTimeZone.getDefault().toString());

try {

Thread.sleep(800);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

});

EuropeThread.start();

UTCThread.start();

while (UTCThread.isAlive()) {

try {

Thread.sleep(50);

} catch (InterruptedException e) {

}

}

}

Beeing能够在不同的线程上使用不同的默认值也会有所帮助.

另一个想法是扩展JodaDateTime并覆盖to string()方法及其所有重载:

class MyDateTime extends DateTime{

@Override

public String toString(){

return super().withZone(DateTimeZone.for("Europe/Berlin")).toString();

}

@Override

public String toString(String format){

return super().withZone(DateTimeZone.for("Europe/Berlin")).toString(format);

}

}

> DateTime是“最终”…… 🙁

所以,目前我总是需要手动处理它,生成期间名称(年,季,月,周……)

public String getMonthPeriodName() {

DateTime firstOfMonth = this.getPeriodStart().withZone(DateTimeZone.forID("Europe/Berlin")).dayOfMonth().withMinimumValue().millisOfDay().withMinimumValue();

DateTime lastOfMonth = this.getPeriodEnd().withZone(DateTimeZone.forID("Europe/Berlin")).dayOfMonth().withMaximumValue().millisOfDay().withMaximumValue();

if (firstOfMonth.isEqual(getPeriodStart()) && lastOfMonth.isEqual(getPeriodEnd())) {

// full month.

return firstOfMonth.withZone(DateTimeZone.forID("Europe/Berlin")).toString("MMM yyyy", Locale.US);

} else {

// just partial month.

String Basename = firstOfMonth.withZone(DateTimeZone.forID("Europe/Berlin")).toString("MMM yyyy", Locale.US);

String f = getPeriodStart().withZone(DateTimeZone.forID("Europe/Berlin")).dayOfMonth().getAsShortText();

String t = getPeriodEnd().withZone(DateTimeZone.forID("Europe/Berlin")).dayOfMonth().getAsShortText();

return Basename + " (" + f + " to " + t + ")";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的时区概念是非常重要的,因为在开发中需要处理不同时区的时间。下面是关于Java时区的一些详解: 1. 世界时区 全球被分为24个时区,每个时区都有一个对应的UTC偏移量,表示该地区与协调世界时(UTC)之间的差异。例如,北京时间是UTC+8,纽约时间是UTC-5。 2. Java时区 Java中的时区使用了ID来标识。它们遵循了IETF的时区标识符,例如“America/New_York”和“Asia/Shanghai”。这些ID是唯一的,并且对应于特定的时区偏移量和时区规则。 3. 时区的转换 使用Java中的DateTimeFormatter类,可以将时间从一个时区转换为另一个时区。例如,将纽约时间转换为北京时间: ``` DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse("2022-06-30 10:30:00", formatter); ZoneId newYorkZone = ZoneId.of("America/New_York"); ZoneId beijingZone = ZoneId.of("Asia/Shanghai"); ZonedDateTime newYorkTime = ZonedDateTime.of(dateTime, newYorkZone); ZonedDateTime beijingTime = newYorkTime.withZoneSameInstant(beijingZone); System.out.println(beijingTime.format(formatter)); ``` 4. 时区的默认值 Java中的时区默认值是系统时区。可以使用System类的getProperty()方法获取系统时区: ``` System.out.println(System.getProperty("user.timezone")); ``` 5. 时区的设置 可以使用System类的setProperty()方法设置时区: ``` System.setProperty("user.timezone", "Asia/Shanghai"); ``` 需要注意的是,时区设置必须在程序启动时进行,否则可能会出现意料之外的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值