简体   繁体   English

Java 的 Calendar.DAY_OF_WEEK 返回不正确的值

[英]Java's Calendar.DAY_OF_WEEK returns incorrect value

Today is Monday (should return 2), but returns 7 (Saturday).今天是星期一(应该返回 2),但返回 7(星期六)。 What am I missing?我错过了什么?

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

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.按照您的方式,您只能打印常量Calendar.DAY_OF_WEEK的值,而不会获得Calendar对象当天的实际值。

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;

java.time时间

The legacy date-time API ( java.util date-time types and their formatting type, SimpleDateFormat ) is outdated and error-prone.遗留日期时间 API( java.util日期时间类型及其格式类型SimpleDateFormat )已过时且容易出错。 It is recommended to stop using it completely and switch to java.time , the modern date-time API * .建议完全停止使用它并切换到java.time现代日期时间 API *

Solution using the modern API:使用现代 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 .Trail: Date Time 中了解有关现代日期时间 API * 的更多信息。


* 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 . * 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和您的 Android API 工作级别仍然不符合 Java-8,请检查 通过 desugaringHow to use ThreeTenABP in Android Project 可用的 Java 8+ APIs

使用Calendar.DAY_OF_WEEK将返回用于get方法的字段编号(在这种情况下为 7)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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