在Java中更改日期对象的时区? [英] Change a Date Object's Timezone in Java?

查看:546
本文介绍了在Java中更改日期对象的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

土耳其有两个时区GMT + 2和GMT + 3。
我想将GMT + 2日期更改为GMT + 3,但我希望保护在GMT + 2 TimeZone中的小时和分钟。



我想要需要几分钟时间,然后将这些值设置为GMT + 3 TimeZone日期。结果,在几小时和几分钟内不能有任何变化,但是timeZone只能改变。在函数 toconvert 日期必须是GMT + 2格式,但返回值必须为GMT + 3格式。怎么做清楚?

  public static Date convertTimezone(Date toConvert){
Date date = new Date();
date.setYear(toConvert.getYear());
date.setMonth(toConvert.getMonth());
date.setHours(toConvert.getHours());
date.setMinutes(toConvert.getMinutes());
返回日期;
}


解决方案

$ c> Date 代表一个时间点,没有别的。这意味着 Date 不知道如何打印,哪个时区等...



时区是因此,当打印 Date 时,您设置的内容。类 DateFormat 通常用于打印,时区是一部分您可以在 DateFormat 上设置的属性。通常,人们使用 SimpleDateFormat 子类。


Turkey has two TimeZone GMT+2 and GMT+3. I want to change the GMT+2 dates into GMT+3, but I want to protect hours and minutes that in GMT+2 TimeZone.

I want to take hours and minutes, and then set these values into GMT+3 TimeZone date. At result there must be no change in hours and minutes but the timeZone must be change only. At function toconvert date is must be GMT+2 format, but the return value must be GMT+3 format. How to do it clearly?

public static Date convertTimezone(Date toConvert) {
    Date date = new Date();
    date.setYear(toConvert.getYear());
    date.setMonth(toConvert.getMonth());
    date.setHours(toConvert.getHours());
    date.setMinutes(toConvert.getMinutes());
    return date;
}

解决方案

In Java a Date represents a point in time, nothing else. This means that Date knows nothing about how it is printed, which time zone etc...

Time Zone is therefore something you set when printing the Date. The class DateFormat is typically used for printing and the time zone is part of the properties you can set on DateFormat. Typically, people use the subclass SimpleDateFormat.

这篇关于在Java中更改日期对象的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆