Java 如何将当前时间更改为用户时间

Java 如何将当前时间更改为用户时间,java,swing,user-interface,Java,Swing,User Interface,我正在一个时钟上创建一个JavaGUI,它要求用户从当前时间更改时间。我有系统时间和用户时间。代码应该是什么,以便显示用户时间。我正在使用的愚蠢代码: Date curDate = new Date(); //changeTime i have got from the user double time=0; if(curDate.getTime()- changeTime.getTime() > 0){

我正在一个时钟上创建一个JavaGUI,它要求用户从当前时间更改时间。我有系统时间和用户时间。代码应该是什么,以便显示用户时间。我正在使用的愚蠢代码:

        Date curDate = new Date();
        //changeTime i have got from the user
        double time=0;
        if(curDate.getTime()- changeTime.getTime() > 0){
            time = curDate.getTime() - changeTime.getTime();
            time = time/(1000);
        }
        else{
            time = changeTime.getTime()-curDate.getTime();
            time = time/(1000);
        }
        if(changeTime.getHours()==0 && changeTime.getMinutes()==0){
            time=0;
        }
        curDate.setHours(curDate.getHours()+(int)time/(3600));
        curDate.setMinutes(curDate.getMinutes()+((int)time/60)%60);

一种简单的方法是根据用户时间获取系统时间
添加
减去

例如,系统时间为下午2点,用户将时间更改为下午4点,然后只需在系统时间上再增加2小时,并将其显示在所需的用户时间字段中。A
新日期()
旨在反映协调世界时(UTC)。“用户在时钟上看到的内容取决于
时区,可以在用于呈现日期的
日期格式中指定。下面的示例以格林尼治标准时间打印同一时刻,并选择从纽约到柏林的时区

附录:我发现将
Date
视为一个模型,将
DateFormat
视为该模型的一个视图是很有帮助的

03-Feb-2013 18:01:42 GMT GMT 1359914502673 03-Feb-2013 13:01:42 EST America/New_York 1359914502673 03-Feb-2013 14:01:42 AST America/Aruba 1359914502673 03-Feb-2013 15:01:42 ART America/Buenos_Aires 1359914502673 03-Feb-2013 16:01:42 BRST America/Sao_Paulo 1359914502673 03-Feb-2013 17:01:42 AZOT Atlantic/Azores 1359914502673 03-Feb-2013 18:01:42 GMT Europe/London 1359914502673 03-Feb-2013 19:01:42 CET Europe/Berlin 1359914502673
我已经粘贴了代码,但是没有得到正确的结果。没有得到正确的结果?你得到了什么样的错误结果
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/** @see http://stackoverflow.com/a/14675418/230513 */
public class TestSDF {

    private static final String s = "dd-MMM-yyyy HH:mm:ss zz";
    private static final DateFormat f = new SimpleDateFormat(s);

    public static void main(String[] args) {
        Date date = new Date();
        print("GMT", date);
        print("America/New_York", date);
        print("America/Aruba", date);
        print("America/Buenos_Aires", date);
        print("America/Sao_Paulo", date);
        print("Atlantic/Azores", date);
        print("Europe/London", date);
        print("Europe/Berlin", date);
    }

    private static void print(String tz, Date d) {
        f.setTimeZone(TimeZone.getTimeZone(tz));
        System.out.println(f.format(d)
            + " " + tz
            + " " + d.getTime());
    }
}