Listing Days in Java with Calendar

I would like to know if there is an existing enumeration containing all days of the week in Java. I use Calendar to process the logic of time, but I have not seen anything like it (I could create it myself, but I would rather use something existing). I also saw the threeten project , but it's still in alpha. Since an enumeration containing the days of the week is so trivial that it is shown by the sun in the enumeration documentation, I thought that something might already exist. Any ideas?

+6
source share
3 answers

The calendar class has:

 /** * Value of the {@link #DAY_OF_WEEK} field indicating * Sunday. */ public final static int SUNDAY = 1; /** * Value of the {@link #DAY_OF_WEEK} field indicating * Monday. */ public final static int MONDAY = 2; /** * Value of the {@link #DAY_OF_WEEK} field indicating * Tuesday. */ public final static int TUESDAY = 3; /** * Value of the {@link #DAY_OF_WEEK} field indicating * Wednesday. */ public final static int WEDNESDAY = 4; /** * Value of the {@link #DAY_OF_WEEK} field indicating * Thursday. */ public final static int THURSDAY = 5; /** * Value of the {@link #DAY_OF_WEEK} field indicating * Friday. */ public final static int FRIDAY = 6; /** * Value of the {@link #DAY_OF_WEEK} field indicating * Saturday. */ public final static int SATURDAY = 7; 

(source: java.util.Calendar)

Not a listing, because there was not a single calendar in JDK 1.1.

+2
source

Inconvenient old time classes, such as java.util.Calendar , are now deprecated, being superseded by java.time classes.

java.time.DayOfWeek enum

Java now includes a java.time.DayOfWeek enumeration.

Skip the objects of this enumeration, and not just the integers 1-7, to make your code more self-documenting, provide security by type, and provide a range of valid values.

 invoices.printReportForDayOfWeek( DayOfWeek.MONDAY ); 

Numbering is 1-7 from Monday to Sunday, according to ISO 8601.

Get the localized day name by calling getDisplayName .

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

To track several days of the week, use 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() ); 

About java.time

The java.time framework is built into Java 8 and later. These classes supersede old inconvenient time classes such as 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 for qaru for many examples and explanations.

Most java.time functions return to Java 6 and 7 in ThreeTen-Backport and are 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.

+2
source

Source: https://habr.com/ru/post/952086/


All Articles