diff --git a/core-java-modules/core-java-datetime-conversion/src/main/java/com/baeldung/timestamptolong/TimestampToLong.java b/core-java-modules/core-java-datetime-conversion/src/main/java/com/baeldung/timestamptolong/TimestampToLong.java index 2ebe30f4ff..25a8a57ba7 100644 --- a/core-java-modules/core-java-datetime-conversion/src/main/java/com/baeldung/timestamptolong/TimestampToLong.java +++ b/core-java-modules/core-java-datetime-conversion/src/main/java/com/baeldung/timestamptolong/TimestampToLong.java @@ -1,32 +1,31 @@ package com.baeldung.timestamptolong; -import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.Date; public class TimestampToLong { - public void usingSimpleDateFormat() throws ParseException { + + public long usingSimpleDateFormat(String timestampString) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String currentDateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); - long actualTimestamp = sdf.parse(currentDateString).getTime(); + Date date = sdf.parse(timestampString); + String currentDateString = sdf.format(date); + return sdf.parse(currentDateString).getTime(); } - public void usingInstantClass() { - Instant instant = Instant.now(); - long actualTimestamp = instant.toEpochMilli(); + public long usingInstantClass(String timestampString) { + Instant instant = LocalDateTime.parse(timestampString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + .atZone(ZoneId.systemDefault()) + .toInstant(); + return instant.toEpochMilli(); } - public void usingTimestamp() { - Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - long actualTimestamp = timestamp.getTime(); + public long usingJava8DateTime(String timestampString) { + LocalDateTime localDateTime = LocalDateTime.parse(timestampString.replace(" ", "T")); + return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } - - public void usingJava8DateTime() { - LocalDateTime localDateTime = LocalDateTime.now(); - long actualTimestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); - } -} \ No newline at end of file +} diff --git a/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/timestamptolong/TimestampToLongUnitTest.java b/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/timestamptolong/TimestampToLongUnitTest.java index ede8f7792d..868d019cf2 100644 --- a/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/timestamptolong/TimestampToLongUnitTest.java +++ b/core-java-modules/core-java-datetime-conversion/src/test/java/com/baeldung/timestamptolong/TimestampToLongUnitTest.java @@ -2,51 +2,42 @@ package com.baeldung.timestamptolong; import org.junit.Test; -import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.Date; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; public class TimestampToLongUnitTest { - - private static final long TOLERANCE = 1000; + private static final String timestampString = "2023-11-15 01:02:03"; @Test - public void givenSimpleDateFormat_whenFormattingDate_thenTConvertToLong() throws ParseException { + public void givenSimpleDateFormat_whenFormattingDate_thenConvertToLong() throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date date = sdf.parse(timestampString); - String currentDateString = sdf.format(new Date()); + String currentDateString = sdf.format(date); long actualTimestamp = sdf.parse(currentDateString).getTime(); - - assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE); + assertEquals(1700010123000L, actualTimestamp); } @Test - public void givenInstantClass_whenGettingTimestamp_thenTConvertToLong() { - Instant instant = Instant.now(); + public void givenInstantClass_whenGettingTimestamp_thenConvertToLong() { + Instant instant = LocalDateTime.parse(timestampString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + .atZone(ZoneId.systemDefault()) + .toInstant(); long actualTimestamp = instant.toEpochMilli(); - - assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE); + assertEquals(1700010123000L, actualTimestamp); } @Test - public void givenTimestamp_whenCreatingTimestamp_thenTConvertToLong() { - Timestamp timestamp = new Timestamp(System.currentTimeMillis()); - long actualTimestamp = timestamp.getTime(); - - assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE); - } - - @Test - public void givenJava8DateTime_whenGettingTimestamp_thenTConvertToLong() { - LocalDateTime localDateTime = LocalDateTime.now(); + public void givenJava8DateTime_whenGettingTimestamp_thenConvertToLong() { + LocalDateTime localDateTime = LocalDateTime.parse(timestampString.replace(" ", "T")); long actualTimestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); - - assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE); + assertEquals(1700010123000L, actualTimestamp); } }