-->
Skip to main content

Posts

Convert ZonedDateTime to Timestamp in Java [2021]

Convert ZonedDateTime to Timestamp in Java [2021]  In this tutorial, we are going to cover how to convert ZonedDateTime to Timestamp and how to convert TImestamp to ZonedDateTime. To Convert ZonedDateTime to sql Timestamp instance we have TimeStamp.valueOf(LocalDateTime time) method which takes LocalDateTime as parameter. So to convert ZonedDateTime to java.sql.Timestamp we first have to convert ZonedDateTime to LocalDateTime and then we use the Timestamp.valueOf(LocalDateTime time) method to convert  LocalDateTime to Timestamp. Contents Example : Convert ZonedDateTime to TimeStamp Example : Convert TimeStamp to ZonedDateTime Conclusion References Convert ZonedDateTime to TimeStamp To Convert java.time.ZonedDateTime to java.sql.Timestamp instance we have two steps: First Convert ZonedDateTime to LocalDateTime using ZonedDateTime.toLocalDateTime() method. Second Convert LocalDateTime to Timestamp using Timestamp.valueOf(LocalDateTime time) method. Example ...

ZonedDateTime to Date Conversion [2021]

ZonedDateTime to Date Conversion [2021] To convert ZonedDateTime to Date we have to simply convert ZonedDateTime to Instant instance and then instant instance to Date instance. ZonedDateTime to Date Conversion can be done using Date.from(Instant instant) method provided by java.util.Date class . Contents Example 1: ZonedDateTime to Date Example 2: Convert ZonedDateTime to LocalDate Example 3: Convert ZonedDateTime to UTC Example 4: Convert ZonedDateTime to LocalDateTime Conclusion References Example 1: ZonedDateTime to Date In this example, we will take ZonedDateTime instance and convert that to Date instance using Date.from(Instant instant) method. import java.time.Instant; import java.time.ZonedDateTime; import java.util.Date; public class ZonedDateTimetoDateEx1 { public static void main(String[] args) { //Creating ZonedDateTime instance ZonedDateTime time = ZonedDateTime.now(); System.out.println("ZonedDateTime : " + time); ...

LocalDate to ZonedDateTime Conversion [2021]

LocalDate to ZonedDateTime Conversion [2021] To Convert LocalDate to ZonedDateTime we must add time and zoneId with LocalDate as ZonedDateTime  has time and ZoneId added with the date. LocalDate to ZonedDateTime conversion can be done using  LocalDate. atStartOfDay() method which will first convert localdate to localdatetime   and then localdatetime to ZonedDatetime using atZone(ZoneId zone) method on localdatetime instance. Contents Example 1: LocalDate to ZonedDateTime Example 2: LocalDate to ZonedDateTime UTC Example 3: LocalDate to ZonedDateTime change timezone Example 4: string to ZonedDateTime Conclusion References Example 1: LocalDate to ZonedDateTime In this example, we are going to create a localdate instance and then convert that to ZonedDateTime instance. import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; public class LocalDateToZonedDateTimeEx1 { public static void main(String[] args) { // Crea...