Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java';s Calendar.DAY\u OF\u WEEK返回不正确的值_Java_Calendar - Fatal编程技术网

Java';s Calendar.DAY\u OF\u WEEK返回不正确的值

Java';s Calendar.DAY\u OF\u WEEK返回不正确的值,java,calendar,Java,Calendar,今天是星期一(应返回2),但返回7(星期六)。我错过了什么 int today = Calendar.DAY_OF_WEEK; System.out.println(today); 而不是 int today = Calendar.DAY_OF_WEEK; 你必须使用 Calendar cal = Calendar.getInstance(); int today = cal.get(Calendar.DAY_OF_WEEK); 以获取值 按照您的方式,您只打印常量日历的值。周中的天,而不

今天是星期一(应返回2),但返回7(星期六)。我错过了什么

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

int today = Calendar.DAY_OF_WEEK;
你必须使用

Calendar cal = Calendar.getInstance();
int today = cal.get(Calendar.DAY_OF_WEEK);
以获取值

按照您的方式,您只打印常量
日历的值。周中的天
,而不获取
日历
对象当天的实际值。

而不是

int today = Calendar.DAY_OF_WEEK;
你必须使用

Calendar cal = Calendar.getInstance();
int today = cal.get(Calendar.DAY_OF_WEEK);
以获取值

按照您的方式,您只打印常量
日历的值。周中的天
,而不获取
日历
对象当天的实际值。

当您使用

int today = Calendar.DAY_OF_WEEK;
这是归还给你的东西

   /**
     * 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;
当你使用

int today = Calendar.DAY_OF_WEEK;
这是归还给你的东西

   /**
     * 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;

使用
Calendar.DAY\u OF_WEEK
将返回用于
get
方法的字段号(在这种情况下为7)

使用
Calendar.DAY\u OF_WEEK
将返回用于
get
方法的字段号(在这种情况下为7)

java.time 旧式日期时间API(
java.util
date-time类型及其格式类型,
SimpleDateFormat
)已过时且容易出错。建议完全停止使用它,并切换到
java.time
,即*

使用现代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);
    }
}
SATURDAY
5
Saturday
6
输出:

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);
    }
}
SATURDAY
5
Saturday
6
了解有关*的更多信息


*无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。

java.time 旧式日期时间API(
java.util
date-time类型及其格式类型,
SimpleDateFormat
)已过时且容易出错。建议完全停止使用它,并切换到
java.time
,即*

使用现代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);
    }
}
SATURDAY
5
Saturday
6
输出:

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);
    }
}
SATURDAY
5
Saturday
6
了解有关*的更多信息


*无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。

+1。我想知道为什么该方法的契约是接受
int
而不是接受来自enum的值。啊,就是这样-忘记了cal.get部分!谢谢@Calendar类来自JDK1.1——当时没有枚举类型,类型安全的枚举模式也没有广泛传播。请注意,JavaSE8引入了新的Java.time包。@Puce,谢谢。很高兴知道这一点!我真的怀疑这样的事情,但没有检查它。:)+1.我想知道为什么该方法的契约是接受
int
而不是接受来自enum的值。啊,就是这样-忘记了cal.get部分!谢谢@Calendar类来自JDK1.1——当时没有枚举类型,类型安全的枚举模式也没有广泛传播。请注意,JavaSE8引入了新的Java.time包。@Puce,谢谢。很高兴知道这一点!我真的怀疑这样的事情,但没有检查它。:)