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)
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");
}
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With