
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are various ways to compare time values in Java?
The LocalTime class represents the local time i.e. the time without time zone. This class provides various methods such as isBefore(), isAfter() and, isEqual() to compare two times.
Example
import java.time.LocalTime; public class Test { public static void main(String args[]) { LocalTime Time1 = LocalTime.of(10, 15, 45); LocalTime Time2 = LocalTime.of(07, 25, 55); Boolean bool1 = Time1.isAfter(Time2); Boolean bool2 = Time1.isBefore(Time2); if(bool1){ System.out.println(Time1+" is after "+Time2); }else if(bool2){ System.out.println(Time1+" is before "+Time2); }else{ System.out.println(Time1+" is equal to "+Time2); } } }
Output
10:15:45 is after 07:25:55
Example
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class CreateDateTime { public static void main(String args[])throws ParseException{ String timeStr1 = "8:27:45 AM"; String timeStr2 = "2:30:12 PM"; //Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:SS a"); Date dateTime1 = formatter.parse(timeStr1); Date dateTime2 = formatter.parse(timeStr2); Boolean bool1 = dateTime1.after(dateTime2); Boolean bool2 = dateTime1.before(dateTime2); Boolean bool3 = dateTime1.equals(dateTime2); if(bool1){ System.out.println(timeStr1+" is after "+timeStr2); }else if(bool2){ System.out.println(timeStr1+" is before "+timeStr2); }else if(bool3){ System.out.println(timeStr1+" is equla to "+timeStr2); } } }
Output
8:27:45 AM is after 2:30:12 PM
- Related Articles
- What are various ways to compare dates in Java?
- What are the various ways to save the environment from excessive plastic wastes?
- How to compare two dates along with time in Java?
- Various ways to define a variable in JavaScript
- Describe the various ways by which seeds are dispersed.
- What are the 6 ways to use this keyword in Java?
- What are time temporal fields in Java?
- What are the difference ways to replace nulls values in MySQL using SELECT statement?
- What are the various ways by which we protect crops from weeds and pests?
- How to compare time in different time zones in Python?
- What are the different ways to print an exception message in java?
- What are the different ways to iterate over an array in Java?
- In how many ways can I compare Strings in Java explain with example?
- What are the default values used by DB2 for various data types?
- Compare date time using after() method of Java Calendar

Advertisements