简体   繁体   中英

How to set java DayOfWeek values?

I want to create a function that gets an enum of DayOfWeek and then use this day's value to something else.

My problem is that I can't set (actually overriding) the returned values.

For example:

DayOfWeek.SUNDAY.getValue(); // return 7

I want to set SUNDAY to 0 , MONDAY to 1 and so on... Is there any way doing it?

java.time.DayOfWeek

Java 8 and later has such an enum built-in, java.time.DayOfWeek .

Per the ISO 8601 standard, the week is defined as Monday-Sunday, numbered 1-7.

int dayNumber = DayOfWeek.MONDAY.getValue() ;  // 1 = Monday, 7 = Sunday.

Convert from ISO 8601 style to US style

If you want a week as commonly used in the United States, with Sunday being first, use math. Calculate modulo 7 then plus 1.

( DayOfWeek.SUNDAY.getValue() % 7 ) + 1

Like this:

System.out.println( ( DayOfWeek.SUNDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.MONDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.TUESDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.WEDNESDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.THURSDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.FRIDAY.getValue() % 7 ) + 1 ) ;
System.out.println( ( DayOfWeek.SATURDAY.getValue() % 7 ) + 1 ) ;

1

2

3

4

5

6

7

You question is contradictory and unclear, so I am not sure of your goal if you Sunday-Saturday being 0-6, then use above math but do not add one.

( DayOfWeek.SUNDAY.getValue() % 7 )  // Sunday = 0, Monday = 1 , … , Saturday = 6.

java.util.Calendar constants

While I would never recommend using the legacy Date / Calendar classes, but for your information, the java.util.Calendar class defines a series of int constants ( not an enum) for days of the week where Calendar.SUNDAY is 1 and so on to Calendar.SATURDAY is 7 .


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .

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

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

Where to obtain the java.time classes?

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 .

Simply define your own enumeration with a member of type int, a constructor that assigns that int, and accessor:

public enum MyDayOfWeek {

  SUNDAY(0), MONDAY(1)....;
  private int id;
  MyDayOfWeek(int id) {
    this.id = id;
  }

  public int getId() { return id;}
}
public enum DayOfWeek{
    SUNDAY(0),
    MONDAY(1),
    TUESDAY(2);
}

DayofWeek day = Status.SUNDAY; System.out.println(day.name());

try arranging your enum to the order. this might help you

You can write something like this:

public static int getDayOrderNumber(DayOfWeek dow)
{
    EnumMap<DayOfWeek, Integer> days = new EnumMap(DayOfWeek.class){{
        put(DayOfWeek.SUNDAY, 0);
        put(DayOfWeek.MONDAY, 1);
        ...
    }};
    return days.get(dow);
}

Only do some refactoring, initialize map in more appropriate place.

Here is a simple method I wrote and I use it a lot:

public static int getDayOfWeekBySundayIs0(DayOfWeek day)
{
    if(day == DayOfWeek.SUNDAY)
    {
        return 0;
    }
    else
    {
        // NOTE: getValue() is starting to count from 1, and not from 0
        return  day.getValue();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM