UTCTime { public static int dateTimeToUTCTimestamp(@Nonnull final DateTime dateTime) { assert dateTime != null; if (dateTime.isBefore(MIN_TIMESTAMP_DATE) || dateTime.isAfter(MAX_TIMESTAMP_DATE)) { throw new IllegalArgumentException( "Given date " + dateTime + "not within valid tiemstamp dates " + MIN_TIMESTAMP_DATE + ", " + MAX_TIMESTAMP_DATE); } return (int) (from(dateTime).getMillis() / 1000); } private  UTCTime(); @SuppressWarnings("SSBasedInspection") @Nonnull static DateTime now(); @Nonnull static DateTime from(@Nonnull final DateTime time); @Nonnull static DateTime from(@Nonnull final Date time); @Nonnull static DateTime parse(@Nonnull final String str); static int dateTimeToUTCTimestamp(@Nonnull final DateTime dateTime); static final DateTime MIN_TIMESTAMP_DATE; static final DateTime MAX_TIMESTAMP_DATE; }
@Test public void testDateTimeToUTCTimestamp() { LOG.info("testDateTimeToUTCTimestamp"); final DateTime dt1 = DateTime.parse("1960-10-04T08:15:30-05:00").withZone(DateTimeZone.UTC); final int t1 = UTCTime.dateTimeToUTCTimestamp(dt1); Assert.assertEquals(-291638670, t1); final DateTime dt2 = DateTime.parse("2000-10-04T08:15:30-05:00").withZone(DateTimeZone.UTC); final int t2 = UTCTime.dateTimeToUTCTimestamp(dt2); Assert.assertEquals(970665330, t2); final DateTime dt3 = DateTime.parse("2020-10-04T08:15:30-05:00").withZone(DateTimeZone.UTC); final int t3 = UTCTime.dateTimeToUTCTimestamp(dt3); Assert.assertEquals(1601817330, t3); final DateTime dt4 = UTCTime.MIN_TIMESTAMP_DATE; final int t4 = UTCTime.dateTimeToUTCTimestamp(dt4); Assert.assertEquals(-2147483646, t4); final DateTime dt5 = UTCTime.MAX_TIMESTAMP_DATE; final int t5 = UTCTime.dateTimeToUTCTimestamp(dt5); Assert.assertEquals(2147483647, t5); try { final DateTime dt6 = UTCTime.MIN_TIMESTAMP_DATE.minusSeconds(1); UTCTime.dateTimeToUTCTimestamp(dt6); Assert.fail("Expected to fail because it is less than minimum allowed date"); } catch (final IllegalArgumentException ignored) { } try { final DateTime dt7 = UTCTime.MAX_TIMESTAMP_DATE.plusSeconds(1); UTCTime.dateTimeToUTCTimestamp(dt7); Assert.fail("Expected to fail because it is more than maximum allowed date"); } catch (final IllegalArgumentException ignored) { } }