Date to zoneddatetime java

Java Convert Date to ZonedDateTime

In this Java core tutorial we learn how to convert a java.util.Date object to a java.time.ZonedDateTime object in Java programming language.

How to convert Date to ZonedDateTime in Java

In Java, with a given Date object you can follow these steps to convert it to a ZonedDateTime object.

  • Step 1: convert Date object to Instant object using the Date.toInstant() method.
  • Step 2: Use the ZonedDateTime.ofInstant(Instant instant, ZoneId zone) method to convert the above Instant object to ZonedDateTime object in system default time zone.
import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; import java.util.TimeZone; public class ConvertDateToZonedDateTimeExample1  public static void main(String. args)  Date date = new Date(); Instant instant = date.toInstant(); ZoneId zoneId = TimeZone.getDefault().toZoneId(); ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId); System.out.println("Date: " + date); System.out.println("ZonedDateTime: " + zonedDateTime); > >
Date: Fri May 20 00:21:24 ICT 2022 ZonedDateTime: 2022-05-20T00:21:24.936+07:00[Asia/Bangkok]

Источник

javamultiplex / ConvertDateToZonedDateTime.java

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

package com . javamultiplex . datetime ;
import java . time . Instant ;
import java . time . ZoneId ;
import java . time . ZonedDateTime ;
import java . util . Date ;
/**
*
* @author Rohit Agarwal
* @category Date and Time
* @problem How to convert Date to ZonedDateTime?
*
*/
public class ConvertDateToZonedDateTime
public static void main ( String [] args )
Date date = new Date ();
System . out . println ( «Date : » + date );
// Get system default time zone id.
ZoneId defaultZoneId = ZoneId . systemDefault ();
// Convert Date to Instant.
Instant instant = date . toInstant ();
// Instant + default time zone.
ZonedDateTime zonedDateTime = instant . atZone ( defaultZoneId );
System . out . println ( «ZonedDateTime : » + zonedDateTime );
>
>

Источник

Java Convert SQL Date to ZonedDateTime

In Java, with a given SQL Date object we can follow these steps to convert it to a ZonedDateTime object.

  • Step 1: use Date.toLocalDate() method to convert the SQL Date object to a LocalDate object.
  • Step 2: use LocalDate.atStartOfDay(ZoneId zone) method to convert the LocalDate object from step 1 to a ZonedDateTime object with system default time zone.
import java.sql.Date; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; public class ConvertSQLDateToZonedDateTimeExample1  public static void main(String. args)  Date date = new Date(System.currentTimeMillis()); // Convert SQL Date object to ZonedDateTime object LocalDate localDate = date.toLocalDate(); ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); System.out.println("SQL Date: " + date); System.out.println("ZonedDateTime: " + zonedDateTime); > >
SQL Date: 2022-05-23 ZonedDateTime: 2022-05-23T00:00+07:00[Asia/Bangkok]

Источник

Читайте также:  Python slice multidimensional array
Оцените статью