java检查时间更长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18186680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:06:35  来源:igfitidea点击:

java check time is greater time

javatimejava-7

提问by RookieCookie

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

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

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?

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

I know the current time is:

我知道当前时间是:

date= new Date();

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

在上述格式中,它将返回 dateFormat.format(date);

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

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

回答by Aaron Digulla

Use Date.compareTo():

使用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:

但请注意 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 ...
}

回答by Developer Marius ?il?nas

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) ) {
}

回答by Kevin Bhuva

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

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

回答by Ruchira Gayan Ranaweera

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

回答by K.D

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

回答by Tarik Chakur

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

}

回答by Ferhat KO?ER

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

此函数检查字符串时间(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;
}

回答by Chandu Bammidi

TRY THIS ONCE

试试这个

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