2

嗨,我正在尝试检查当前时间是否为 > ,例如 14:00。有谁知道如何做到这一点?

这就是我所拥有的:

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

不知何故,我想我现在需要创建一个 Date 对象并使用这个日期格式?

我知道当前时间是:

date= new Date();

并以上述格式返回dateFormat.format(date);

所以任何人都可以提供帮助 - 非常感谢。(我正在使用 Java 7 顺便说一句)

4

8 回答 8

9

试试下面的代码,它应该可以工作。

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");
}
于 2015-06-18T06:38:21.767 回答
7

使用公历希望这会有所帮助;)

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

}
于 2016-01-07T03:11:38.367 回答
0

使用Date.compareTo()

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

但请注意,java.util.Date 和所有相关类都有许多怪癖和细微的错误。使用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 ...
}
于 2013-08-12T12:17:47.583 回答
0

要检查一个日期是否早于另一个日期,您可以使用日历。

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) ) {
}
于 2013-08-12T12:19:08.183 回答
0

您可以使用 java.util 中的 Calendar 来实现这一点。希望这可以帮助。

于 2013-08-12T12:20:28.910 回答
0

尝试这样的事情

 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");
    }
于 2013-08-12T12:20:42.590 回答
0

检查时间是否大于当前时间

import java.util.*;
import java.time.*;
class one_part
{
public static void main(String []args)
{
LocalTime present=LocalTime.now();
String bf1="07:00:00";
String bf2="10:30:00";
LocalTime bf3 = LocalTime.parse(bf1);
LocalTime bf4 = LocalTime.parse(bf2);
String lunch1="11:30:00";
String lunch2="15:00:00";
LocalTime lunch3=LocalTime.parse(lunch1);
LocalTime lunch4=LocalTime.parse(lunch2);
String dinner1="19:30:00";
String dinner2="22:30:00";
LocalTime dinner3=LocalTime.parse(dinner1);
LocalTime dinner4=LocalTime.parse(dinner2);
if(present.isAfter(bf3) && present.isBefore(bf4))
{
System.out.println("this is breakfast time");
}
else if(present.isAfter(lunch3) && present.isBefore(lunch4))
{
    System.out.println("This is lunch time");
}
else if(present.isAfter(dinner3) && present.isBefore(dinner4))
{
    System.out.println("This is dinner time");
}
else
{
    System.out.println("Restaurent closed");
}
}
}
于 2020-03-30T07:08:47.057 回答
0

此函数检查字符串时间(12:00:10 > 12:00:00 等...)

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;
}
于 2017-03-22T13:04:00.090 回答