java check time is greater time

陌路散爱 提交于 2019-12-13 19:14:36

问题


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)


回答1:


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");
}



回答2:


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");
    } 

}



回答3:


Use Date.compareTo():

Date now = new Date();
Date date = dateFormat.parse(...);
if( now.compareTo( date) < 0 ) {
    ... date is in the future ...
}

But note that java.util.Date and all related classes have many quirks and subtle bugs. You will make your life much easier by using joda-time:

DateTime now = DateTime.now();
DateTime date = dateFormat.parse(...); // see http://joda-time.sourceforge.net/userguide.html#Input_and_Output
if( now.isBefore(date) ) {
    ... date is in the future ...
}



回答4:


You can use Calendar from java.util to achieve this. hope this helps.




回答5:


Try something like this

 SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
    Date date = new Date();        
    System.out.println(dateFormat.format(date));
    if(date.after(dateFormat.parse("14:00"))){
        System.out.println("Current time greater than 14.00");
    }



回答6:


To check if one date is earlier than another you could use Calendar.

Date yourDate                 = new Date();
Date yourDateToCompareAgainst = new Date();
Calendar calendar             = Calendar.getInstance();
calendar.setTime(yourDateToCompareAgainst);
calendar.set(HOUR, 14);

/** You can then check for your condition */
if( calendar.before(yourDate) ) {
}



回答7:


This Function check string times (12:00:10 > 12:00:00 etc...)

public static boolean biggerThanTime(String time1,String time2){
    String hhmmss1[] = time1.split(":");
    String hhmmss2[] = time2.split(":");
    for (int i = 0; i < hhmmss1.length; i++) {
        if(Integer.parseInt(hhmmss1[i])>Integer.parseInt(hhmmss2[i]))
            return true;
    }
    return false;
}


来源:https://stackoverflow.com/questions/18186680/java-check-time-is-greater-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!