0

我不是专业的程序员,但我在网上寻找解决方案,但找不到任何可以帮助我的东西。

这是我正在尝试为工作中的自动化项目做的事情。我得到了一个以毫秒为单位的时间值。我需要取那个时间值,加上 6 分钟,然后检索小时、分钟和 AM_PM 值,这样我就可以进行测试了。

问题是在我检索时间后,我用日历设置它,做加法,当我去检索分钟和小时时,它们设置不正确。

例如,这是我的代码:

    _logger.info("Get current system time in milliseconds");
    long currentTime = TimeApi.getCurrentSystemTime(_deviceUnderTestPIN);
    _logger.info("Current device time is : " + Long.toString(currentTime));

    _logger.info("Set Calendar object with device time");
    Calendar now = Calendar.getInstance();
    now.setTimeInMillis(currentTime);

    long timeSet = now.getTimeInMillis();
    _logger.info("Calendar object is set to : " + Long.toString(timeSet));

    _logger.info("add mintuesToAdd to current time");
    now.add(Calendar.MINUTE, minutesToAdd);

    long timeAdd = now.getTimeInMillis();
    _logger.info("Calendar Time after Add: " + Long.toString(timeAdd));

    _logger.info("set hour and minute");
    // if the hour is 12am or 12pm the Calendar object will return 0, we need to pass  in 12 so we will set it to 12 below if it returns 0
    hour = now.get(Calendar.HOUR);
    if (hour == 0) {
        hour = 12;
    }
    minutes = now.get(Calendar.MINUTE);

    _logger.info("set amPM");
    if (now.get(Calendar.AM_PM) == 0) {
        amPM = false;
    } else {
        amPM = true;
    }

    _logger.info("Setting alarm hour to: " + Integer.toString(hour));
    _logger.info("Setting the alarm minutes to: " + Integer.toString(minutes));
    _logger.info("Setting alarm AM_PM to: " + Boolean.toString(amPM));

这是我的测试运行的输出:

2013-06-06  13:15:36.007  INFO Current device time is : 1370524535000
2013-06-06  13:15:36.007  INFO Set Calendar object with device time
2013-06-06  13:15:36.007  INFO Calendar object is set to : 1370524535000
2013-06-06  13:15:36.007  INFO add mintuesToAdd to current time
2013-06-06  13:15:36.007  INFO Calendar Time after Add: 1370524895000
2013-06-06  13:15:36.007  INFO set hour and minute
2013-06-06  13:15:36.007  INFO set amPM
2013-06-06  13:15:36.023  INFO Setting alarm hour to: 1
2013-06-06  13:15:36.023  INFO Setting the alarm minutes to: 21
2013-06-06  13:15:36.023  INFO Setting alarm AM_PM to: true

如您所见,我拥有并尝试将其设置为 Thu Jun 06 2013 09:15:35 GMT-0400(东部夏令时间)。所以我不明白的部分是为什么当我调用 now.get(Calendar.HOUR) 时会占用服务器时间???

任何帮助将不胜感激

4

2 回答 2

1

我认为在获取 Calendar 的实例时需要使用 timezome 参数,如下所示:

Calendar now = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
于 2013-06-06T15:33:08.910 回答
0
于 2018-02-01T04:27:52.940 回答