Search in sources :

Example 1 with DateTimeStamp

use of jodd.datetime.DateTimeStamp in project jodd by oblac.

the class AbstractFormatter method parse.

/**
	 * {@inheritDoc}
	 * @see JdtFormatter#parse(String, String)
	 */
public DateTimeStamp parse(String value, String format) {
    char[] valueChars = value.toCharArray();
    char[] formatChars = format.toCharArray();
    int i = 0, j = 0;
    int valueLen = valueChars.length;
    int formatLen = formatChars.length;
    // detect if separators are used
    boolean useSeparators = true;
    if (valueLen == formatLen) {
        useSeparators = false;
        for (char valueChar : valueChars) {
            if (!CharUtil.isDigit(valueChar)) {
                useSeparators = true;
                break;
            }
        }
    }
    DateTimeStamp time = new DateTimeStamp();
    StringBuilder sb = new StringBuilder();
    while (true) {
        int n = findPattern(formatChars, i);
        if (n != -1) {
            // pattern founded
            int patternLen = patterns[n].length;
            i += patternLen;
            sb.setLength(0);
            if (!useSeparators) {
                for (int k = 0; k < patternLen; k++) {
                    sb.append(valueChars[j++]);
                }
            } else {
                char next = 0xFFFF;
                if (i < formatLen) {
                    // next = delimiter
                    next = formatChars[i];
                }
                while ((j < valueLen) && (valueChars[j] != next)) {
                    char scj = valueChars[j];
                    if ((scj != ' ') && (scj != '\t')) {
                        // ignore surrounding whitespaces
                        sb.append(valueChars[j]);
                    }
                    j++;
                }
            }
            parseValue(n, sb.toString(), time);
        } else {
            if (!useSeparators) {
                throw new IllegalArgumentException("Invalid value: " + value);
            }
            if (formatChars[i] == valueChars[j]) {
                j++;
            }
            i++;
        }
        if ((i == formatLen) || (j == valueLen)) {
            break;
        }
    }
    return time;
}
Also used : DateTimeStamp(jodd.datetime.DateTimeStamp)

Example 2 with DateTimeStamp

use of jodd.datetime.DateTimeStamp in project jodd by oblac.

the class ParserTest method testParseWithoutDelimiters.

@Test
public void testParseWithoutDelimiters() {
    Iso8601JdtFormatter formatter = new Iso8601JdtFormatter();
    DateTimeStamp dateTimeStamp = formatter.parse("12341112", "YYYYMMDD");
    assertEquals(new DateTimeStamp(1234, 11, 12), dateTimeStamp);
    String timePattern = "YYYYMMDDhhmmssmss";
    JDateTime jdt = new JDateTime();
    String format = jdt.toString(timePattern);
    JDateTime jdt1 = new JDateTime();
    jdt1.parse(format, timePattern);
    assertEquals(jdt, jdt1);
}
Also used : DateTimeStamp(jodd.datetime.DateTimeStamp) JDateTime(jodd.datetime.JDateTime) Test(org.junit.Test)

Example 3 with DateTimeStamp

use of jodd.datetime.DateTimeStamp in project jodd by oblac.

the class ParserTest method testParseWithDelimiters.

@Test
public void testParseWithDelimiters() {
    Iso8601JdtFormatter formatter = new Iso8601JdtFormatter();
    DateTimeStamp dateTimeStamp = formatter.parse("1234-11-12", "YYYY-MM-DD");
    assertEquals(new DateTimeStamp(1234, 11, 12), dateTimeStamp);
}
Also used : DateTimeStamp(jodd.datetime.DateTimeStamp) Test(org.junit.Test)

Aggregations

DateTimeStamp (jodd.datetime.DateTimeStamp)3 Test (org.junit.Test)2 JDateTime (jodd.datetime.JDateTime)1