week.java
来自「JfreeChart 常用图表例子」· Java 代码 · 共 551 行 · 第 1/2 页
JAVA
551 行
* @return The serial index number. */ public long getSerialIndex() { return this.year.getYear() * 53L + this.week; } /** * Returns the first millisecond of the week, evaluated using the supplied * calendar (which determines the time zone). * * @param calendar the calendar. * * @return The first millisecond of the week. */ public long getFirstMillisecond(Calendar calendar) { Calendar c = (Calendar) calendar.clone(); c.clear(); c.set(Calendar.YEAR, this.year.getYear()); c.set(Calendar.WEEK_OF_YEAR, this.week); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); c.set(Calendar.HOUR, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); //return c.getTimeInMillis(); // this won't work for JDK 1.3 return c.getTime().getTime(); } /** * 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) { RegularTimePeriod next = next(); return next.getFirstMillisecond(calendar) - 1; } /** * Returns a string representing the week (e.g. "Week 9, 2002"). * * TODO: look at internationalisation. * * @return A string representing the week. */ public String toString() { return "Week " + this.week + ", " + this.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 == this) { return true; } if (!(obj instanceof Week)) { return false; } Week that = (Week) obj; if (this.week != that.week) { return false; } if (!this.year.equals(that.year)) { return false; } return true; } /** * Returns a hash code for this object instance. The approach described by * Joshua Bloch in "Effective Java" has been used here: * <p> * <code>http://developer.java.sun.com/developer/Books/effectivejava * /Chapter3.pdf</code> * * @return A hash code. */ public int hashCode() { int result = 17; result = 37 * result + this.week; result = 37 * result + this.year.hashCode(); return result; } /** * 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. */ public static Week parseWeek(String s) { 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( "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( "Can't evaluate the week." ); } result = new Week(w, y); } else { throw new TimePeriodFormatException( "Can't evaluate the year." ); } } } else { throw new TimePeriodFormatException( "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 = 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 + =
减小字号Ctrl + -
显示快捷键?