Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java check time is greater time

Tags:

java

time

java-7

Hi I'm trying to check if the currentime is > , say, 14:00. Does anyone know how to do this?

This is what I have:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm");

Somehow I guess I need to now create a Date object and use this dateformat?

I know the current time is:

date= new Date();

and in the above format it would be return dateFormat.format(date);

So can anyone help - much appreciated. (am using Java 7 btw)

like image 955
RookieCookie Avatar asked Dec 30 '25 17:12

RookieCookie


2 Answers

Try below code, it should work.

Date date = new Date() ;
SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm") ;
dateFormat.format(date);
System.out.println(dateFormat.format(date));

if(dateFormat.parse(dateFormat.format(date)).after(dateFormat.parse("12:07")))
{
    System.out.println("Current time is greater than 12.07");
}else{
    System.out.println("Current time is less than 12.07");
}
like image 104
K.D Avatar answered Jan 01 '26 06:01

K.D


Using Gregorian Calendar hope this helps ;)

public Class Time {

    public static void main() {

        Calendar C = new GregorianCalendar();
        int hour = C.get( Calendar.HOUR_OF_DAY );
        int minute = C.get( Calendar.MINUTE );

        if( hour == 14 && minute > 0 )
            System.out.println("time > 14");
    } 

}
like image 23
Tarik Chakur Avatar answered Jan 01 '26 08:01

Tarik Chakur



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!