This PR is related to BAEL-7181 (#15230)
* Update TimestampToLongUnitTest.java * Update TimestampToLong.java
This commit is contained in:
parent
0b229a9af4
commit
290147ff08
|
@ -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 void usingJava8DateTime() {
|
||||
LocalDateTime localDateTime = LocalDateTime.now();
|
||||
long actualTimestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
||||
public long usingJava8DateTime(String timestampString) {
|
||||
LocalDateTime localDateTime = LocalDateTime.parse(timestampString.replace(" ", "T"));
|
||||
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue