Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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中日期的星期几?_Java_Date_Calendar - Fatal编程技术网

如何计算Java中日期的星期几?

如何计算Java中日期的星期几?,java,date,calendar,Java,Date,Calendar,如何用Java编写一个方法,当我以computeDayOfWeek(Month,date)格式键入日期时,该方法将告诉我一周中的哪一天?如果您正在使用Java。您可以使用Joda Time API来实现这一点: 检查此处的文档: 示例: DateTime dt = new DateTime() System.out.println(dt.dayOfWeek().getAsText()); 如果你正在寻找Java。您可以使用Joda Time API来实现这一点: 检查此处的文档: 示例: Da

如何用Java编写一个方法,当我以
computeDayOfWeek(Month,date)
格式键入日期时,该方法将告诉我一周中的哪一天?

如果您正在使用Java。您可以使用Joda Time API来实现这一点: 检查此处的文档:

示例

DateTime dt = new DateTime()
System.out.println(dt.dayOfWeek().getAsText());

如果你正在寻找Java。您可以使用Joda Time API来实现这一点: 检查此处的文档:

示例

DateTime dt = new DateTime()
System.out.println(dt.dayOfWeek().getAsText());

不需要外部库
java.util.Calendar
类处理这些内容

Calendar c = Calendar.getInstance();
c.setTime(put a java.util.Date instance here);
int dow = c.get(Calendar.DAY_OF_WEEK); //1=sunday, 2=monday, 3=Calendar.WEDNESDAY...

不需要外部库
java.util.Calendar
类处理这些内容

Calendar c = Calendar.getInstance();
c.setTime(put a java.util.Date instance here);
int dow = c.get(Calendar.DAY_OF_WEEK); //1=sunday, 2=monday, 3=Calendar.WEDNESDAY...

如果您需要自己计算,而不是使用预先制作的库函数,请查看


我在去年实施了这项计划,然后纠正了学生们提出的大约40个解决方案(其中相当一部分学生是第一个真正的解决方案),他们试图自己动手——这里有很多方法会犯错误。确保测试不同日期(1月、2月、其他月份、正常年份、闰年、2000年(是闰年)和1900年(不是闰年))。

如果需要自己计算,而不是使用预先制作的库函数,请查看


我在去年实施了这项计划,然后纠正了学生们提出的大约40个解决方案(其中相当一部分学生是第一个真正的解决方案),他们试图自己动手——这里有很多方法会犯错误。确保你测试不同的日期(1月、2月、其他月份、正常年份、闰年、2000年(是闰年)和1900年(不是闰年))。

大约10年前我写了一节有趣的小课。我只是为了好玩而做的,从来没有使用过,但是请随意使用和改进它(给我发邮件)。相当可怕的代码,因为我当时是初学者

package se.danero.calendar.util;

import java.lang.NullPointerException;

/**
 * This is a Calendar that is meant to be used as a utility. To come up with this class, I analyzed the gregorian calendar, and I searched the web for
 * useful, free algorithms. Then I put them together to create this version of of the Calendar.<BR>
 * <BR>
 * <B>The analyze</B><BR>
 * In my analyze of the gregorian calendar, I have come to the conclution that<BR>
 * there are 8 different types of year endings. These endings have a recurring<BR>
 * cycle of 28 years. The pattern repeats itself over 28 years, continiously.<BR>
 * <BR>
 * The pattern consists of 8 different types of year-endings. These are listed below <BR>
 * <BR>
 * <B>Type</B><BR>
 * 1 = 4 times over a period of 28 year period<BR>
 * 2 = 4 times over a period of 28 year period<BR>
 * 3 = 3 times ... <BR>
 * 4 = 4 times ... <BR>
 * 5 = 4 times ... <BR>
 * 6 = 4 times ... <BR>
 * 7 = 4 times ... <BR>
 * 8 = 1 time ... <BR>
 * <BR>
 * 
 * @author Robert Nordin, robert.nordin@bredband.net
 * @version 0.5
 * 
 */
public class Calendar {
    // ****************************************************************************
    // *** MEMBERS
    // ****************************************************************************
    // *** Type of year break; beginning with break 1980/1981 ***
    private final int types[] = { 1, 2, 3, 4, 5, 6, 1, 2, 4, 7, 5, 6, 2, 3, 4, 7, 6, 1, 2, 3, 7, 5, 6, 1, 8, 4, 7, 5 };

    // *** Week of the last week of the year and the first week of the new year ***
    private final int weeks[][] = { { 52, 1 }, { 52, 53 }, { 52, 1 }, { 52, 1 }, { 52, 1 }, { 52, 1 }, { 52, 1 }, { 52, 53 } };

    // Date of the last day of the last week and the last date of the first
    // week in the new year
    // private final int dates[][] = {{28, 4}, {27, 3}, { 2, 9}, { 1, 8},
    // {30, 6}, {29, 5}, {31, 7}, {26, 2}};

    private boolean initiated = false;
    private int year = 0;
    private int preMonths = 0;
    private int aftMonths = 0;

    // ****************************************************************************
    // *** CONSTRUCTORS
    // ****************************************************************************
    public Calendar() {
    }

    // ****************************************************************************
    public Calendar(int year) throws OutOfRangeException {
        if (!isYearValid(year)) {
            throw new OutOfRangeException(OutOfRangeException.TYPE_YEAR, "Year[" + year + "]");
        } else {
            this.year = year;
            initiated = true;
        }
    }

    // ****************************************************************************
    public Calendar(int year, int preMonths, int aftMonths) throws OutOfRangeException {
        if (!isYearValid(year)) {
            throw new OutOfRangeException(OutOfRangeException.TYPE_YEAR, "Year[" + year + "]");
        } else {
            this.year = year;
            initiated = true;
        }
        this.preMonths = ((preMonths >= 0) && (preMonths <= 12)) ? preMonths : 0;
        this.aftMonths = ((aftMonths >= 0) && (aftMonths <= 12)) ? aftMonths : 0;
    }

    // ****************************************************************************
    private boolean isYearValid(int year) {
        return ((year >= Date.MIN_YEAR) && (year <= Date.MAX_YEAR)) ? true : false;
    }

    // ****************************************************************************
    private int[] getWeeks(int type) {
        int retval[] = null;
        int arrayindex = type - 1;
        try {
            retval = weeks[arrayindex];
        } catch (Exception ex) {
            System.err.println("getWeeks(): Type is not inside valid range");
        }
        return retval;
    }

    // ****************************************************************************
    private int getType(int index) {
        int retval = types[index];
        if ((retval < 1) || (retval > 8)) {
            System.err.println("getType(): Index is not inside valid range");
        }
        return retval;
    }

    // ****************************************************************************
    private int getFirstWeek(int type) {
        int retval = 1;
        if (type == 3 || type == 4) {
            retval = 52;
        } else
            if (type == 2 || type == 8) {
                retval = 53;
            }
        return retval;
    }

    // ****************************************************************************
    private int getLastWeek(int type) {
        int retval = 52;
        if (type == 1 || type == 5 || type == 6) {
            retval = 1;
        } else
            if (type == 2 || type == 8) {
                retval = 53;
            }
        return retval;
    }

    // ****************************************************************************
    public String intToWeekday(int day, boolean longVersion) {
        String retval = "";
        /*
        if (day == 1) {
            retval = (longVersion) ? "Monday" : "Mon";
        } else
            if (day == 2) {
                retval = (longVersion) ? "Tuesday" : "Tue";
            } else
                if (day == 3) {
                    retval = (longVersion) ? "Wednesday" : "Wed";
                } else
                    if (day == 4) {
                        retval = (longVersion) ? "Thursday" : "Thu";
                    } else
                        if (day == 5) {
                            retval = (longVersion) ? "Friday" : "Fri";
                        } else
                            if (day == 6) {
                                retval = (longVersion) ? "Saturday" : "Sat";
                            } else
                                if (day == 7) {
                                    retval = (longVersion) ? "Sunday" : "Sun";
                                } else {
                                    System.err.println("intToWeekday(): Day is not inside valid range");
                                }
         */
        switch (day) {
        case 1: 
            retval = (longVersion) ? "Monday" : "Mon";
            break;
        case 2: 
            retval = (longVersion) ? "Tuesday" : "Tue";
            break;
        case 3: 
            retval = (longVersion) ? "Wednesday" : "Wed";
            break;
        case 4: 
            retval = (longVersion) ? "Thursday" : "Thu";
            break;
        case 5: 
            retval = (longVersion) ? "Friday" : "Fri";
            break;
        case 6: 
            retval = (longVersion) ? "Saturday" : "Sat";
            break;
        case 7: 
            retval = (longVersion) ? "Sunday" : "Sun";
            break;
        default: 
            System.err.println("intToWeekday(): Day is not inside valid range");
        }           
        return retval;
    }

    // ****************************************************************************
    private int getYearType(int year) {
        int retval = -1;
        if (year >= 1980) {
            retval = getType(((year - 1980) % 28));
        } else
            if (year == 1979) {
                retval = 5;
            } else {
                System.err.println("getYearType(): Year is not inside valid range");
            }
        return retval;
    }

    /**
     * Compute the sequential number of a day in the year (1-Jan=1, 31-Dec=365/366). The algorithm used: J.D. Robertson: Remark on Algorithm 398,
     * Comm. ACM 13, 10 (Oct. 1972), p. 918
     * 
     * @param date
     *            a date of the day you query about
     * @return returns an integer between 1 and 365/366. 366 if it is a leapyear.
     * @throw NullPointerException if the date is null.
     */
    public int getDayInYear(Date date) throws NullPointerException {
        if (date == null)
            throw new NullPointerException("Date cannot be null");
        int y = date.getYear();
        int m = date.getMonth();
        int d = date.getDay();
        int lm = (m + 10) / 13;
        return (int) (((long) 3055 * (long) (m + 2)) / 100 - 2 * lm - 91
                + (1 - (y - (y / 4) * 4 + 3) / 4 + (y - (y / 100) * 100 + 99) / 100 - (y - (y / 400) * 400 + 399) / 400) * lm + d);
    }

    /**
     * Returns an array of dates that conforms to a year and a week.
     * 
     * @param year
     *            the year in the form eg. 1980. Must be within Date.MIN_YEAR and Date.MAX_YEAR.
     * @param week
     *            the week. Must be less than 54
     * @return returns a full week of valid ISODates if inparameters are within the given range. returns a full week of null pointers if not.
     */
    public Date[] getWeekDates(int year, int week) {
        Date retval[] = new Date[7];
        if ((isYearValid(year)) && ((week > 0) && (week < 54))) {
            int dayCounter = 0;
            for (int i = 1; i < 13; i++) {
                for (int j = 1; j <= Date.getDaysInMonth(year, i); j++) {
                    Date tmp = new Date(year, i, j);

                    if (this.getWeekNumber(tmp) == week) {
                        retval[dayCounter] = tmp;
                        if (dayCounter == 7) {
                            i = 13;
                            j = 31;
                        }
                        if (dayCounter < 6) {
                            dayCounter++;
                        }
                    }
                }
            }
        }
        return retval;
    }

    /**
     * Returns the weeknumber of the given date.
     * 
     * @param date
     *            a date that you want to know in what week it is.
     * @return returns the weeknumber as an integer.
     * @throws NullPointerException
     *             if the parameter is null.
     */
    public int getWeekNumber(Date date) throws NullPointerException {
        int retval = -1;
        if (date == null)
            throw new NullPointerException("ISODate cannot be null");
        // *** Incoming data ***
        int year = date.getYear();
        int month = date.getMonth();
        int day = date.getDay();

        if (isYearValid(year)) {
            int targetDay = getDayInYear(date);
            int firstWeek = getFirstWeek(getYearType((year - 1)));
            int lastWeek = getLastWeek(getYearType(year));
            Date oneone = new Date(year, 1, 1);

            int firstDay = getDayInWeek(oneone);
            int dayCounter = 0;
            int weekCounter = firstWeek;
            int dayInWeek = firstDay;

            while (dayCounter != targetDay) {
                if (dayInWeek == 8) {
                    dayInWeek = 1;
                    if ((weekCounter > 51) && (dayCounter < 10)) {
                        weekCounter = 1;
                    } else
                        if ((dayCounter > 360) && (weekCounter > lastWeek)) {
                            weekCounter = lastWeek;
                        } else {
                            weekCounter++;
                        }
                }
                dayCounter++;
                dayInWeek++;
            }
            retval = weekCounter;
        }
        return retval;
    }

    /**
     * Returns an integer that tells you what day of the week the date specifies. The algorithm used is Zeller's congruence for the Gregorian
     * calendar.
     * 
     * @param date
     *            a date that points out the day you want to know the weekday of.
     * 
     * @return returns the weekday in the form of an integer. 1=Monday, 2=Tuesday...
     * @throws NullPointerException
     *             if the parameter is null.
     */
    public int getDayInWeek(Date date) throws NullPointerException {
        if (date == null)
            throw new NullPointerException("ISODate cannot be null");
        int year = date.getYear();
        int month = date.getMonth();
        int day = date.getDay();
        if (month < 3) {
            month += 12;
            year--;
        }
        return ((13 * month + 3) / 5 + day + year + year / 4 - year / 100 + year / 400) % 7 + 1;
    }
}
包se.danero.calendar.util;
导入java.lang.NullPointerException;
/**
*这是一个打算用作实用程序的日历。为了上这门课,我分析了公历,并在网上搜索了
*有用的免费算法。然后我将它们放在一起创建这个版本的日历。
*
*分析
*在我对公历的分析中,我得出的结论是
*有8种不同类型的年终。这些结尾具有重复性
*周期为28年。这种模式持续重复了28年。
*
*该模式包括8种不同类型的年终。下面列出了它们
*
*类型
*1=28年期间的4次
*2=28年期间的4次
*3=3次
*4=4次
*5=4次
*6=4次
*7=4次
*8=1次
*
* *@作者罗伯特·诺丁,罗伯特。nordin@bredband.net *@version 0.5 * */ 公课日历{ // **************************************************************************** //***成员 // **************************************************************************** //***年假类型;从1980/1981年的年假开始*** 私有final int类型[]={1,2,3,4,5,6,1,2,4,7,5,6,2,3,4,7,6,1,2,3,7,5,6,1,8,4,7,5}; //***一年中最后一周的一周和新年的第一周*** 私人最后整数周[][]={{52,1},{52,53},{52,1},{52,1},{52,1},{52,1},{52,1},{52,1},{52,53}; //上周最后一天的日期和第一周最后一天的日期 //新年的一周 //私人最终整数日期[][]={{28,4},{27,3},{2,9},{1,8}, // {30, 6}, {29, 5}, {31, 7}, {26, 2}}; 私有布尔值=false; 私家车整年=0; 私有整数前几个月=0; 私人月数=0; // **************************************************************************** //***施工人员 // **************************************************************************** 公历(){ } // **************************************************************************** 公共日历(整数年)抛出范围异常{ 如果(!isYearValid(年)){ 抛出新的OutOfRangeException(OutOfRangeException.TYPE_YEAR,“YEAR[“+YEAR+”]); }否则{ 今年=年; 启动=真; } } // **************************************************************************** 公历(整数年、整数前月、整数后月)抛出范围异常{ 如果(!isYearValid(年)){ 抛出新的OutOfRangeException(OutOfRangeException.TYPE_YEAR,“YEAR[“+YEAR+”]); }否则{ 今年=年; 启动=真; } this.preMonths=((preMonths>=0)和&(preMonths=0)和&(aftmounts=Date.minu YEAR)和&(YEAR 8)){ System.err.println(“getType():索引不在有效范围内”); } 返回返回; } // **************************************************************************** 私有整数getFirstWeek(整数类型){ int-retval=1; 如果(类型==3 | |类型==4){ retval=52; }否则 如果(类型==2 | |类型==8){ retval=53; } 返回返回; } // **************************************************************************** 私有整数getLastWeek(整数类型){ int-retval=52; 如果(类型==1 | |类型==5 | |类型==6){ retval=1; }否则 如果(类型==2 | |类型==8){ retval=53; } 返回返回; } // **************************************************************************** 公共字符串intToWeekday(int-day,布尔长版本){ 字符串retval=“”; /* 如果(天==1){ 电视转播