如何在Java中比较星期几和时间 [英] How do you compare Day of week and Time in Java

查看:210
本文介绍了如何在Java中比较星期几和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何在Java中将星期五2:00 PM与星期四4:00 PM进行比较?

How do you compare "Friday 2:00 PM" against "Thursday 4:00 PM" in Java?

输入数组:
星期五2: 00 PM
星期四2:00 AM
星期六4:00 PM

Input array: Friday 2:00 PM Thursday 2:00 AM Saturday 4:00 PM

输出:
星期四2:00 AM
星期五2:00 PM
星期六4:00 PM

Output: Thursday 2:00 AM Friday 2:00 PM Saturday 4:00 PM

推荐答案

我的解决方案是建立一个小类来表示时代。我们可以将其称为 TimeOfWeek WeeklyRecurringEvent 或任何适合您的域的名称,请尝试明智地选择。该类将具有两个实例字段,工作日为 DayOfWeek ,时间为 LocalTime DayOfWeek LocalTime java.time 中(在Java 8,并反向移植到Java 6和7)。我的班级将有一个接受 String 参数的构造函数,例如星期五2:00 PM 并将其解析为两个实例字段。我相信相同的 DateTimeFormatter 可以用于两者,因此这可以是一个类常量(私有静态最终字段)。 DayOfWeek 没有 parse 方法,因此您需要类似 DayOfWeek.from( yourDateTimeFormatter.parse(yourString))

My solution would be to build a little class to represent the times. We could call it TimeOfWeek or WeeklyRecurringEvent or whatever fits your domain, please try to choose wisely. The class would have two instance fields, a DayOfWeek for the weekday and a LocalTime for the time. DayOfWeek and LocalTime are in java.time (introduced in Java 8 and backported to Java 6 and 7). My class would have a constructor accepting a String argument like "Friday 2:00 PM" and would parse it into the two instance fields. I believe the same DateTimeFormatter could be used for both, so this could be a class constant (private static final field). DayOfWeek does not have a parse method, so you would need something like DayOfWeek.from(yourDateTimeFormatter.parse(yourString)).

您的类应实现 Comparable ,因此您可以按照对象的自然顺序对其进行排序。在您的 compareTo 方法中,首先比较星期几,如果天数相同,则比较时间。 DayOfWeek 是从星期一到星期日自然排序的;如果您的一周从星期一开始的另一天开始,则需要特别处理。

Your class should implement Comparable so you can sort your objects by their natural order. In your compareTo method compare day of week first, then time if day is the same. DayOfWeekare sorted naturally from Monday to Sunday; if your week begins on another day than Monday, you will need to handle it specially.

您的课程也可能有 ZoneId 时区,但假设您所有的时间都在同一时区,则可能没有理由。

Your class could also have a ZoneId field for time zone, but assuming all your times are in the same time zone, there’s probably no reason.

快乐编码。

您可能还会看到是否可以找到适合您要求的课程;使用您的搜索引擎。我不知道一个,但你永远也说不清。

You may also see if you can find a class that fits your requirements somewhere out there; use your search engine. I don’t know of one, but you can never tell.

java.time 中的类非常通常可以满足有关日期和时间的有趣要求。您显然可以有一个没有时间的日期或没有日期的时间。一年中的某天可以没有年份(很适合纪念您的周年纪念日),而一周中的某天则可以说没有一周等等。不幸的是,没有一个课程可以完全满足您对星期几和时间的要求,这就是为什么我建议将它与现有的两个课程放在一起。

The classes in java.time are very often good for funny requirements about day and time. You can obviously have a date without a time or a time without a date. You can have a day of the year without a year (good for remembering your anniversary) and as mentioned a day of week without a week, and more. Unfortunately there isn’t a class to match your requirement of day of week and time exactly, which is why I suggest putting it together from two of the existing classes.

编辑:从您的Stack Overflow个人资料看来,您可能是一名Android程序员?如果要在Android上使用 java.time 类,请获取并使用 ThreeTenABP (这些类是在JSR-310中首次描述的,因此,这是用于JSR-310的ThreeTen和用于Android Backport的ABP)。

It seems from your Stack Overflow profile that you may be an Android programmer? If you want to use the java.time classes on Android, get and use the ThreeTenABP (the classes were first described in JSR-310, so that’s ThreeTen for JSR-310 and ABP for Android Backport).

这篇关于如何在Java中比较星期几和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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