4

I take two date time object in database. And I need to compare it without seconds.

Date 1 Value : 2018-05-25T13:55:24.000+05:30 Date 2 value : 2018-05-31T08:31:00.000+05:30

I need to check date 1 is after date 2.

if(Checks.checkNotNull(dateTime) && (partiallyInvalidateDate.isAfter(dateTime))){
    code = null;
  }

but I need to do above comparison without second part. Need some expert help to do it.

3
  • 4
    Use a LocalDateTime and use dateTime.withSeconds(0).withNano(0) to zero out those values. You could also use trunctaedTo(ChronoUnit.MINUTES) if that were easier Commented May 31, 2018 at 4:34
  • what is the datebase you use Commented May 31, 2018 at 4:35
  • @AnushkaEkanayake sql server 2017. Commented May 31, 2018 at 5:24

4 Answers 4

5

Assuming you're getting date times from java.sql.ResultSet as java.sql.Timestamp, you can set the seconds and nanos directly on those objects then compare with after (though you'll get a deprecated warning on setSeconds). Test with mock Timestamps:

import java.sql.Timestamp;

public class TestTimestamp {

     public static void main(String []args){
        Timestamp ts1 = Timestamp.valueOf("2011-12-25 11:12:13");
        Timestamp ts2 = Timestamp.valueOf("2011-12-25 11:12:14");

        ts1.setSeconds(0);
        ts1.setNanos(0);
        ts2.setSeconds(0);
        ts2.setNanos(0);

        System.out.println(ts1);
        System.out.println(ts2);
        //should print false since they're now identical
        System.out.println(ts2.after(ts1));
    }
}

If you need to use LocalDateTime, do this:

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class Test{

     public static void main(String []args){
        Timestamp ts1 = Timestamp.valueOf("2011-12-25 11:12:13");
        Timestamp ts2 = Timestamp.valueOf("2011-12-25 11:12:14");

        LocalDateTime dt1 = ts1.toLocalDateTime().truncatedTo(ChronoUnit.MINUTES);
        LocalDateTime dt2 = ts2.toLocalDateTime().truncatedTo(ChronoUnit.MINUTES);

        System.out.println(dt1);
        System.out.println(dt2);
        //should print false since they're now identical
        System.out.println(dt2.isAfter(dt1));
     }
}
Sign up to request clarification or add additional context in comments.

Comments

3

I consider MadProgrammer’s suggestion to use truncatedTo best:

    OffsetDateTime date1 = OffsetDateTime.parse("2018-05-25T13:55:24.000+05:30");
    OffsetDateTime date2 = OffsetDateTime.parse("2018-05-31T08:31:00.000+05:30");

    if (date1.truncatedTo(ChronoUnit.MINUTES).isAfter(date2.truncatedTo(ChronoUnit.MINUTES))) {
        System.out.println("Is after ignoring seconds and smaller");
    } else {
        System.out.println("Is on or before ignoring seconds and smaller");
    }

Output in this case:

Is on or before ignoring seconds and smaller

truncatedTo(ChronoUnit.MINUTES) sets seconds and smaller units (down to nanoseconds) to 0. As the date-time is immutable, a modified copy is returned. From the docs of the method:

Truncation returns a copy of the original date-time with fields smaller than the specified unit set to zero. For example, truncating with the minutes unit will set the second-of-minute and nano-of-second field to zero.

If you are using ZonedDateTime, no problem, it too has a truncatedTo method with the same effect, as have even more of the classes of java.time.

Comments

1

This is not so native than working with DateFormat or smth. like that, but this is the most simple way how to do this:

public static int compareUpToMinutes(long d1, long d2) {
    return Long.compare(d1 / 10000, d2 / 10000);
}

Examples:

final ZonedDateTime date = ZonedDateTime.now();
final Function<ZonedDateTime, Long> milli = d -> d.withSecond(10).toInstant().toEpochMilli();

compareUpToMinutes(milli.apply(date.withSecond(10)), milli.apply(date.withSecond(11))); //  0 => d1 = d2
compareUpToMinutes(milli.apply(date.withMinute(10)), milli.apply(date.withMinute(11))); // -1 => d1 < d2
compareUpToMinutes(milli.apply(date.withMinute(22)), milli.apply(date.withMinute(21))); //  1 => d1 > d2

Comments

0

Refer followings, hope you can get a solution with them.

SQL-Server 2008 and later: CAST(timestamp AS DATE)

SQL-Server 2005 and Earlier DATEADD(DAY, DATEDIFF(DAY, 0, timestamp), 0)

SQLite DATE(timestamp)

Oracle TRUNC(timestamp)

MySQL DATE(timestamp)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.