⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 week.java

📁 大家打开看看啊, 很有用的东西
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        Calendar day1FirstWeek = (Calendar) calendar.clone();
        day1FirstWeek.set(year.getYear(), Calendar.JANUARY, 1);
        int dayOfWeek = day1FirstWeek.get(Calendar.DAY_OF_WEEK);
        day1FirstWeek.add(Calendar.DAY_OF_YEAR, (1 - dayOfWeek));

        int day = day1FirstWeek.get(Calendar.DAY_OF_MONTH);
        int month = day1FirstWeek.get(Calendar.MONTH) + 1;
        int thisYear = day1FirstWeek.get(Calendar.YEAR);

        SerialDate jan1 = SerialDate.createInstance(day, month, thisYear);
        SerialDate startOfWeek = SerialDate.addDays((this.week - 1) * 7, jan1);
        Day first = new Day(startOfWeek);
        result = first.getFirstMillisecond(calendar);

        return result;

    }

    /**
     * Returns the last millisecond of the week, evaluated using the supplied
     * calendar (which determines the time zone).
     *
     * @param calendar  the calendar.
     *
     * @return the last millisecond of the week.
     */
    public long getLastMillisecond(Calendar calendar) {

        Calendar cal = (Calendar) calendar.clone();
        cal.set(year.getYear(), Calendar.JANUARY, 1);
        int actualMaxWeek = cal.getActualMaximum(Calendar.WEEK_OF_YEAR);

        if (this.week == actualMaxWeek) {
            return this.year.getLastMillisecond(calendar);
        }
        else {
            Calendar firstDayOfFirstWeek = Calendar.getInstance();
            firstDayOfFirstWeek.set(year.getYear(), Calendar.JANUARY, 1);
            int dayOfWeek = firstDayOfFirstWeek.get(Calendar.DAY_OF_WEEK);
            firstDayOfFirstWeek.add(Calendar.DAY_OF_YEAR, (1 - dayOfWeek));

            int day = firstDayOfFirstWeek.get(Calendar.DAY_OF_MONTH);
            int month = firstDayOfFirstWeek.get(Calendar.MONTH) + 1;
            int thisYear = firstDayOfFirstWeek.get(Calendar.YEAR);

            SerialDate jan1 = SerialDate.createInstance(day, month, thisYear);
            SerialDate endOfWeek = SerialDate.addDays((this.week) * 7 - 1, jan1);
            Day last = new Day(endOfWeek);

           // Date date = new Date(last.getLastMillisecond(calendar));

            return last.getLastMillisecond(calendar);
        }

    }

    /**
     * Returns a string representing the week (e.g. "Week 9, 2002").
     * <P>
     * To do: look at internationalisation.
     *
     * @return A string representing the week.
     */
    public String toString() {
        return "Week " + week + ", " + year;
    }

    /**
     * Tests the equality of this Week object to an arbitrary object.  Returns
     * true if the target is a Week instance representing the same week as this
     * object.  In all other cases, returns false.
     * @param obj The object.
     *
     * @return <code>true</code> if week and year of this and object are the same.
     */
    public boolean equals(Object obj) {

        if (obj != null) {
            if (obj instanceof Week) {
                Week target = (Week) obj;
                return ((week == target.getWeek()) && (year.equals(target.getYear())));
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }

    }

    /**
     * Returns an integer indicating the order of this Week object relative to
     * the specified object:
     *
     * negative == before, zero == same, positive == after.
     *
     * @param o1  the object to compare.
     *
     * @return negative == before, zero == same, positive == after.
     */
    public int compareTo(Object o1) {

        int result;

        // CASE 1 : Comparing to another Week object
        // --------------------------------------------
        if (o1 instanceof Week) {
            Week w = (Week) o1;
            result = this.year.getYear() - w.getYear().getYear();
            if (result == 0) {
                result = this.week - w.getWeek();
            }
        }

        // CASE 2 : Comparing to another TimePeriod object
        // -----------------------------------------------
        else if (o1 instanceof RegularTimePeriod) {
            // more difficult case - evaluate later...
            result = 0;
        }

        // CASE 3 : Comparing to a non-TimePeriod object
        // ---------------------------------------------
        else {
            // consider time periods to be ordered after general objects
            result = 1;
        }

        return result;

    }

    /**
     * Parses the string argument as a week.
     * <P>
     * This method is required to accept the format "YYYY-Wnn".  It will also
     * accept "Wnn-YYYY". Anything else, at the moment, is a bonus.
     *
     * @param s  string to parse.
     *
     * @return <code>null</code> if the string is not parseable, the week otherwise.
     *
     * @throws TimePeriodFormatException if there is a problem parsing the string.
     */
    public static Week parseWeek(String s) throws TimePeriodFormatException {

        Week result = null;
        if (s != null) {

            // trim whitespace from either end of the string
            s = s.trim();

            int i = Week.findSeparator(s);
            if (i != -1) {
                String s1 = s.substring(0, i).trim();
                String s2 = s.substring(i + 1, s.length()).trim();

                Year y = Week.evaluateAsYear(s1);
                int w;
                if (y != null) {
                    w = Week.stringToWeek(s2);
                    if (w == -1) {
                        throw new TimePeriodFormatException(
                            "Week.parseWeek(String): can't evaluate the week.");
                    }
                    result = new Week(w, y);
                }
                else {
                    y = Week.evaluateAsYear(s2);
                    if (y != null) {
                        w = Week.stringToWeek(s1);
                        if (w == -1) {
                            throw new TimePeriodFormatException(
                                "Week.parseWeek(String): can't evaluate the week.");
                        }
                        result = new Week(w, y);
                    }
                    else {
                        throw new TimePeriodFormatException(
                            "Week.parseWeek(String): can't evaluate the year.");
                    }
                }

            }
            else {
                throw new TimePeriodFormatException(
                    "Week.parseWeek(String): could not find separator.");
            }

        }
        return result;

    }

    /**
     * Finds the first occurrence of ' ', '-', ',' or '.'
     *
     * @param s  the string to parse.
     *
     * @return <code>-1</code> if none of the characters was found, the
     *      index of the first occurrence otherwise.
     */
    private static int findSeparator(String s) {

        int result = -1;
        result = s.indexOf('-');
        if (result == -1) {
            result = s.indexOf(',');
        }
        if (result == -1) {
            result = s.indexOf(' ');
        }
        if (result == -1) {
            result = s.indexOf('.');
        }
        return result;
    }

    /**
     * Creates a year from a string, or returns null (format exceptions
     * suppressed).
     *
     * @param s  string to parse.
     *
     * @return <code>null</code> if the string is not parseable, the year otherwise.
     */
    private static Year evaluateAsYear(String s) {

        Year result = null;
        try {
            result = Year.parseYear(s);
        }
        catch (TimePeriodFormatException e) {
            // suppress
        }
        return result;

    }

    /**
     * Converts a string to a week.
     *
     * @param s  the string to parse.
     * @return <code>-1</code> if the string does not contain a week number,
     *      the number of the week otherwise.
     */
    private static int stringToWeek(String s) {

        int result = -1;
        s = s.replace('W', ' ');
        s = s.trim();
        try {
            result = Integer.parseInt(s);
            if ((result < 1) || (result > LAST_WEEK_IN_YEAR)) {
                result = -1;
            }
        }
        catch (NumberFormatException e) {
            // suppress
        }
        return result;

    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -