Example usage for org.jfree.data.time Week findSeparator

List of usage examples for org.jfree.data.time Week findSeparator

Introduction

In this page you can find the example usage for org.jfree.data.time Week findSeparator.

Prototype

private static int findSeparator(String s) 

Source Link

Document

Finds the first occurrence of ' ', '-', ',' or '.'

Usage

From source file:org.jfree.data.time.Week.java

/**
 * Parses the string argument as a week.
 * <P>//w w w . jav a 2s .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;

}