Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

comparing the two times is greater or lesser in java

Tags:

java

datetime

  Date date1= new java.util.Date();
                  java.sql.Date Sqldob = new java.sql.Date(date1.getTime());
                  System.out.println("date" +Sqldob);

                  Time Sqldob1 = new Time(date1.getTime());
                  System.out.println("User Time: " +Sqldob1);
                  String yourTime="09:30:00"; 

                  SimpleDateFormat ra = new SimpleDateFormat("HH:mm:ss");
                  Date yourDate = ra.parse(yourTime);
                  Time sqlTime3 = new Time(yourDate.getTime());
                  System.out.println("your time"+sqlTime3);

                  if(Sqldob1.before(sqlTime3)){
                     Sqldob1 = sqlTime3; 
                     System.out.println("inside loop");
                  }

In the code above I am comparing two time variables for equality, but it is giving me the same value -1 for all the types of input

like image 398
user2767354 Avatar asked Sep 27 '13 08:09

user2767354


3 Answers

You need to use the Date#before(Date),Date#after(Date) and Date#equals(Date) methods for basic date comparisons.

E.g:

Date d1 = new Date();
Date d2 = new Date();

if(d1.after(d2)){
    // Do something
}

if(d1.before(d2)){
    // Do something
}

if(d1.equals(d2)){
    // Do something
}

You can use the Date#compareTo(Date) method also, but then, you need to interpret the output of the compareTo method accordingly.

As the docs say:

The value 0 if the argument Date is equal to this Date; a value less than 0 if this Date is before the Date argument; and a value greater than 0 if this Date is after the Date argument.

In your case, you are getting -1 because

  1. new SimpleDateFormat("HHH:mm:ss"); is wrong. Should be new SimpleDateFormat("HH:mm:ss");

  2. int compare= sqlTime3.compareTo(Sqldob1); This sqlTime3 has only time in it. The date is the epoch date as you've not mentioned that, and hence, its always going to be before new Date() which is today.

Your solution:- (Hope this addresses your problem)

java.util.Date date1= new java.util.Date();

Time Sqldob1 = new Time(date1.getTime());
System.out.println("User Time: " +Sqldob1);

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 19); // Your hour
cal.set(Calendar.MINUTE, 30); // Your Mintue
cal.set(Calendar.SECOND, 00); // Your second

Time sqlTime3 = new Time(cal.getTime().getTime());
System.out.println("your time: "+sqlTime3);

if(Sqldob1.before(sqlTime3)){
   Sqldob1 = sqlTime3; 
   System.out.println("inside loop");
}
like image 144
Rahul Avatar answered Sep 19 '22 01:09

Rahul


You need to parse the date and call methods like before(), after() and equals() like this,

if(date.before(date1)){
    System.out.println(" date is before date1 ");
}
if(date.after(date1)){
    System.out.println(" date is after date1 ");
}

If both date and date1 are equal you can use equals method,

if(date.equals(date1)){
    System.out.println(" date and date1 are equal");
}
like image 30
Lucky Avatar answered Sep 21 '22 01:09

Lucky


Modern version:

    LocalDateTime dateTime1 = LocalDateTime.now(ZoneId.systemDefault());
    LocalDate dob = dateTime1.toLocalDate();
    System.out.println("date " + dob);

    LocalTime dob1 = dateTime1.toLocalTime();
    System.out.println("User Time: " + dob1);

    String yourTime = "09:30:00"; 
    LocalTime time3 = LocalTime.parse(yourTime);
    System.out.println("your time " + time3);

    if (dob1.isBefore(time3)) {
        dob1 = time3;
        System.out.println("inside if statement");
    }

When I ran this code this morning, it printed:

date 2017-07-07
User Time: 05:32:01.881
your time 09:30
inside if statement

The point is: With the old and now long outdated classes Date and Time it is easy not to get things right. With the modern classes I use here, it’s much easier to get them right.

Are you using java.sql types because you really need to get your date and time from a database and/or store them into one? This was what these were for, you shouldn’t really have used them for other purposes. I use “was” and “were” intentionally because you don’t need them for this purpose either anymore. With a new JDBC driver, you can get a LocalDateTime from the database and store one back, or depending on your column datatype get an Instant and convert it to LocalDateTime:

    LocalDateTime dateTime2 = instantFromDb.atZone(ZoneId.systemDefault())
            .toLocalDateTime();

PS Item 2. in SoduRahul’s answer gives the real and correct explanation of what went wrong in your program: though Time was meant for time-of-day only, your Sqldob1 ends up holding today’s date and sqlTime3 the date of the epoch (January 1, 1970), so the former will always be after the latter by their before method.

like image 44
Ole V.V. Avatar answered Sep 22 '22 01:09

Ole V.V.