本文介绍了在JAVA中创建一周中的几天的自定义集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想要这样的自定义日历:I want a custom calendar like this:enum TradingDays {Monday, Tuesday, Wednesday, Thursday, Friday};然后我需要遍历它,检查一个特定的枚举元素是否是TODAY的星期几。问题是,JAVA日历与我的日历中的星期几不匹配。所以:Then I need to iterate over it and check if a particular enum element is the day of week TODAY. The problem is that the JAVA calendar does not match to days of week from my calendar. So:Calendar now = Calendar.getInstance();TradingDays.Monday is not equal to any of now.get(Calendar.DAY_OF_WEEK);那么如何从我的日历TradingDays分配星期一,星期二等同一类型整数值)来自JAVA日历?So how do I assign Monday, Tuesday etc from my calendar TradingDays the same type (in this case an integer value) from the JAVA calendar? PS我需要有这样的日历TradingDays喜欢,因为它然后显示给用户,所以他/她选择交易的日子。P.S. I need to have that calendar TradingDays like that because it is then shown to the user so he/she chooses on which days to trade.推荐答案 tl; drtl;drSet<DayOfWeek> tradingDays = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.FRIDAY ) ; // Mon, Tue, Wed, Thu, & Fri.Boolean todayIsTradingDay = tradingDays.contains( LocalDate.now( ZoneId.of( "America/Montreal" ) ).getDayOfWeek() ) ; 详情 Java。DetailsThis functionality is built into Java. Java包括 java.time.DayOfWeek 枚举。Java includes the java.time.DayOfWeek enum.传递此枚举的对象,而不是整数1-7,以使您的代码更自我记录,提供类型安全,并确保一系列有效值。Pass around objects of this enum rather than mere integers 1-7 to make your code more self-documenting, provide type-safety, and ensure a range of valid values.invoices.printReportForDayOfWeek( DayOfWeek.MONDAY );周一至周日的编号为1-7,每 ISO 8601 标准。Numbering is 1-7 for Monday to Sunday, per ISO 8601 standard.通过调用 getDisplayName 。String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ; // Or Locale.US, etc. 星期对象集合 要跟踪一周中的多天,请使用 EnumSet (实现 设置 )或 EnumMap (实施 Map )。Collection of day-of-week objectsTo track multiple days of the week, use an EnumSet (implementation of Set) or EnumMap (implementation of Map).Set<DayOfWeek> weekend = EnumSet.of( DayOfWeek.SATURDAY , DayOfWeek.SUNDAY ) ;…Boolean todayIsWeekend = weekend.contains( LocalDate.now( ZoneId.of( "America/Montreal" ) ).getDayOfWeek() ) ;或者在这个问题的情况下,特别是一个工作日的集合。如果定义在应用程序执行期间没有改变,则可以定义为静态最终常数。Or in the case of this Question specifically, a collection of the weekdays. Perhaps define as a static final constant if the definition does not change during execution of the app.static final Set<DayOfWeek> tradingDays = EnumSet.of( DayOfWeek.MONDAY , DayOfWeek.TUESDAY , DayOfWeek.WEDNESDAY , DayOfWeek.THURSDAY , DayOfWeek.FRIDAY ) ;或者通过将EnumSet定义为按顺序定义的枚举对象范围,使它更短。指定MONDAY& FRIDAY并让 EnumSet 填写之间的值。使用 EnumSet .range 。Or make that even shorter by defining an EnumSet as a range of enum objects defined in a sequential order. Specify MONDAY & FRIDAY and let EnumSet fill in the values in between. Use EnumSet.range.static final Set<DayOfWeek> tradingDays = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.FRIDAY ) ; // Mon, Tue, Wed, Thu, & Fri.然后测试今天。请注意,时区对于确定当前日期至关重要。Then test for today. Note that a time zone is crucial in determining the current date. For any given moment the date varies around the globe by zone.ZoneId zoneId = ZoneId.of( "America/Montreal" ) ;LocalDate today = LocalDate.now( zoneId ) ;DayOfWeek todayDow = today.getDayOfWeek() ;Boolean todayIsTradingDay = tradingDays.contains( todayDow ).getDayOfWeek() ) ; 关于java.time java.time 框架内置于Java 8及以后。这些类取代了旧的麻烦的日期时间类,例如 java.util.Date , .Calendar java.text.SimpleDateFormat 。About java.timeThe java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat. Joda-Time 专案,现在位于维护模式中,建议您迁移到java.time。The Joda-Time project, now in maintenance mode, advises migration to java.time.要了解详情,请参阅 Oracle教程 。并且搜索Stack Overflow的很多例子和解释。To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.很多java.time功能都支持Java 6& 7在 ThreeTen-Backport 中,并进一步修改为 ThreeTenABP 中的org / wiki / Android_(operating_system) =nofollow> Android 。Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP. ThreeTen-Extra 专案使用附加类扩展java.time。这个项目是将来可能增加java.time的一个证明。您可以在这里找到一些有用的类,例如 Interval , YearWeek , YearQuarter 等等。The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more. 这篇关于在JAVA中创建一周中的几天的自定义集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-28 20:16