Find Difference In Hour, Minute and Second In Two Different TimeZones in java 8




1)  Find Difference In Hour, Minute and Second in My And OtherTimeZone

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
/**
* Find Difference In Hour, Minute and Second in
* My And OtherTimeZone
*/
public class FindDifferenceInHourMinuteSecondIn_My_And_OtherTimeZone {
   public static void main(String[] args) {
       //Find time difference in two different timeZone
       LocalTime myLocalTime = LocalTime.now(); //It will pick my current timeZone by default  //It is = Asia/Kolkata
       LocalTime otherLocalTime = LocalTime.now(ZoneId.of("Europe/Paris")); //LocalTime of Europe/Paris
       //Display two different timeZone
       System.out.println("myLocalTime = "+myLocalTime);
       System.out.println("otherLocalTime = "+otherLocalTime);
       //Find which timeZone is before
       System.out.println("myLocalTime is before otherLocalTime = "+myLocalTime.isBefore(otherLocalTime));  // true
       System.out.println("\nNow, calculate difference between Two Different TimeZones (i.e. myLocalTime and otherLocalTime)  ");
      
       //Find difference in HOURS in between timeZones
       long hoursDifference = ChronoUnit.HOURS.between(myLocalTime, otherLocalTime);
       System.out.println("hoursDifference = "+hoursDifference);  //-4   // Because difference is -4:30 hrs (when daylight saving is OFF)
                                                                              //Output may be '-3'
                                                                              //Because difference is -3:30 hrs (when daylight saving is ON)
       //Find difference in MINUTES in between timeZones
       long minutesDifference = ChronoUnit.MINUTES.between(myLocalTime, otherLocalTime);
       System.out.println("minutesDifference = "+ minutesDifference); //-270   //Because -4:30 hrs = 270 minutes (when daylight saving is OFF)
                                                                       //-210   //Because -3:30 hrs = 210 minutes (when daylight saving is ON)
       //Find difference in SECONDS in between timeZones
       long secondsDifference = ChronoUnit.SECONDS.between(myLocalTime, otherLocalTime);
       System.out.println("secondsDifference = "+ secondsDifference); //-16200 //Because -4:30 hrs = 16200 seconds (when daylight saving is OFF)
                                                                       //-12600   //Because -3:30 hrs = 12600 seconds (when daylight saving is ON)
   }
}
/* output
myLocalTime = 16:57:50.700
otherLocalTime = 13:27:50.700
myLocalTime is before otherLocalTime = false
Now, calculate difference between Two Different TimeZones (i.e. myLocalTime and otherLocalTime)
hoursDifference = -3
minutesDifference = -210
secondsDifference = -12600
*/

2)  Find Difference In Hour, Minute and Second In Two Different TimeZones
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
/**
* Find Difference In Hour, Minute and Second In Two Different TimeZones
*/
public class FindDifferenceInHourMinuteSecondIn_TwoDifferentTimeZones {
   public static void main(String[] args) {
       //Find time difference in two different timeZone
       LocalTime localTime1 = LocalTime.now(ZoneId.of("Europe/Paris")); //LocalTime of Europe/Paris
       LocalTime localTime2 = LocalTime.now(ZoneId.of("Asia/Kolkata")); //LocalTime of Asia/Kolkata
       //Display two different timeZone
       System.out.println("localTime1 = "+localTime1);
       System.out.println("localTime2 = "+localTime2);
       //Find which timeZone is before
       System.out.println("localTime1 is before localTime2 = "+localTime1.isBefore(localTime2));  // true
       System.out.println("\nNow, calculate difference between Two Different TimeZones (i.e. localTime1 and localTime12)  ");
      
       //Find difference in HOURS in between timeZones
       long hoursDifference = ChronoUnit.HOURS.between(localTime1, localTime2);
       System.out.println("hoursDifference = "+hoursDifference);  //4   // Because actual difference is 4:30 hrs (when daylight saving is OFF)
                                                               //Output may be '3'
                                                                 //Because difference is 3:30 hrs (when daylight saving is ON)
       //Find difference in MINUTES in between timeZones
       long minutesDifference = ChronoUnit.MINUTES.between(localTime1, localTime2);
       System.out.println("minutesDifference = "+minutesDifference); //270   //Because 4:30 hrs = 270 minutes (when daylight saving is OFF)
                                                                     //210   //Because 3:30 hrs = 210 minutes (when daylight saving is ON)
       //Find difference in SECONDS in between timeZones
       long secondsDifference = ChronoUnit.SECONDS.between(localTime1, localTime2);
       System.out.println("secondsDifference = "+secondsDifference); //16200 //Because 4:30 hrs = 16200 seconds (when daylight saving is OFF)
                                                                      //12600   //Because 3:30 hrs = 12600 seconds (when daylight saving is ON)
   }
}
/* output
localTime1 = 13:26:08.320
localTime2 = 16:56:08.336
localTime1 is before localTime2 = true
Now, calculate difference between Two Different TimeZones (i.e. localTime1 and localTime12)
hoursDifference = 3
minutesDifference = 210
secondsDifference = 12600
*/


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.





eEdit
Must read for you :