Java Tutorials


Java 8 Date Time Classes

ZonedDateTime class Usage

   ZonedDateTime represents LocalDate+LocalTime+ZoneId

Creating a ZonedDateTime Object.
Creating a ZonedDateTime object using LocalDateTime object using LocalDateTime's atZone method
		LocalDateTime ldt = LocalDateTime.now()
		ZonedDateTime zdt = ldt.atZone(ZoneId.of("America/New_York"))
      
Creating a ZonedDateTime object using Instant object using Instant's atZone method
		Instant ins = Instant.now()
		ZonedDateTime zdt = ins.atZone(ZoneId.of("America/New_York"))
      

Finding Difference Between two Zoned DateTime classes using Duration class

			ZonedDateTime zdtInd = ZonedDateTime.now(ZoneId.of("Asia/Kolkata"))
			ZonedDateTime zdtParis = ZonedDateTime.now(ZoneId.of("Europe/Paris"))
			ZonedDateTime zdtUS = ZonedDateTime.now(ZoneId.of("America/New_York"))

		India Time-zone comes with +5:30 where as Paris Time-Zone falls under +1:00 offset,
		so time difference is 4hrs and 30minuts
		
		Programatically calculating Time difference
		Find Time Difference between India and Paris City
		
		Duration d = Duration.between(zdtInd.toLocalDateTime(),zdtParis.toLocalDateTime())
		System.out.println(d);
		PT-4H-29M-52.388065S

		so Paris is 4hours and 30minutes behind Indian Standard Time.
		Note: Due to Day Light Saving settings, in Summer Paris uses
		 time offset +2:00 instead of +1:00, so this value(time difference) will change in summer months
		 
		
		India falls under same time zone, i.e +5:30, New York city time offset is -5:00
		Find Time Difference between India and New York City
		
		Duration d = Duration.between(zdtInd.toLocalDateTime(),zdtUS.toLocalDateTime())
		System.out.println(d);
		PT-10H-30M-32.727672S
		
		New York city 10hours 30 minutes behind Indian Standard Time
		Note: Due to Day Light Saving settings, in Summer New York uses
		 time offset -4:00 instead of -5:00, so this value(time difference) will change in summer months
		 

	

ADS