Java 将DateTime转换为时区、设置时间并转换回
我有一个小实用方法,可以将Java 将DateTime转换为时区、设置时间并转换回,java,jodatime,Java,Jodatime,我有一个小实用方法,可以将日期时间转换为特定的日期时区,设置该时区的时间,然后再次转换为另一个时区: /** * Delivers a DateTime with changed time in the specified DateTimeZone. * * @param dateTime the date time to convert * @param hour the hour to set in the client timezone date t
日期时间
转换为特定的日期时区
,设置该时区的时间,然后再次转换为另一个时区:
/**
* Delivers a DateTime with changed time in the specified DateTimeZone.
*
* @param dateTime the date time to convert
* @param hour the hour to set in the client timezone date time
* @param minute the minute to set in the client timezone date time
* @param second the second to set in the client timezone date time
* @param dtzConversion the client time zone
* @param dtzReturn the time zone of the return date time (optionally: can be null)
* @return the date time
*/
public DateTime convertDateTimeToTimeZone(final DateTime dateTime, final int hour, final int minute, final int second,
final DateTimeZone dtzConversion, final DateTimeZone dtzReturn)
{
// convert to given timezone
DateTime dtClientTimezone = dateTime.withZoneRetainFields(dtzConversion);
// adjust time
dtClientTimezone = dtClientTimezone.withTime(hour, minute, second, 0);
if (dtzReturn != null) {
// convert to target timezone
dtClientTimezone = dtClientTimezone.withZoneRetainFields(dtzReturn);
}
return dtClientTimezone;
}
在我的示例中,dateTime
是德国日期2015年9月30日22:00:00 UTC
和dtzConversion
是Europe/Berlin
和dtzReturn
是UTC
设置时间12:00
结果是2015年9月30日12:00:00。但我希望2015年10月1日10:00:00,因为2015年9月30日22:00:00 UTC
到Europe/Berlin
应该是2015年10月1日00:00
。时间设置为“12:00:00”,结果是01.10.2015 12:00:00
。这在UTC
中是01.10.2015 10:00:00
。我的错在哪里?该方法不转换字段值。相反,它只是更改时区(以及基础毫秒数,以便新时区中的字段值与旧时区中的字段值相同)
您正在搜索的方法是,用于调整字段:
public static DateTime convertDateTimeToTimeZone(final DateTime dateTime, final int hour, final int minute,
final int second,
final DateTimeZone dtzConversion, final DateTimeZone dtzReturn)
{
// convert to given timezone
DateTime dtClientTimezone = dateTime.withZone(dtzConversion);
// adjust time
dtClientTimezone = dtClientTimezone.withTime(hour, minute, second, 0);
if (dtzReturn != null) {
// convert to target timezone
dtClientTimezone = dtClientTimezone.withZone(dtzReturn);
}
return dtClientTimezone;
}
public static void main(String[] args)
{
DateTime parse = DateTime.parse("2015-09-30T22:00:00Z");
DateTime convertDateTimeToTimeZone = convertDateTimeToTimeZone(parse, 12, 0, 0, DateTimeZone.forOffsetHours(2), DateTimeZone.UTC);
System.out.println(convertDateTimeToTimeZone);
}
结果:
2015-10-01T10:00:00.000Z
似乎是对的。但是为什么第一次时区转换的时间毫秒是一样的呢
DateTime dtClientTimezone=DateTime.withZone(dtzConversion)带有dateTime
millis的code>是144365040000(30.09.2015 22:00:00),而dtzConversion
是“欧洲/柏林”,现在dtClientTimezone
millis也是144365040000。为什么?毫秒数应大于2小时。不是吗?也许datetime
的时区被设置为德国时区,那么就不会进行调整了。用一个例子更新了代码。你是说像当地时区吗?在我的例子中,它发生在服务器上,所以我只想将这些毫秒转换为特定的时区。我对joda time有点困惑。是的,如果你使用DateTime.now(),一个没有DateTimeZone
参数的构造函数,或者解析一个没有时区/偏移量的字符串,DateTime对象是用系统的时区创建的。没有白板很难解释;-)我认为您缺少了Instant
、LocalDateTime
和DateTime
之间的链接。阅读这也适用于joda时间(其中DateTime类似于ZonedDateTime)。