Java localdate to zoneddatetime

Java Convert LocalDate to ZonedDateTime

In Java ZonedDateTime is a date-time data type with a time-zone in the ISO-8601 calendar system, such as 2022-07-11T10:15:30+01:00 Europe/Paris. In this Java core tutorial we learn how to convert a java.time.LocalDate object to a java.time.ZondedDateTime object in Java programming language.

How to convert LocalDate to ZonedDateTime

In Java with a given LocalDate object we can use LocalDate.atStartOfDay() method with a specified ZoneId to convert LocalDate to ZonedDateTime.

In the following Java program we convert a LocalDate to ZonedDateTime with the default system time zone.

import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; public class ConvertLocalDateToZonedDateTimeExample1  public static void main(String. args)  LocalDate localDate = LocalDate.of(2022, 7, 11); ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zonedDateTime = localDate.atStartOfDay(zoneId); System.out.println("LocalDate: " + localDate); System.out.println("ZonedDateTime: " + zonedDateTime); > >
LocalDate: 2022-07-11 ZonedDateTime: 2022-07-11T00:00+07:00[Asia/Bangkok]

We can convert LocalDate to ZonedDateTime at a specified time zone, using the ZoneId.of() method to get instance of ZoneId as the following Java program.

import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; public class ConvertLocalDateToZonedDateTimeExample2  public static void main(String. args)  LocalDate localDate = LocalDate.of(2022, 7, 11); ZoneId sydneyZoneId = ZoneId.of("Australia/Sydney"); ZonedDateTime zonedDateTime = localDate.atStartOfDay(sydneyZoneId); System.out.println("LocalDate: " + localDate); System.out.println("ZonedDateTime: " + zonedDateTime); > >
LocalDate: 2022-07-11 ZonedDateTime: 2022-07-11T00:00+10:00[Australia/Sydney]

Источник

Convert between LocalDate and ZonedDateTime

Learn to convert from LocalDate to ZonedDateTime and from ZonedDateTime to LocalDate in Java 8.

As we know, LocalDate represents a calendar date without the time and the zone information. ZonedDateTime instance contains all three information i.e. date, time and zone.

ZonedDateTime = LocalDate + time + timezone

1. LocalDate to ZonedDateTime

To convert a LocalDate instance to ZonedDateTime instance, we have two approaches.

1.1. LocalDate -> ZonedDateTime

If we only want to convert a local date in the current timezone to localdate in a different timezone i.e. only want to add zone information, then we can use LocalDate.atStartOfDay(zoneId) method.

LocalDate localDate = LocalDate.now(); ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.of("EST5EDT")); System.out.println(zonedDateTime);

1.2. LocalDate -> LocalDateTime -> ZonedDateTime

If we want to add both time and timezone information to a localdate, then we need to add both parts one by one to get to ZonedDateTime instance. We can use the following methods to add the time information to local date.

  • ZonedDateTime atStartOfDay()
  • ZonedDateTime atTime(LocalTime time)
  • ZonedDateTime atTime(int hour, int minutes)
  • ZonedDateTime atTime(int hour, int minutes, int seconds)
  • ZonedDateTime atTime(int hour, int minute, int second, int nanoOfSecond)

Then we can use LocalDateTime.atZone(ZoneId) method to add zone information.

LocalDate localDate = LocalDate.now(); //local date LocalDateTime localDateTime = localDate.atTime(10, 45, 56); //Add time information ZoneId zoneId = ZoneId.of("Asia/Kolkata"); // Zone information ZonedDateTime zdtAtAsia = localDateTime.atZone(zoneId); // add zone information ZonedDateTime zdtAtET = zdtAtAsia .withZoneSameInstant(ZoneId.of("America/New_York")); // Same time in ET timezone System.out.println(zdtAtAsia); System.out.println(zdtAtET);
2019-04-02T10:45:56+05:30[Asia/Kolkata] 2019-04-02T01:15:56-04:00[America/New_York]

2. ZonedDateTime to LocalDate

To convert ZonedDateTime to LocalDate instance, use toLocalDate() method. It returns a LocalDate with the same year, month and day as given date-time.

ZonedDateTime zonedDateTime = ZonedDateTime.now(); LocalDate localDate = zonedDateTime.toLocalDate(); System.out.println(localDate);

Источник

Java Convert LocalDate to ZonedDateTime

Twitter Facebook Google Pinterest

A quick guide to convert LocalDate to ZonedDateTime in different ways in java 8.

1. Overview

LocalDate is having only date units without timezone and ZonedDateTime object contains date, time units with timezone information. But, when we want to convert LocalDate to ZonedDateTime, we have to supply the remaining time units and timezone values and add to the LocalDate object.

This is solved using a simple method atStartOfDay() from LocalDate class with ZoneId information as an argument.

Java Convert LocalDate to ZonedDateTime

2. Java 8 Convert String to ZonedDateTime Object

In the below example, we are taking the string as input date in format «yyyy-MM-dd» and pass to the LocalDate.parse() method and then covert it to the ZonedDateTime object.

package com.javaprogramto.java8.dates.localdate; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; /** * Example to convert String to ZonedDateTime. * * @author JavaProgramTo.com * */ public class StringToZonedDateTimeExample < public static void main(String[] args) < // date in string format String date = "2022-03-02"; // Converting String to LocalDate. LocalDate localDate = LocalDate.parse(date); // Creating timezone ZoneId zoneid = ZoneId.systemDefault(); // LocalDate to zoneddatetime ZonedDateTime zonedDateTimeFromString = localDate.atStartOfDay(zoneid); System.out.println("String date : " + date); System.out.println("ZonedDateTime : " + zonedDateTimeFromString); >>
String date : 2022-03-02 ZonedDateTime : 2022-03-02T00:00+05:30[Asia/Kolkata]

3. Java 8 Convert LocalDate to ZonedDateTime Object

package com.javaprogramto.java8.dates.localdate; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; /** * Example to convert LocalDate to ZonedDateTime with PST zone. * * @author JavaProgramTo.com * */ public class LocalDateToZonedDateTimePSTExample < public static void main(String[] args) < // Creating LocalDate object LocalDate localDate = LocalDate.now(); // Creating timezone with PST zone id. We can pass here any timezone id supported by java. ZoneId zoneid = ZoneId.of("US/Pacific"); // LocalDate to PST zoneddatetime ZonedDateTime zonedDateTimeInPST = localDate.atStartOfDay(zoneid); System.out.println("localDate : " + localDate); System.out.println("ZonedDateTime in PST : " + zonedDateTimeInPST); >>
localDate : 2020-12-07 ZonedDateTime in PST : 2020-12-07T00:00-08:00[US/Pacific]

4. Conclusion

In this article, We’ve seen the examples on how to convert LocalDate to ZonedDateTime in java 8 api.

Labels:

SHARE:

Twitter Facebook Google Pinterest

About Us

Java 8 Tutorial

  • Java 8 New Features
  • Java 8 Examples Programs Before and After Lambda
  • Java 8 Lambda Expressions (Complete Guide)
  • Java 8 Lambda Expressions Rules and Examples
  • Java 8 Accessing Variables from Lambda Expressions
  • Java 8 Method References
  • Java 8 Functional Interfaces
  • Java 8 — Base64
  • Java 8 Default and Static Methods In Interfaces
  • Java 8 Optional
  • Java 8 New Date Time API
  • Java 8 — Nashorn JavaScript

Java Threads Tutorial

Kotlin Conversions

Kotlin Programs

Java Conversions

  • Java 8 List To Map
  • Java 8 String To Date
  • Java 8 Array To List
  • Java 8 List To Array
  • Java 8 Any Primitive To String
  • Java 8 Iterable To Stream
  • Java 8 Stream To IntStream
  • String To Lowercase
  • InputStream To File
  • Primitive Array To List
  • Int To String Conversion
  • String To ArrayList

Java String API

  • charAt()
  • chars() — Java 9
  • codePointAt()
  • codePointCount()
  • codePoints() — Java 9
  • compareTo()
  • compareToIgnoreCase
  • concat()
  • contains()
  • contentEquals()
  • copyValueOf()
  • describeConstable() — Java 12
  • endsWith()
  • equals()
  • equalsIgnoreCase()
  • format()
  • getBytes()
  • getChars()
  • hashcode()
  • indent() — Java 12
  • indexOf()
  • intern()
  • isBlank() — java 11
  • isEmpty()
  • join()
  • lastIndexOf()
  • length()
  • lines()
  • matches()
  • offsetByCodePoints()
  • regionMatches()
  • repeat()
  • replaceFirst()
  • replace()
  • replaceAll()
  • resolveConstantDesc()
  • split()
  • strip(), stripLeading(), stripTrailing()
  • substring()
  • toCharArray()
  • toLowerCase()
  • transform() — Java 12
  • valueOf()

Spring Boot

$show=Java%20Programs

$show=Kotlin

accumulo,1,ActiveMQ,2,Adsense,1,API,37,ArrayList,18,Arrays,24,Bean Creation,3,Bean Scopes,1,BiConsumer,1,Blogger Tips,1,Books,1,C Programming,1,Collection,8,Collections,37,Collector,1,Command Line,1,Comparator,1,Compile Errors,1,Configurations,7,Constants,1,Control Statements,8,Conversions,6,Core Java,149,Corona India,1,Create,2,CSS,1,Date,3,Date Time API,38,Dictionary,1,Difference,2,Download,1,Eclipse,3,Efficiently,1,Error,1,Errors,1,Exceptions,8,Fast,1,Files,17,Float,1,Font,1,Form,1,Freshers,1,Function,3,Functional Interface,2,Garbage Collector,1,Generics,4,Git,9,Grant,1,Grep,1,HashMap,2,HomeBrew,2,HTML,2,HttpClient,2,Immutable,1,Installation,1,Interview Questions,6,Iterate,2,Jackson API,3,Java,32,Java 10,1,Java 11,6,Java 12,5,Java 13,2,Java 14,2,Java 8,128,Java 8 Difference,2,Java 8 Stream Conversions,4,java 8 Stream Examples,12,Java 9,1,Java Conversions,14,Java Design Patterns,1,Java Files,1,Java Program,3,Java Programs,114,Java Spark,1,java.lang,4,java.util. function,1,JavaScript,1,jQuery,1,Kotlin,11,Kotlin Conversions,6,Kotlin Programs,10,Lambda,2,lang,29,Leap Year,1,live updates,1,LocalDate,1,Logging,1,Mac OS,3,Math,1,Matrix,6,Maven,1,Method References,1,Mockito,1,MongoDB,3,New Features,1,Operations,1,Optional,6,Oracle,5,Oracle 18C,1,Partition,1,Patterns,1,Programs,1,Property,1,Python,2,Quarkus,1,Read,1,Real Time,1,Recursion,2,Remove,2,Rest API,1,Schedules,1,Serialization,1,Servlet,2,Sort,1,Sorting Techniques,8,Spring,2,Spring Boot,23,Spring Email,1,Spring MVC,1,Streams,31,String,61,String Programs,28,String Revese,1,StringBuilder,1,Swing,1,System,1,Tags,1,Threads,11,Tomcat,1,Tomcat 8,1,Troubleshoot,26,Unix,3,Updates,3,util,5,While Loop,1,

Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content

Источник

Java – Convert LocalDate to ZonedDateTime

In this tutorial, we will see how to convert LocalDate to ZonedDateTime. LocalDate represents the date without time and zone information, ZonedDateTime represents the date with time and zone. To convert the LocalDate to ZonedDateTime, we must add the time and zone id with the local date.

Example 1: Converting the LocalDate given in String format to the ZonedDateTime

In this example, we have date in the string format, we are first parsing the given date in LocalDate and then converting the LocalDate to ZonedDateTime using the atStartOfDay() method.

import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; public class Example < public static void main(String[] args) < //Parsing the given string into a LocalDate LocalDate localDate = LocalDate.parse("2017-07-22"); //Displaying the LocalDate System.out.println("LocalDate is: "+localDate); /* Converting the LocalDate to ZonedDateTime by using the * default zone id and appending the midnight time and default * zone id to the local date using atStartOfDay() method */ ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); //Displaying the ZonedDateTime System.out.println("ZoneDateTime is: "+zonedDateTime); >>
LocalDate is: 2017-07-22 ZoneDateTime is: 2017-07-22T00:00+05:30[Asia/Kolkata]

Example 2: LocalDate to ZonedDateTime

In the first example we have the date as String. In this example we have the day, month and year information, using which we are creating an instance of LocalDate and then appending the time and zone id with it using the atStartOfDay() method, the same way that we have seen in the above program.

import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; public class Example < public static void main(String[] args) < //Converting the given year, month, date to LocalDate LocalDate localDate = LocalDate.of(2017, 07, 22); //printing the local date System.out.println("LocalDate is: "+localDate); /* Converting the LocalDate to ZonedDateTime the same way that * we have seen in the above example */ ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault()); //Displaying the output ZonedDateTime System.out.println("ZoneDateTime is: "+zonedDateTime); >>
LocalDate is: 2017-07-22 ZoneDateTime is: 2017-07-22T00:00+05:30[Asia/Kolkata]

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Источник

Читайте также:  Php xpath and or
Оцените статью