diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java | 49 | ||||
-rw-r--r-- | src/test/java/org/uic/barcode/test/TimeStampTest.java | 28 |
2 files changed, 51 insertions, 26 deletions
diff --git a/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java b/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java index 2f54da3..3095ea5 100644 --- a/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java +++ b/src/main/java/org/uic/barcode/dynamicContent/fdc1/TimeStamp.java @@ -1,8 +1,5 @@ package org.uic.barcode.dynamicContent.fdc1;
-import java.time.Instant;
-import java.time.ZoneOffset;
-import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@@ -104,26 +101,26 @@ public class TimeStamp { * @return the date and time of content creation in UTC
*/
public Date getTimeAsDate() {
-
- ZonedDateTime now = Instant.now().atZone(ZoneOffset.UTC);
- int dayOfYear = now.getDayOfYear();
-
+
+ Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+ int dayOfYear = now.get(Calendar.DAY_OF_YEAR);
+
if (dayOfYear - day.intValue() > 250) {
- now = now.plusYears(1);
+ now.add(Calendar.YEAR, 1);
}
if (day.intValue() - dayOfYear > 250) {
- now = now.minusYears(1);
+ now.add(Calendar.YEAR, -1);
}
- now = now.withDayOfYear(1);
- now = now.withSecond(0);
- now = now.withHour(0);
- now = now.withMinute(0);
- now = now.withNano(0);
- now = now.withDayOfYear(day.intValue());
- now = now.plusSeconds(secondOfDay);
-
- return Date.from(now.toInstant());
+ now.set(Calendar.DAY_OF_YEAR, 1);
+ now.set(Calendar.SECOND, 0);
+ now.set(Calendar.HOUR_OF_DAY, 0);
+ now.set(Calendar.MINUTE, 0);
+ now.set(Calendar.MILLISECOND, 0);
+ now.set(Calendar.DAY_OF_YEAR, day.intValue());
+ now.add(Calendar.SECOND, secondOfDay.intValue());
+
+ return now.getTime();
}
@@ -134,13 +131,15 @@ public class TimeStamp { */
public void setDateTime(Date dateUTC) {
- ZonedDateTime date = dateUTC.toInstant().atZone(ZoneOffset.UTC);
-
- day = (long) date.getDayOfYear();
-
- secondOfDay = (long) date.getSecond();
- secondOfDay = secondOfDay + 60 * (long) date.getMinute();
- secondOfDay = secondOfDay + 60 * 60 * (long) date.getHour();
+ Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
+ c.setTime(dateUTC);
+
+ day = (long) c.get(Calendar.DAY_OF_YEAR);
+
+ secondOfDay = (long) c.get(Calendar.SECOND);
+ secondOfDay += 60L * c.get(Calendar.MINUTE);
+ secondOfDay += 3600L * c.get(Calendar.HOUR_OF_DAY);
+
}
diff --git a/src/test/java/org/uic/barcode/test/TimeStampTest.java b/src/test/java/org/uic/barcode/test/TimeStampTest.java index f03a63c..a3e8256 100644 --- a/src/test/java/org/uic/barcode/test/TimeStampTest.java +++ b/src/test/java/org/uic/barcode/test/TimeStampTest.java @@ -84,6 +84,32 @@ public class TimeStampTest { assert (secondOfDay2 >= secondOfDay - 1);
assert (secondOfDay2 <= secondOfDay + 1);
-
}
+
+ @Test public void testSetDateTime() {
+ ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC"));
+ Date dateNow = Date.from(now.toInstant());
+
+ TimeStamp ts = new TimeStamp();
+ ts.setDateTime(dateNow);
+
+ assert (ts.day.intValue() == now.get(ChronoField.DAY_OF_YEAR));
+ assert (ts.secondOfDay.intValue() == now.get(ChronoField.SECOND_OF_DAY));
+ }
+
+ @Test public void testGetTimeAsDate() {
+ ZonedDateTime now = ZonedDateTime.now(ZoneId.of("UTC")).withNano(0); // we ignore nano seconds
+ Date dateNow = Date.from(now.toInstant());
+
+ TimeStamp ts = new TimeStamp();
+ ts.setDateTime(dateNow);
+
+ Date resDate = ts.getTimeAsDate();
+
+ assert (resDate.getTime() == dateNow.getTime());
+ ZonedDateTime res = ZonedDateTime.ofInstant(resDate.toInstant(), ZoneId.of("UTC")).withNano(0);
+ assert (res.compareTo(now) == 0);
+
+ }
+
}
|