如何知道,如果现在时间为两小时? [英] How to know if now time is between two hours?

查看:195
本文介绍了如何知道,如果现在时间为两小时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现在时间:

 新的Date();
 

和我有一些时刻常数,例如, 23 8 (这是晚上11点或23点,早上8点或08:00)。 我怎么能知道现在是时候与它的2小时常数?

它需要运行程序的某些code或不运行,如果现在时间为两小时,例如,如果已经是晚上不运行约code和趁其不一个上午。

下面的图像更好地解释:

http://i.stack.imgur.com/wPS09.png

在静音模式下不火的一些情况:

  13年6月20日00:00  -  23:00 13年6月20日// 23.00后,可以大声!

23:00 13年6月20日 -  15:00 13年6月20日// 15.00后可以大声!

01:00 13年6月20日 -  08:00 13年6月20日//后08.00可以大声!

21:00 13年6月20日 -  08:00 13年6月20日//后08.00可以大声!
 

解决方案

试试这个

  INT从= 2300;
    int值= 800;
    日期日期=新的日期();
    日历C = Calendar.getInstance();
    c.setTime(日期);
    INT T = c.get(Calendar.HOUR_OF_DAY)* 100 + c.get(Calendar.MINUTE);
    布尔isBetween =到大于从&安培;&安培; T> =从&安培;&安培; T< =到||到<从&安培;&安培; (T> = ||从T< =到);
 

I have a now time:

new Date();

And I have some hour constants, for example, 23 and 8 (it's 11pm or 23:00, 8am or 08:00). How I can know is now time between it's two hour constants?

It need to run some code of program or not to run if now time is between in two hours, for example, do not run some code if its already evening and while it is not a morning.

Here the image to better explain:

http://i.stack.imgur.com/wPS09.png

Some situations when silent mode does not fire:

00:00 20.06.13 - 23:00 20.06.13 // after 23.00 can loud!!

23:00 20.06.13 - 15:00 20.06.13 // after 15.00 can loud!!

01:00 20.06.13 - 08:00 20.06.13 // after 08.00 can loud!!

21:00 20.06.13 - 08:00 20.06.13 // after 08.00 can loud!!

解决方案

try this

    int from = 2300;
    int to = 800;
    Date date = new Date();
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    int t = c.get(Calendar.HOUR_OF_DAY) * 100 + c.get(Calendar.MINUTE);
    boolean isBetween = to > from && t >= from && t <= to || to < from && (t >= from || t <= to);

这篇关于如何知道,如果现在时间为两小时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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