Files
spring-boot-rest/java-dates/src/test/java/com/baeldung/timestamp/StringToTimestampConverterUnitTest.java
Laurentiu Delcea ba98695605 Convert between String and Timestamp
Issue: BAEL-2325
2018-11-19 08:05:05 -07:00

22 lines
771 B
Java

package com.baeldung.timestamp;
import org.junit.Assert;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class StringToTimestampConverterUnitTest {
@Test
public void givenDatePattern_whenParsing_thenTimestampIsCorrect() {
String pattern = "MMM dd, yyyy HH:mm:ss.SSSSSSSS";
String timestampAsString = "Nov 12, 2018 13:02:56.12345678";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
LocalDateTime localDateTime = LocalDateTime.from(formatter.parse(timestampAsString));
Timestamp timestamp = Timestamp.valueOf(localDateTime);
Assert.assertEquals("2018-11-12 13:02:56.12345678", timestamp.toString());
}
}