java得到星期几不准确 [英] java get day of week is not accurate

查看:126
本文介绍了java得到星期几不准确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定一周中的哪一天是本月的第一天,但​​由于某种原因,我并没有让我在一周中正确的一天。

I am trying to determine what day of the week is the first day of the month but for some reason it is not returning me the correct day of the week.

下面是我的代码:

CalendarMonth[] months = CalendarUtils.constructMonthViewArray(new GregorianCalendar());


    public static CalendarMonth[] constructMonthViewArray(Calendar cal) {
        CalendarMonth[] months = new CalendarMonth[CALENDAR_GRID_SIZE];


        int year = cal.get(cal.YEAR);
        int month = cal.get(cal.MONTH);;
        // calculate how many days in the month
        int numOfDays = getNumOfDaysInMonth(cal);
        // calculate what day(mon-sunday) is the 1st of the month
        int firstDayOfMonth = getFirstDayOfMonth(cal);



private static int getFirstDayOfMonth(Calendar cal) {
        int firstDay = cal.get(Calendar.DAY_OF_WEEK);

        Log.d(TAG, "");

        // decrement it because our array deals with values 0-6(indexes)
        firstDay--;


        if (firstDay == 0) {
            firstDay = 6;
        } else {
            // decrement again so that the days start from 0.
            firstDay--;
        }
        return firstDay;
    }

int firstDay = cal.get(Calendar.DAY_OF_WEEK)的行; 没有给我一个星期的正确的一天,当月的第一个月在星期六(7)时,返回获得本月(2011年1月)的第一天的价值2。

The line from "int firstDay = cal.get(Calendar.DAY_OF_WEEK);" fails to give me the correct day of the week and returns the value 2 for getting the 1st day of this month(January 2011) when the first of the month was on a Saturday(7).

我错过了什么吗?我已经调试并检查了哪个月份,年份和日期的cal变量被设置,它确实表示今天的日期被更正,但是当我得到星期几没有得到值7。

Am I missing something? I have debugged and checked what month, year and date the cal variable is set and it indeed indicated today's date as corrected but when i get the day of week it doesn't get the value 7.

推荐答案

我无法重现您所看到的问题。正如迈克尔所说,有很多代码没有显示给我们,但Calendar.getDayOfWeek绝对有效:

I can't reproduce the problem you're seeing. As Michael says, there's a lot of code you haven't shown us, but Calendar.getDayOfWeek definitely works:

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Calendar calendar = new GregorianCalendar();
        calendar.set(2011, 0, 1); // 0 = January
        System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); // Prints 7
    }
}

你可能忘记了几个月0-based in java.util.Calendar

Did you maybe forget that months are 0-based in java.util.Calendar?

如果你可以生成一个类似的简短但完整的程序,显示

If you can produce a similar short but complete program which shows the wrong day of the week, please post it.

事实上,你减少了 firstDay getFirstDayOfMonth 中的两次似乎有点奇怪,以及它并没有真正反映该方法的名称(如Michael所述)。

The fact that you're decrementing firstDay twice within getFirstDayOfMonth seems somewhat odd, as well as the fact that it doesn't really reflect the name of the method (as Michael mentioned).

最后,我对Java日期/时间处理的不断推荐:如果你可以可能使用 Joda Time ,而不是 java.util.Calendar ,这样做。这是一个非常好的API。

Finally, my constant recommendation for Java date/time handling: if you can possibly use Joda Time instead of java.util.Calendar, do so. It's a much, much better API.

这篇关于java得到星期几不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆