I have tried with the below code to compare two times:
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
Date inTime = sdf.parse("11:00");
Date outTime = sdf.parse("10:00");
if(inTime.compareTo(outTime) > 0){
Toast.makeText(this, "Out time should be greater than In time", Toast.LENGTH_SHORT).show();
}
Above code is working fine.
But if I give in time as 11:00 and out time as 12:00, I am getting above toast message.
I am getting above toast message if I give out time from 12:00 to 12:59. In other cases above code is working fine.
Where I am doing wrong. Please help me.
By Calendar Object:getTime(); Date date2 = calendar2. getTime(); Then compare both date using compareTo, before(date) or after(date).
Go to file name in project then press control then select Compare File and select another file you wish to compare. Seperate window will open up showing differences by colour contrast. Welcome to SO !
In order to compare time, we use the compareTo() method of the LocalTime class. The compareTo() method of the class compares two LocalTime objects.
Change SimpleDateFormat like below...
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Below are the patterns:
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
This will work....
Use java.time.
LocalTime
.parse( "12:00" )
.isAfter(
LocalTime.parse( "11:00" )
)
You have three problems:
Date
& SimpleDateFormat
) that were supplanted years ago by the modern java.time classes.hh:mm
should have been uppercase for 24-hour clock rather than 12-hour clock: HH:mm
. Use LocalTime
. This class represents a time-of-day without a date and without a time zone.
LocalTime start = LocalTime.parse( "11:00" ) ;
LocalTime stop = LocalTime.parse( "12:00" ) ;
You can compare.
boolean isStopAfterStart = stop.isAfter( start ) ;
Calculate the elapsed time as a Duration
.
Duration d = Duration.between( start , stop ) ;
d.toString(): PT1H
You can also ask the Duration
if it is negative, as another way to detect the stop time being before the start.
boolean isStopBeforeStart = d.isNegative() ;
Caveat: Working on time-of-day without the context of a date and a time zone can produce unrealistic results. That approach ignores the anomalies that occur in time zones such as Daylight Saving Time (DST) and other shifts to the wall-clock time used by the people of a particular region. An hour can repeat, or be skipped. A day can be 23, 23.5, 23.75, 25, or other number of hours long.
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With