Reputation: 23
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
Upvotes: 0
Views: 332
Reputation: 338276
Instant.ofEpochMilli( 1_370_524_535_000L ) // Parse count-of-milliseconds-since-epoch as a `Instant`.
.plus( Duration.ofMinutes( 6 ) ) // Calculate six minutes later.
.atZone( ZoneId.of( "Asia/Kolkata" ) ) // Adjust from UTC of a `Instant` to the time zone of a `ZonedDateTime` object.
.toLocalTime() // Extract the time-of-day, without the date and without the time zone.
.format( // Generate a string.
DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ) // Automatically localize using…
.withLocale( Locale.US ) // …the human language and cultural norms of a given locale.
)
6:51:35 PM
You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.
If given a count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z, then parse as a Instant
. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
Instant instant = Instant.ofEpochMilli( 1_370_524_535_000L ) ;
instant.toString(): 2013-06-06T13:15:35Z
Represent your six minute span of time as a Duration
.
Duration d = Duration.ofMinutes( 6 ) ; // Span of time (hours-minutes-seconds) unattached to the timeline.
Do the math.
Instant sixMinutesLater = instant.plus( d ) ; // Add the duration of six minutes to the UTC moment.
Convert to the more flexible OffsetDateTime
, to complete our other work.
OffsetDateTime odt = OffsetDateTime.ofInstant( sixMinutesLater , ZoneOffset.UTC ) ;
To get the time of day, without a date and without a time zone, extract a LocalTime
.
LocalTime lt = odt.toLocalTime() ;
To ask if it is morning, compare to another LocalTime
representing noon, 12:00.
Boolean isBeforeNoon = lt.isBefore( LocalTime.NOON ) ;
You can interrogate for the parts: getHour
(0-23), getMinute
, and so on.
You may want to generate a string representing this value. Specify a formatting pattern, or automatically localize.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime( FormatStyle.MEDIUM ).withLocale( Locale.US ) ;
String output = lt.format( f ) ;
The above code assumes you want to work in UTC. If instead you want the time-of-day as seen on a wall-clock by the people of a certain region, you must specify a time zone (ZoneId
) to get a ZonedDateTime
object.
ZoneId z = ZoneId.of( "Africa/Tunis" );
ZonedDateTime zdtAfricaTunis = instant.atZone( z );
LocalTime ltAfricaTunis = zdtAfricaTunis.toLocalTime();
Dump to console.
System.out.println( "instant.toString(): " + instant + " and sixMinutesLaterInstant: " + sixMinutesLaterInstant );
System.out.println( "odt.toString(): " + odt + " and lt.toString(): " + lt + " is morning: " + isBeforeNoon + " with output: " + output );
System.out.println( "zdtAfricaTunis.toString(): " + zdtAfricaTunis + " and ltAfricaTunis.toString(): " + ltAfricaTunis );
instant.toString(): 2013-06-06T13:15:35Z and sixMinutesLaterInstant: 2013-06-06T13:21:35Z
odt.toString(): 2013-06-06T13:21:35Z and lt.toString(): 14:21:35 is morning: false with output: 1:21:35 PM
zdtAfricaTunis.toString(): 2013-06-06T14:21:35+01:00[Africa/Tunis] and ltAfricaTunis.toString(): 14:21:35
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 0
Reputation: 34367
I think you need to use the timezome parameter when getting the instance of the Calendar as below:
Calendar now = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
Upvotes: 1