Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

compare two times in java

String startTime = "10:00";
    String endTime = "12:00";
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    Date d1 = sdf.parse(startTime);
    Date d2 = sdf.parse(endTime);
    long elapsed = d2.getTime() - d1.getTime(); 
    System.out.println(elapsed);
Comment

java compare dates

Date has before and after methods and can be compared to each other as follows:

if(todayDate.after(historyDate) && todayDate.before(futureDate)) {
    // In between
}
For an inclusive comparison:

if(!historyDate.after(todayDate) && !futureDate.before(todayDate)) {
    /* historyDate <= todayDate <= futureDate */ 
}
You could also give Joda-Time a go, but note that:

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time (JSR-310).

Back-ports are available for Java 6 and 7 as well as Android.
Comment

compare time in java

for ( Map.Entry<DayOfWeek , LocalTime> entry : dayToTimeMap.entrySet () ) {
    DayOfWeek key = entry.getKey ();
    LocalTime value = entry.getValue ();
    int comparison = key.compareTo ( today );
    if ( comparison < 0 ) { // if earlier day…
        earlier.add ( key );
    } else if ( comparison == 0 ) { //If same day…
        if ( value.isBefore ( now ) ) {
            earlier.add ( key );
        } else {  // Else same time as now or later than now…
            later.add ( key );
        }
    } else if ( comparison > 0 ) {
        later.add ( key );
    } else {
        throw new RuntimeException ( "Unexpectedly reached IF-ELSE for comparison: " + comparison );
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: java max array 
Java :: java nested for loop 
Java :: object class java 
Java :: Caused by: java.lang.ClassNotFoundException: 
Java :: Duplicate class com.google.android.gms.internal.firebase_messaging.zzo found in modules jetified-firebase-iid 
Java :: throw keyword in java 
Java :: autowired in spring 
Java :: java break statement 
Java :: java lambda 
Java :: convert long to localdatetime java 
Java :: multiple string java 
Java :: final method java 
Java :: how to get last index of array in java 
Java :: compare two strings java 
Java :: java, how to find the most repeated character 
Java :: how to stop screen going off android studio 
Java :: ArrayIndexOutOfBoundsException 
Java :: $950 at 6% per annum for three years. 
Java :: matlab leslie eigenvalue java 
Sql :: magento 2 order delete from db 
Sql :: select not matching data from two tables 
Sql :: Port 5432 is already in use Usually this means that there is already a PostgreSQL server running on your Mac. If you want to run multiple servers simultaneously, use different ports. 
Sql :: SQL order random 
Sql :: mysql workbench in ubuntu 14.04 
Sql :: postgresql db owner change 
Sql :: mysql grant all privileges to user from any host 
Sql :: sql add days to date 
Sql :: ubuntu stop mysql 
Sql :: Find all tables containing column with specified name - MS SQL Server 
Sql :: dump mysql 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =