This PR is related to BAEL-7181 ()

* Update TimestampToLongUnitTest.java

* Update TimestampToLong.java
This commit is contained in:
MohamedHelmyKassab 2023-11-17 00:27:43 +02:00 committed by GitHub
parent 0b229a9af4
commit 290147ff08
2 changed files with 30 additions and 40 deletions
core-java-modules/core-java-datetime-conversion/src
main/java/com/baeldung/timestamptolong
test/java/com/baeldung/timestamptolong

@ -1,32 +1,31 @@
package com.baeldung.timestamptolong; package com.baeldung.timestamptolong;
import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
public class TimestampToLong { 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"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); Date date = sdf.parse(timestampString);
long actualTimestamp = sdf.parse(currentDateString).getTime(); String currentDateString = sdf.format(date);
return sdf.parse(currentDateString).getTime();
} }
public void usingInstantClass() { public long usingInstantClass(String timestampString) {
Instant instant = Instant.now(); Instant instant = LocalDateTime.parse(timestampString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
long actualTimestamp = instant.toEpochMilli(); .atZone(ZoneId.systemDefault())
.toInstant();
return instant.toEpochMilli();
} }
public void usingTimestamp() { public long usingJava8DateTime(String timestampString) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); LocalDateTime localDateTime = LocalDateTime.parse(timestampString.replace(" ", "T"));
long actualTimestamp = timestamp.getTime(); return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
public void usingJava8DateTime() {
LocalDateTime localDateTime = LocalDateTime.now();
long actualTimestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
} }
} }

@ -2,51 +2,42 @@ package com.baeldung.timestamptolong;
import org.junit.Test; import org.junit.Test;
import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals;
public class TimestampToLongUnitTest { public class TimestampToLongUnitTest {
private static final String timestampString = "2023-11-15 01:02:03";
private static final long TOLERANCE = 1000;
@Test @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"); 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(); long actualTimestamp = sdf.parse(currentDateString).getTime();
assertEquals(1700010123000L, actualTimestamp);
assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE);
} }
@Test @Test
public void givenInstantClass_whenGettingTimestamp_thenTConvertToLong() { public void givenInstantClass_whenGettingTimestamp_thenConvertToLong() {
Instant instant = Instant.now(); Instant instant = LocalDateTime.parse(timestampString, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
.atZone(ZoneId.systemDefault())
.toInstant();
long actualTimestamp = instant.toEpochMilli(); long actualTimestamp = instant.toEpochMilli();
assertEquals(1700010123000L, actualTimestamp);
assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE);
} }
@Test @Test
public void givenTimestamp_whenCreatingTimestamp_thenTConvertToLong() { public void givenJava8DateTime_whenGettingTimestamp_thenConvertToLong() {
Timestamp timestamp = new Timestamp(System.currentTimeMillis()); LocalDateTime localDateTime = LocalDateTime.parse(timestampString.replace(" ", "T"));
long actualTimestamp = timestamp.getTime();
assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE);
}
@Test
public void givenJava8DateTime_whenGettingTimestamp_thenTConvertToLong() {
LocalDateTime localDateTime = LocalDateTime.now();
long actualTimestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); long actualTimestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
assertEquals(1700010123000L, actualTimestamp);
assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE);
} }
} }