This PR is related to BAEL-7181 (#15192)
* This commit is related to BAEL-7181 This commit aims to add a class "TimestampToLong.java" that provides several approaches to convert timestamp string into long. * This commit is related to BAEL-7181 This commit aims to add a test class "TimestampToLongUnitTest.java" that provides several approaches to convert timestamp string into long.
This commit is contained in:
parent
8532499dcc
commit
c5af2f3f3b
|
@ -0,0 +1,32 @@
|
|||
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.util.Date;
|
||||
|
||||
public class TimestampToLong {
|
||||
public void usingSimpleDateFormat() 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();
|
||||
}
|
||||
|
||||
public void usingInstantClass() {
|
||||
Instant instant = Instant.now();
|
||||
long actualTimestamp = 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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
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.util.Date;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class TimestampToLongUnitTest {
|
||||
|
||||
private static final long TOLERANCE = 1000;
|
||||
|
||||
@Test
|
||||
public void givenSimpleDateFormat_whenFormattingDate_thenTConvertToLong() throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
String currentDateString = sdf.format(new Date());
|
||||
long actualTimestamp = sdf.parse(currentDateString).getTime();
|
||||
|
||||
assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInstantClass_whenGettingTimestamp_thenTConvertToLong() {
|
||||
Instant instant = Instant.now();
|
||||
long actualTimestamp = instant.toEpochMilli();
|
||||
|
||||
assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE);
|
||||
}
|
||||
|
||||
@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();
|
||||
long actualTimestamp = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
||||
|
||||
assertTrue(Math.abs(System.currentTimeMillis() - actualTimestamp) < TOLERANCE);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue