How to change java.time.LocalDate
, java.time.LocalDateTime
to java.sql.Timestamp
.
Or vice versa, here`s how to change it.
LocalDateTime, LocalDate are Time APIs added in JAVA8.
LocalDateTime <=> Timestamp
How to convert LocalDateTime to Timestamp and Timestamp to LocalDateTime.
package time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
public class Example01 {
public static void main(String args[]) {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime);
// LocalDateTime -> Convert to Timestamp
Timestamp timestamp2 = Timestamp.valueOf(localDateTime);
System.out.println(timestamp2);
// Time output in milliseconds from 1970/01/01 00:00:00 GMT
System.out.println(timestamp2.getTime());
// Timestamp -> Convert to LocalDateTime
LocalDateTime localDateTime1 = timestamp2.toLocalDateTime();
System.out.println(localDateTime1);
}
}
result
2019-10-31T08:45:54.874
2019-10-31 08:45:54.874
1572479154874
2019-10-31T08:45:54.874
LocalDate <=> Timestamp
How to convert LocalDate to Timestamp and Timestamp to LocalDate.
package time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Example02 {
public static void main(String args[]) {
LocalDate localDate = LocalDate.now();
System.out.println(localDate);
// Convert to LocalDate -> Timestamp
Timestamp timestamp = Timestamp.valueOf(localDate.atStartOfDay());
System.out.println(timestamp);
System.out.println(timestamp.getTime());
// Timestamp -> Convert to LocalDate
LocalDate localDate1 = timestamp.toLocalDateTime().toLocalDate();
System.out.println(localDate1);
}
}
result
2019-10-31
2019-10-31 00:00:00.0
1572447600000
2019-10-31
Reference
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)