java自定义集合,在JAVA中创建一周中的几天的自定义集合

本文探讨如何使用Java 8的java.time框架创建定制交易日历,解决与实际周日历不符的问题。通过EnumSet和DayOfWeek枚举,实现Monday到Friday的范围,并检查当前是否为交易日。关键在于理解如何使用EnumSet和处理时区对日期的影响。
部署运行你感兴趣的模型镜像

I want a custom calendar like this:

enum TradingDays {Monday, Tuesday, Wednesday, Thursday, Friday};

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);

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?

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;dr

Set tradingDays = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.FRIDAY ) ; // Mon, Tue, Wed, Thu, & Fri.

Boolean todayIsTradingDay = tradingDays.contains( LocalDate.now( ZoneId.of( "America/Montreal" ) ).getDayOfWeek() ) ;

Details

This functionality is built into Java.

java.time.DayOfWeek enum

Java includes the java.time.DayOfWeek enum.

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 );

Numbering is 1-7 for Monday to Sunday, per ISO 8601 standard.

Get a localized name of the day by calling getDisplayName.

String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ; // Or Locale.US, etc.

Collection of day-of-week objects

To track multiple days of the week, use an EnumSet (implementation of Set) or EnumMap (implementation of Map).

Set 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 tradingDays = EnumSet.of( DayOfWeek.MONDAY , DayOfWeek.TUESDAY , DayOfWeek.WEDNESDAY , DayOfWeek.THURSDAY , DayOfWeek.FRIDAY ) ;

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 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() ) ;

About java.time

The 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.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.

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.

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值