Sunday, March 30, 2014

Java 8 Date Time API Tutorial : LocalTime

This blogpost is a part of tutorial series on Date Time API introduced in Java 8. In this blogpost, I will go over some of the methods available in LocalTime class.

LocalTime is an immutable, thread-safe date-time object that represents time normally viewed as hour-minute-second. This can provide time with precision up to nano secs. And also this class deals with time only.

There are several ways to create a LocalTime object, some of them indicated below.


LocalTime lt = LocalTime.now(); // retrieves the time from the local system in 
// the default time zone.
System.out.println("The time now is :"+lt.toString());
LocalTime lt2mins = LocalTime.of(4, 34);// creates a time with the provided 
// hours and minutes values.
System.out.println("The time after setting hours and minutes is :"+lt2mins.toString());
LocalTime lt2secs = LocalTime.of(4, 34, 23); );// creates a time with the 
// provided hours, minutes and secs values.
System.out.println("The time after setting hours minutes and secs is :"+lt2secs.toString());
LocalTime lt2ns = LocalTime.of(4, 32, 34, 120); );// creates a time with 
// the provided hours, minutes, secs and nano seconds values.
System.out.println("The time after setting hours, minutes,secs and nano secs is :"+lt2ns.toString());

There are a couple of other methods available to create LocalTime object. LocalTime can be created by either passing in the number of nanoseconds or seconds.
             

LocalTime ltnd = LocalTime.ofNanoOfDay(120000);//Creates time equal to 120000 nano seconds.
System.out.println("The time after setting nanos of the day  is :"+ltnd.toString());
LocalTime ltsd = LocalTime.ofSecondOfDay(32100);//Creates time equal to 32100 seconds
System.out.println("The time after setting seconds of the day  is :"+ltsd.toString());
             
There are several getXXX methods which can return the field values for hours, minutes, seconds and nanoseconds.

 
int hour = lt.getHour();//returns the hour value from the time.
System.out.println("The hours of the time  is :"+hour);
int mins = lt.getMinute();//returns the minute value from the time.
System.out.println("The minutes of the time  is :"+mins);
int nanos = lt.getNano();//returns the nano seconds value from the time.
System.out.println("The nanos of the time  is :"+nanos);
int secs = lt.getSecond();//returns the seconds value from the time.
System.out.println("The secs of the time  is :"+secs);
The below methods convert the time object into secs or nanoseconds

 
long nanoOfDay = lt.toNanoOfDay();//converts the time into the number of nano seconds.
System.out.println("The nanos of the day  is :"+nanoOfDay);
int secOfDay = lt.toSecondOfDay();//converts time into seconds.
System.out.println("The secs of the day  is :"+secOfDay);
      
There are several plusXXX methods which can add hours/minutes/seconds/nanoseconds to the time.
             

LocalTime ltph = lt.plusHours(10);//Adds 10 hours to the existing time.
System.out.println("The time after adding hours  is :"+ltph.toString());
LocalTime ltpm = lt.plusMinutes(20);//Adds 20 minutes to the existing time.
System.out.println("The time after adding minutes  is :"+ltpm.toString());
LocalTime ltpn = lt.plusNanos(12000);//Adds 12000 nano seconds to the existing time object.
System.out.println("The time after adding nanos  is :"+ltpn.toString());
LocalTime ltps = lt.plusSeconds(120);//Adds 120 seconds to the time object.
System.out.println("The time after adding secs  is :"+ltps.toString());
             

Similar to plusXXX methods there are several minusXXX methods which can subtract hours/minutes/seconds/nanoseconds from the time.


LocalTime ltmh = lt.minusHours(21);//Subtract 21 hours to existing time.
System.out.println("The time after subtracting hours  is :"+ltmh.toString());
LocalTime ltmm = lt.minusMinutes(123); //Subtract 123 minutes to existing time.
System.out.println("The time after subtracting minutes  is :"+ltmm.toString());
LocalTime ltmn = lt.minusNanos(120000); //Subtract 120000 nanoseconds to existing time.
System.out.println("The time after subtracting nanos  is :"+ltmn.toString());
LocalTime ltms = lt.minusSeconds(120); //Subtract 120 seconds to existing time.
System.out.println("The time after subtracting secs  is :"+ltms.toString());
There are some methods to set particular values to the fields.


LocalTime lth = lt.withHour(4); //sets the hour of the day to 4. The values should be in the range 0-23.
System.out.println("The time after setting hours of the day  is :"+lth.toString());
LocalTime ltm = lt.withMinute(12); //sets the minute of the hours to 12. The valid values 0 - 59
System.out.println("The time after setting minutes of the hour  is :"+ltm.toString());
LocalTime ltn = lt.withNano(12000);//sets nano seconds to seconds. valid values 0 - 999999999
System.out.println("The time after setting nanos of the second  is :"+ltn.toString());
LocalTime lts = lt.withSecond(12); //sets the seconds of the minute to 12. The valid values 0 - 59
System.out.println("The time after setting seconds of the minute  is :"+lts.toString());         
I did not cover all the methods in the LocalTime class, but this should be a good starting point to understand the uses of LocalTime class.

No comments:

Post a Comment