Skip to content Skip to sidebar Skip to footer

Check Current Time Lies In Two Times In Java

I am trying to check whether current time is between Start Time and End time. But my method always return false: from = '5:10:00'; to = '10:10:00' private boolean isTimeBetweenTwo

Solution 1:

The problem is that when you do this

Date date_from = formatter.parse(from);
Date date_to = formatter.parse(to);

and the formatter is so defined:

SimpleDateFormatformatter=newSimpleDateFormat("H:mm:ss");

then is for the java api relevant only the time, but date objects hold more than time info, they hold too year, month, etc and those are getting initalize to a epoch UNIXTime

so the initial dates you are creating are

Thu Jan 01 05:10:00 CET 1970 and Thu Jan 01 10:10:00 CET 1970

so asking if today/right now (22th june 2016) is between those dates will NEVER return true...

on the other hand you conditions look inverted and as final tip you dont need SQL-Classes for the calculation, just with date objects you will get it pretty good

Example:

publicstaticvoidmain(String[] args)throws ParseException {
    Stringfrom="5:10:00";
    Stringto="10:10:00";
    Stringn="08:10:00";
    SimpleDateFormatformatter=newSimpleDateFormat("HH:mm:ss");
    Datedate_from= formatter.parse(from);
    Datedate_to= formatter.parse(to);
    DatedateNow= formatter.parse(n);
    if (date_from.before(dateNow) && date_to.after(dateNow)) {
        System.out.println("Yes time between");
    }
    }

Solution 2:

java.time

You are using old outmoded classes. They have been supplanted by the java.time classes built into Java 8 and later. Mush of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

LocalTime

The LocalTime class actually truly represents a time-of-day only value, unlike the java.sql.Time and java.util.Date classes seen in the Question.

LocalTimestart= LocalTime.of( 5 , 10 );
LocalTime stop = LocalTime.of( 10 , 0 );

Time zone

Determining the current time requires a time zone. For any given moment the time varies around the globe by time zone.

ZoneIdzoneId= ZoneId.of( "America/Montreal" );
LocalTimenow= LocalTime.now( zoneId );

Compare

Compare by calling equals, isAfter, or isBefore. We use the Half-open approach here as is common in date-time work where the beginning is inclusive while the ending is exclusive.

Boolean isNowInRange = ( ! now.isBefore( start ) ) && now.isBefore( stop ) ;

Solution 3:

Check this

Pass your dates to method

publicstaticintisValidDate(String start,String end){
        int valid=0;
        DateFormatformatter1=newSimpleDateFormat("MM/dd/yyyy HH:MM:SS");
        Dated1=null;
        Dated2=null;
        Dated3=null;
        CalendarcurDate= Calendar.getInstance();
        SimpleDateFormatdfcurDate=newSimpleDateFormat("MM/dd/yyyy HH:MM:SS");
        Stringdatecurrent= dfcurDate.format(curDate.getTime());
        try {
            d1 = formatter1.parse(datecurrent);
            d2 = newDate(Long.parseLong(end));
            d3 = newDate(Long.parseLong(start));
            System.out.println(d1.after(d3) && d1.before(d2));
            if (d1.after(d3) && d1.before(d2)) {
                valid=1;
           } } catch (Exception e) {
            e.printStackTrace();
        }
        return valid;
    }

if return 1--Current

Post a Comment for "Check Current Time Lies In Two Times In Java"