List of usage examples for org.jfree.data.time TimePeriodFormatException TimePeriodFormatException
public TimePeriodFormatException(String message)
From source file:org.jfree.data.time.Quarter.java
/** * Parses the string argument as a quarter. * <P>//from w ww .ja va 2 s . c o m * This method should accept the following formats: "YYYY-QN" and "QN-YYYY", * where the "-" can be a space, a forward-slash (/), comma or a dash (-). * @param s A string representing the quarter. * * @return The quarter. */ public static Quarter parseQuarter(String s) { // find the Q and the integer following it (remove both from the // string)... int i = s.indexOf("Q"); if (i == -1) { throw new TimePeriodFormatException("Missing Q."); } if (i == s.length() - 1) { throw new TimePeriodFormatException("Q found at end of string."); } String qstr = s.substring(i + 1, i + 2); int quarter = Integer.parseInt(qstr); String remaining = s.substring(0, i) + s.substring(i + 2, s.length()); // replace any / , or - with a space remaining = remaining.replace('/', ' '); remaining = remaining.replace(',', ' '); remaining = remaining.replace('-', ' '); // parse the string... Year year = Year.parseYear(remaining.trim()); Quarter result = new Quarter(quarter, year); return result; }
From source file:org.jfree.data.time.Month.java
/** * Parses the string argument as a month. This method is required to * accept the format "YYYY-MM". It will also accept "MM-YYYY". Anything * else, at the moment, is a bonus.// ww w. jav a 2s. co m * * @param s the string to parse (<code>null</code> permitted). * * @return <code>null</code> if the string is not parseable, the month * otherwise. */ public static Month parseMonth(String s) { Month result = null; if (s == null) { return result; } // trim whitespace from either end of the string s = s.trim(); int i = Month.findSeparator(s); String s1, s2; boolean yearIsFirst; // if there is no separator, we assume the first four characters // are YYYY if (i == -1) { yearIsFirst = true; s1 = s.substring(0, 5); s2 = s.substring(5); } else { s1 = s.substring(0, i).trim(); s2 = s.substring(i + 1, s.length()).trim(); // now it is trickier to determine if the month or year is first Year y1 = Month.evaluateAsYear(s1); if (y1 == null) { yearIsFirst = false; } else { Year y2 = Month.evaluateAsYear(s2); if (y2 == null) { yearIsFirst = true; } else { yearIsFirst = (s1.length() > s2.length()); } } } Year year; int month; if (yearIsFirst) { year = Month.evaluateAsYear(s1); month = SerialDate.stringToMonthCode(s2); } else { year = Month.evaluateAsYear(s2); month = SerialDate.stringToMonthCode(s1); } if (month == -1) { throw new TimePeriodFormatException("Can't evaluate the month."); } if (year == null) { throw new TimePeriodFormatException("Can't evaluate the year."); } result = new Month(month, year); return result; }
From source file:org.jfree.data.time.Week.java
/** * Parses the string argument as a week. * <P>/*from w ww . j a v a2 s .c o m*/ * 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; }