casper
casper

Reputation: 1401

Java's Calendar.DAY_OF_WEEK returns incorrect value

Today is Monday (should return 2), but returns 7 (Saturday). What am I missing?

int today = Calendar.DAY_OF_WEEK;
System.out.println(today);

Upvotes: 1

Views: 3383

Answers (4)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79005

java.time

The legacy date-time API (java.util date-time types and their formatting type, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Solution using the modern API:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        // To use JVM's timezone, simply remove the argument or use
        // ZoneId.systemDefault() as the argument
        LocalDate date = LocalDate.now(ZoneId.of("America/New_York"));

        // As enum
        DayOfWeek dw = date.getDayOfWeek();
        System.out.println(dw); // Value
        System.out.println(dw.ordinal()); // Ordinal

        // As String
        String dayName = dw.getDisplayName(TextStyle.FULL, Locale.ENGLISH);
        System.out.println(dayName);

        // As int
        int day = dw.getValue();
        System.out.println(day);
    }
}

Output:

SATURDAY
5
Saturday
6

Learn more about the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Upvotes: 1

Bibz
Bibz

Reputation: 321

Using Calendar.DAY_OF_WEEK will return the field number (in that case 7) used for the get method.

Upvotes: 0

Hirak
Hirak

Reputation: 3649

When you use

int today = Calendar.DAY_OF_WEEK;

This is what is returned to you...

   /**
     * Field number for <code>get</code> and <code>set</code> indicating the day
     * of the week.  This field takes values <code>SUNDAY</code>,
     * <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
     * <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>.
     *
     * @see #SUNDAY
     * @see #MONDAY
     * @see #TUESDAY
     * @see #WEDNESDAY
     * @see #THURSDAY
     * @see #FRIDAY
     * @see #SATURDAY
     */
    public final static int DAY_OF_WEEK = 7;

Upvotes: 1

user432
user432

Reputation: 3534

Instead of

int today = Calendar.DAY_OF_WEEK;

you have to use

Calendar cal = Calendar.getInstance();
int today = cal.get(Calendar.DAY_OF_WEEK);

to get the value.

With your way you only print the value of the constant Calendar.DAY_OF_WEEK and don't get the actual value for the day of your Calendar object.

Upvotes: 11

Related Questions