确定 java 中的当前时间是否超过预定时间 15 分钟 [英] determine if current time in java is past a predetermined time by 15mins

查看:78
本文介绍了确定 java 中的当前时间是否超过预定时间 15 分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确定当前时间何时等于定义的时间 + 15 分钟.

I would like to determine when the current time equals a defined time + 15mins.

这里定义的时间格式为:

The defined time here is in the format:

private Date fajr_begins;
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
fajr_begins = new Time(formatter.parse(prayerTimes.get(0)).getTime());

到目前为止我提出的代码不起作用(我知道下面的代码很糟糕

The code I have come up so far, which is not working is (the code below is crappy I know

DateTime today = new DateTime();
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");

now1 = new Time(formatter.parse(today));

Duration duration = new Duration(sunrise, now1);
System.out.println(" time to duha " + duration);

推荐答案

问题的上下文有点浅.你想使用一个线程,你想被提醒...?

The context of the question is a little light. Do you want to use a thread, do you want to be alerted...?

但是,作为一个基本示例,您可以执行以下操作...

However, as a basic example you could do something like...

// The time we want the alert...
String time = "16:00";
// The date String of now...
String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
    // The date + time to give us context
    Date timeAt = sdf.parse(date + " " + time);
    boolean rollOver = false;
    // Determine if the time has already passed, if it has
    // we need to roll the date to the next day...
    if (timeAt.before(new Date())) {
        rollOver = true;
    }
    // A Calendar with which we can manipulate the date/time
    Calendar cal = Calendar.getInstance();
    cal.setTime(timeAt);
    // Skip 15 minutes in advance
    cal.add(Calendar.MINUTE, 15);
    // Do we need to roll over the time...
    if (rollOver) {
        cal.add(Calendar.DATE, 1);
    }
    // The date the alert should be raised
    Date alertTime = cal.getTime();
    System.out.println("Raise alert at " + alertTime);
    // The timer with which we will wait for the alert...
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            System.out.println("duha");
        }
    }, alertTime);
} catch (ParseException ex) {
}

现在,在你抱怨 Date 之前,一切都是相对的.如果没有 Date 部分,我们很难知道何时应该发出警报.Date 只是帮助我们确定何时应该发出警报...

Now, before you complain about the Date, everything is relative. Without the Date part of the time, it's difficult to know when we should raise our alert. The Date just helps us pinpoint the when the alert should be raised...

附加

上下文就是一切,例如...如果我们使用以下...

Context is everything, for example...if we use the following...

String time = "16:00";
try {
    Date timeAt = new SimpleDateFormat("HH:mm").parse(time);
    System.out.println(timeAt);
} catch (ParseException ex) {
    ex.printStackTrace();
}

timeAt 值为 Thu Jan 01 16:00:00 EST 1970,这真的没用,时间总是在现在之前......

The timeAt value be Thu Jan 01 16:00:00 EST 1970, which is really useless, the time will always be before now...

如果相反,我们使用类似...

If, instead, we use something like...

String time = "16:00";
String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
try {
    Date timeAt = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(date + " " + time);
    System.out.println(timeAt);
} catch (ParseException ex) {
    ex.printStackTrace();
}

timeAt 现在将是 Thu Sep 05 16:00:00 EST 2013 这为我们提供了一些 now

The timeAt will now be Thu Sep 05 16:00:00 EST 2013 which gives us some context to now

现在如果我们使用 Calendar 将时间提前 15 分钟...

Now if we use Calendar to advance the time by 15 minutes...

Calendar cal = Calendar.getInstance();
cal.setTime(timeAt);
cal.add(Calendar.MINUTE, 15);
Date checkTime = cal.getTime();
System.out.println(checkTime);

checkTime 变为 Thu Sep 05 16:15:00 EST 2013.我使用 Calendar 因为它会自动为我滚动小时和日期,如果它需要......

The checkTime becomes Thu Sep 05 16:15:00 EST 2013. I use Calendar because it will automatically roll the hour and date for me should it need to be...

这现在允许我们开始使用默认的可用 API 功能.因为毫秒匹配的可能性很小,所以我很想做类似的事情......

This now allows us to start using the default available API functionality. Because it's highly unlikely that the milliseconds will ever match, I would be temtered to do something like...

Calendar watchFor = Calendar.getInstance();
watchFor.setTime(timeAt);
watchFor.set(Calendar.MILLISECOND, 0);
watchFor.set(Calendar.SECOND, 0);

Calendar now = Calendar.getInstance();
now.setTime(new Date());
now.set(Calendar.MILLISECOND, 0);
now.set(Calendar.SECOND, 0);

if (watchFor.equals(now)) {
    System.out.println("Go for it...");
}

将毫秒和秒归零,这样我就可以单独比较日期和时间 (HH:mm).

Zeroing out the milliseconds and seconds, so I can compare the Date and time (HH:mm) alone.

你当然也可以直接比较毫秒数...

You could of course compare the milliseconds directly as well...

这篇关于确定 java 中的当前时间是否超过预定时间 15 分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆