How to change java.time.ZonedDateTime
to java.sql.Timestamp
. Or vice versa, here`s how to change it.
ZonedDateTime is a Time API added in JAVA8.
Unlike LocalDateTime, ZonedDateTime has zone information, so you can see how much time difference is in UTC.
ZonedDateTime => Timestamp
How to convert from ZonedDateTime to Timestamp.
package time;
import java.sql.Timestamp;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Example03 {
public static void main(String args[]) {
ZonedDateTime utcDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
System.out.println(utcDateTime);
// ZonedDateTime => Timestamp 변
Timestamp timestamp = Timestamp.valueOf(utcDateTime.toLocalDateTime());
System.out.println(timestamp);
// Time output in milliseconds from 1970/01/01 00:00:00 GMT
System.out.println(timestamp.getTime());
// Convert ZonedDateTime to Instant object => Convert Timestamp
Timestamp timestamp2 = Timestamp.from(utcDateTime.toInstant());
System.out.println(timestamp2);
}
}
result
2019-10-31T00:09:01.962Z[UTC]
2019-10-31 00:09:01.962
1572448141962
2019-10-31 09:09:01.962
Timestamp => ZonedDateTime
How to convert from Timestamp to ZonedDateTime.
package time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Example04 {
public static void main(String args[]) {
Timestamp timestamp = Timestamp.from(Instant.now());
System.out.println(timestamp);
// Timestamp => LocalDateTime 변
LocalDateTime noTimeZoneLocalDateTime = timestamp.toLocalDateTime();
// LocalDateTime => ZonedDateTime (Zone = Seoul)
ZonedDateTime zonedDateTime =
noTimeZoneLocalDateTime.atZone(ZoneId.of("Asia/Seoul"));
System.out.println(zonedDateTime);
// LocalDateTime => ZonedDateTime (Zone = UTC)
ZonedDateTime zonedDateTime1 =
noTimeZoneLocalDateTime.atZone(ZoneId.of("UTC"));
System.out.println(zonedDateTime1);
// LocalDateTime => ZonedDateTime (Zone = "-06:00")
ZonedDateTime zonedDateTime2 =
noTimeZoneLocalDateTime.atZone(ZoneId.of("-06:00"));
System.out.println(zonedDateTime2);
}
}
result
2019-10-31 09:10:35.032
2019-10-31T09:10:35.032+09:00[Asia/Seoul]
2019-10-31T09:10:35.032Z[UTC]
2019-10-31T09:10:35.032-06:00
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)