1

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?

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
nimrod
  • 151
  • 2
  • 11
  • You want localized week day. Tell more about what you really want to do. What do you mean by *want to set SUNDAY to '0', MONDAY to '1'*? Are you using Java Time? Joda? – Tunaki Apr 27 '16 at 13:24
  • What about `DayOfWeek.SUNDAY.getValue() % 7` ? – Arnaud Denoyelle Apr 27 '16 at 13:29
  • @Tunaki - As you said, I want to a localized week by defining 'new' values for the week's days. It's important for me that SUNDAY's value would be '0' and not '7'. this is going to serve me later on for some infrastructue I'm writing. I do want my function to get the DayOfWeek enum, and than defining new days values. – nimrod Apr 27 '16 at 13:30
  • 2
    But what do you _really_ want to do? And what are you using? Java Time? Joda? Setting SUNDAY to 0 doesn't mean anything. SUNDAY is SUNDAY. – Tunaki Apr 27 '16 at 13:31
  • @tunaki - I'm using java 'WeekFields', where the default based on the ISO-8601 standard that considers Monday to be the first day-of-week. It mentioned there that you can adjust to the correct day-of-week, the problem I'm not sure how... I know I can define my own enumeration, but I wanted using different way, with java built in Date pattern. – nimrod Apr 27 '16 at 20:25
  • Isn't [`WeekFields.SUNDAY_START`](https://docs.oracle.com/javase/8/docs/api/java/time/temporal/WeekFields.html#SUNDAY_START) what you're looking for then? – Tunaki Apr 27 '16 at 20:26

5 Answers5

3

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

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;}
}
Pete B.
  • 3,188
  • 6
  • 25
  • 38
0
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

Priyamal
  • 2,919
  • 2
  • 25
  • 52
0

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.

0

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();
    }
}
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52