Java 8 – Convert ZonedDateTime to Timestamp

Java example to convert java.time.ZonedDateTime to java.sql.Timestamp and vice verse. 1. ZonedDateTime -> Timestamp TimeExample1.java package com.mkyong.jdbc; import java.sql.Timestamp; import java.time.ZonedDateTime; public class TimeExample1 { public static void main(String[] args) { ZonedDateTime now = ZonedDateTime.now(); // 1. ZonedDateTime to TimeStamp Timestamp timestamp = Timestamp.valueOf(now.toLocalDateTime()); // 2. ZonedDateTime to TimeStamp , no different Timestamp timestamp2 = …

Read more

Java Date Time Tutorials

A collection of Java date and time examples. 1. Java Date Time APIs In old days, we use the following classic Date and Calendar APIs to represent and manipulate date. java.util.Date – date and time, print with default time-zone. java.util.Calendar – date and time, more methods to manipulate date. java.text.SimpleDateFormat – formatting (date -> text), …

Read more

Java 8 – ZonedDateTime examples

Few Java 8 java.time.ZonedDateTime examples to show you how to convert a time zone between different countries. Table of contents 1. Convert LocalDateTime to ZonedDateTime 2. Malaysia (UTC+08:00) -> Japan (UTC+09:00) 3. France, Paris (UTC+02:00, DST) -> (UTC-05:00) 4. References 1. Convert LocalDateTime to ZonedDateTime The LocalDateTime has no time zone; to convert the LocalDateTime …

Read more

Java 8 – Convert Date to LocalDate and LocalDateTime

Here is the code to convert java.util.Date to java.time.LocalDate. Date date = new Date(); LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); // different way of create instant object LocalDate localDate = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate(); Convert java.util.Date to java.time.LocalDateTime. Date date = new Date(); LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); Convert java.util.Date to java.time.ZonedDateTime. Date date = new Date(); ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.systemDefault()); …

Read more

Java 8 – Convert Instant to ZonedDateTime

Java 8 examples to show you how to convert from Instant to ZonedDateTime 1. Instant -> ZonedDateTime Example to convert a Instant UTC+0 to a Japan ZonedDateTime UTC+9 InstantZonedDateTime1.java package com.mkyong.date; import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; public class InstantZonedDateTime1 { public static void main(String[] argv) { // Z = UTC+0 Instant instant = Instant.now(); …

Read more

Java – Convert date and time between timezone

In this tutorial, we will show you few examples (ZonedDateTime (Java 8), Date, Calendar and Joda Time) to convert a date and time between different time zones. All examples will be converting the date and time from (UTC+8:00) Asia/Singapore – Singapore Time Date : 22-1-2015 10:15:55 AM to (UTC-5:00) America/New_York – Eastern Standard Time Date …

Read more

Java – How to get current date time

In this tutorial, we will show you how to get the current date time from the new Java 8 java.time.* like Localdate, LocalTime, LocalDateTime, ZonedDateTime, Instant and also the legacy date time APIs like Date and Calendar. Table of contents 1. Get current date time in Java 2. java.time.LocalDate 3. java.time.LocalTime 4. java.time.LocalDateTime 5. java.time.ZonedDateTime …

Read more