检查两个时间之间的一个给定的时间处于日期不管

编程入门 行业动态 更新时间:2024-10-23 19:26:21
本文介绍了检查两个时间之间的一个给定的时间处于日期不管的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有时间跨度:

字符串时间1 = 01:00:00

String time1 = 01:00:00

字符串时间2 = 05:00:00

String time2 = 05:00:00

我要检查时间1 和时间2 的 20时11分13秒和14:49:00 。

其实, 01:00:00 大于 20点11分十三秒并小于 14:49:00 考虑 20点11分13秒总是比 14:49:00 。这是给prerequisite。

Actually, 01:00:00 is greater than 20:11:13 and less than 14:49:00 considering 20:11:13 is always less than 14:49:00. This is given prerequisite.

所以,我要的是, 20时11分13秒< 01:00:00< 14:49:00 。

所以,我需要这样的事情:

So I need something like that:

public void getTimeSpans() { boolean firstTime = false, secondTime = false; if(time1 > "20:11:13" && time1 < "14:49:00") { firstTime = true; } if(time2 > "20:11:13" && time2 < "14:49:00") { secondTime = true; } }

我知道,这code没有给出正确的结果,因为我比较字符串对象。

I know that this code does not give correct result as I am comparing the string objects.

如何做到这一点,因为他们的时间跨度,但不是要比较的字符串?

How to do that as they are the timespans but not the strings to compare?

推荐答案

您可以使用,以检查日历类。

You can use the Calendar class in order to check.

例如:

try { String string1 = "20:11:13"; Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1); Calendar calendar1 = Calendar.getInstance(); calendar1.setTime(time1); String string2 = "14:49:00"; Date time2 = new SimpleDateFormat("HH:mm:ss").parse(string2); Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(time2); calendar2.add(Calendar.DATE, 1); String someRandomTime = "01:00:00"; Date d = new SimpleDateFormat("HH:mm:ss").parse(someRandomTime); Calendar calendar3 = Calendar.getInstance(); calendar3.setTime(d); calendar3.add(Calendar.DATE, 1); Date x = calendar3.getTime(); if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) { //checkes whether the current time is between 14:49:00 and 20:11:13. System.out.println(true); } } catch (ParseException e) { e.printStackTrace(); }

更多推荐

检查两个时间之间的一个给定的时间处于日期不管

本文发布于:2023-10-23 22:44:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   两个   日期

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!