Thursday, 18 January 2018

How to use compareTo and equals methods of Java 8 LocalTime Class | Java 8 Date and Time


Click here to watch on Youtube : 
https://www.youtube.com/watch?v=JHmM5HMJ7uM&list=UUhwKlOVR041tngjerWxVccw

LocalTimeDemo1.java

import java.time.LocalTime;

public class LocalTimeDemo1
{

    public static void main(String[] args)
    {
        LocalTime time1 = LocalTime.parse("05:30:30");
        System.out.println(time1);

        LocalTime time2 = LocalTime.parse("06:35:30");
        System.out.println(time2);

        /*
         * Parameters:
         *
         * other - the other time to compare to, not null
         *
         * Returns:the comparator value, negative if less, positive if
         * greater
         */


        int value = time1.compareTo(time2);

        System.out.println(value);
    }

}

Output

05:30:30
06:35:30
-1

LocalTimeDemo2.java

import java.time.LocalTime;

public class LocalTimeDemo2
{

    public static void main(String[] args)
    {
        LocalTime time1 = LocalTime.parse("05:30:30");
        System.out.println(time1);

        LocalTime time2 = LocalTime.parse("05:30:30");
        System.out.println(time2);

        /*
         * Parameters:
         *
         * obj - the object to check, null returns false
         *
         * Returns:true if this is equal to the other time
         */


        boolean value = time1.equals(time2);

        System.out.println(value);
    }

}

Output

05:30:30
05:30:30
true

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/LocalTimeDemo_compareTo_equals.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/LocalTimeDemo_compareTo_equals

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/0f51b390145d58b657670d4890d43cef82a5202a/BasicJava/LocalTimeDemo_compareTo_equals/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment