Create a custom collection of days of the week in JAVA
I need a custom calendar:
enum TradingDays {Monday, Tuesday, Wednesday, Thursday, Friday};
Then I need to iterate over it and check if a particular enum item is the day of the week TODAY. The problem is that the JAVA calendar doesn't match the days of the 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 schedule Monday, Tuesday, etc. from my TradingDays calendar of the same type (integer value in this case) from the JAVA calendar?
PS I need such a TradingDays calendar because it is displayed to the user, so he chooses which days to trade on.
source to share
You can try using a constructor inside your enum like this example:
public enum Currency {
PENNY(1),
NICKLE(5),
DIME(10),
QUARTER(25);
private final int value;
private Currency(int value) {
this.value=value;
}
}
During iteration, you can use coin.value like this:
for(Currency coin: Currency.values()){
System.out.println(coin+" "+coin.value);
if(coin.value==1){
System.out.println("THIS is the PENNY");
}
}
What result:
PENNY 1
THIS IS PENNY
NICKLE 5
DIME 10
QUARTER 25
source to share
enum
You can do many things with help that you are probably familiar with:
File TradingDays.java
:
public enum TradingDays {
Monday(Calendar.MONDAY),
Tuesday(Calendar.TUESDAY),
Wednesday(Calendar.WEDNESDAY),
Thursday(Calendar.THURSDAY),
Friday(Calendar.FRIDAY);
private int calendarValue;
TradingDays(int calendarValue) {
this.calendarValue = calendarValue;
}
public static TradingDays today() {
return fromCalendarValue(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
}
public static TradingDays fromCalendarValue(int calendarValue) {
for(TradingDays td : TradingDays.values()) {
if(td.calendarValue == calendarValue) {
return td;
}
}
throw new IllegalArgumentException(calendarValue + " is not a valid TradingDays calendarValue");
// or simply return null
}
};
source to share
Create a constructor inside your enum that takes an int value:
public class CalendarMain {
enum TradingDays {
Monday(2), Tuesday(3), Wednesday(4), Thursday(5), Friday(6);
private int value;
private TradingDays(int value) {
this.value = value;
}
};
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
if (now.get(Calendar.DAY_OF_WEEK) == TradingDays.Tuesday.value) {
// Chose Wednesday as the day to trade
System.out.println(now.get(Calendar.DAY_OF_WEEK));
System.out.println(TradingDays.Tuesday.value);
}
}
}
source to share
TL; DR
Set<DayOfWeek> tradingDays = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.FRIDAY ) ; // Mon, Tue, Wed, Thu, & Fri.
Boolean todayIsTradingDay = tradingDays.contains( LocalDate.now( ZoneId.of( "America/Montreal" ) ).getDayOfWeek() ) ;
More details
This functionality is built into Java.
java.time.DayOfWeek
enum
Java includes an java.time.DayOfWeek
enum.
Omit the objects of this enumeration, rather than just the integers 1-7, to make your code more self-documenting, type-safe, and enforceable.
invoices.printReportForDayOfWeek( DayOfWeek.MONDAY );
Numbering - 1-7 from Monday to Sunday, for the ISO 8601 standard .
Get the localized name of the day by calling getDisplayName
.
String output = DayOfWeek.MONDAY.getDisplayName( TextStyle.FULL , Locale.CANADA_FRENCH ) ; // Or Locale.US, etc.
Collection of objects of the week of the week
To track multiple days of the week, use EnumSet
(implementation Set
) or EnumMap
(implementation 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 weekdays. Possibly define as a static final constant, if the definition does not change at runtime.
static final Set<DayOfWeek> tradingDays = EnumSet.of( DayOfWeek.MONDAY , DayOfWeek.TUESDAY , DayOfWeek.WEDNESDAY , DayOfWeek.THURSDAY , DayOfWeek.FRIDAY ) ;
Or make it even shorter by specifying the EnumSet as a range of enumeration objects, defined in sequential order. Specify MONDAY and FRIDAY, and EnumSet
enter values ββin between. Use EnumSet.range
.
static final Set<DayOfWeek> tradingDays = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.FRIDAY ) ; // Mon, Tue, Wed, Thu, & Fri.
Then check for today. Note that the time zone is critical to determining the current date. For any given moment, the date changes around the world 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 nasty time classes like java.util.Date
, .Calendar
and java.text.SimpleDateFormat
.
The Joda-Time project , now in maintenance mode , advises switching to java.time.
To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations.
Most of the java.time functionality goes back to Java 6 and 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP .
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes, such as Interval
, YearWeek
, YearQuarter
, etc.
source to share