HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Convert between LocalDateTime to ZonedDateTime

By Lokesh Gupta | Filed Under: Java Date/Time

Java example to convert LocalDateTime to ZonedDateTime and vice-versa.

1) LocalDateTime to ZonedDateTime

ZonedDateTime = LocalDateTime + ZoneId

LocalDateTime ldt = LocalDateTime.now();			//Local date time

ZoneId zoneId = ZoneId.of( "Asia/Kolkata" ); 		//Zone information

ZonedDateTime zdtAtAsia = ldt.atZone( zoneId );		//Local time in Asia timezone

ZonedDateTime zdtAtET = zdtAtAsia
		.withZoneSameInstant( ZoneId.of( "America/New_York" ) ); //Sama time in ET timezone

Output:

2018-07-15T11:27:46.203763100+05:30[Asia/Kolkata]
2018-07-15T01:57:46.203763100-04:00[America/New_York]

2) ZonedDateTime to LocalDateTime

Use ZonedDateTime.toLocalDateTime() method to get local datetime without timezone information.

ZonedDateTime zdtAtET = ZonedDateTime.now(ZoneId.of("America/New_York"));
		
LocalDateTime ldt = zdtAtET.toLocalDateTime();

System.out.println(zdtAtET);
System.out.println(ldt);

Output:

2018-07-15T01:59:52.054839900-04:00[America/New_York]
2018-07-15T01:59:52.054839900

Happy Learning !!

References:

  • ZonedDateTime Javadoc
  • LocalDateTime Javadoc
  • ZoneId Javadoc

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Leave a Reply

This comment form is under antispam protection
This comment form is under antispam protection
  Subscribe  
Notify of

Search Tutorials

Java Date Time Tutorial

  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – DateTimeFormatter
  • Java 8 – Get Current Date
  • Java 8 – Get Current Timestamp
  • Java 8 – Compare Dates
  • Java 8 – Convert to EST Timezone
  • Java 8 – Elapsed Time
  • Java 8 – Add days to Date
  • Java 8 – Duration between dates
  • Java 8 – Period between dates
  • Java – Date
  • Java – Locale
  • Java – Get Current Locale
  • Java – Check leap year
  • Java – Parse String to Date
  • Java – Format Calendar
  • Java – 12 hours pattern
  • Java – Currency Formatting
  • Java – Timezone Conversion
  • Java – Strict Date Validation
  • XMLGregorianCalendar to Date

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz