I'm no expert programmer, but I have looked online for a solution to this and cannot find anything to help me.
Here is what I am trying to do for an automation project at work. I am being given a time value in milliseconds. I need to take that time value, add 6 minutes to it, then retrieve the hour, minutes, and AM_PM value so I can then do my test.
The problem is that after I retrieve the time, I then set it with a CALENDAR, do the addition, and when I go to retrieve the minutes and hours, they are not set correctly.
For example, here is my code:
_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));
And here is the output from my test run:
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
As you can see the time value I have and are trying to set it Thu Jun 06 2013 09:15:35 GMT-0400 (Eastern Daylight Time). So the part I don't understand is why is it taking the server time when i call now.get(Calendar.HOUR)???
Any help would be greatly appreciated