Search in sources :

Example 71 with ParsePosition

use of java.text.ParsePosition in project logging-log4j2 by apache.

the class FastDateParser_MoreOrLessTest method testInputHasWrongTimeZone.

@Test
public void testInputHasWrongTimeZone() {
    final FastDateParser parser = new FastDateParser("mm:ss z", NEW_YORK, Locale.US);
    final String input = "11:23 Pacific Standard Time";
    final ParsePosition parsePosition = new ParsePosition(0);
    Assert.assertNotNull(parser.parse(input, parsePosition));
    Assert.assertEquals(input.length(), parsePosition.getIndex());
    parsePosition.setIndex(0);
    Assert.assertNull(parser.parse("11:23 Pacific Standard ", parsePosition));
    Assert.assertEquals(6, parsePosition.getErrorIndex());
}
Also used : ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 72 with ParsePosition

use of java.text.ParsePosition in project logging-log4j2 by apache.

the class FastDateParser_MoreOrLessTest method testInputHasWrongDay.

@Test
public void testInputHasWrongDay() {
    final FastDateParser parser = new FastDateParser("EEEE, MM/dd/yyy", NEW_YORK, Locale.US);
    final String input = "Thursday, 03/23/61";
    final ParsePosition parsePosition = new ParsePosition(0);
    Assert.assertNotNull(parser.parse(input, parsePosition));
    Assert.assertEquals(input.length(), parsePosition.getIndex());
    parsePosition.setIndex(0);
    Assert.assertNull(parser.parse("Thorsday, 03/23/61", parsePosition));
    Assert.assertEquals(0, parsePosition.getErrorIndex());
}
Also used : ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 73 with ParsePosition

use of java.text.ParsePosition in project lucene-solr by apache.

the class TestBackwardsCompatibility method testCreateSortedIndex.

// ant test -Dtestcase=TestBackwardsCompatibility -Dtestmethod=testCreateSortedIndex -Dtests.codec=default -Dtests.useSecurityManager=false -Dtests.bwcdir=/tmp/sorted
public void testCreateSortedIndex() throws Exception {
    Path indexDir = getIndexDir().resolve("sorted");
    Files.deleteIfExists(indexDir);
    Directory dir = newFSDirectory(indexDir);
    LogByteSizeMergePolicy mp = new LogByteSizeMergePolicy();
    mp.setNoCFSRatio(1.0);
    mp.setMaxCFSSegmentSizeMB(Double.POSITIVE_INFINITY);
    MockAnalyzer analyzer = new MockAnalyzer(random());
    analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
    // TODO: remove randomness
    IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    conf.setMergePolicy(mp);
    conf.setUseCompoundFile(false);
    conf.setIndexSort(new Sort(new SortField("dateDV", SortField.Type.LONG, true)));
    IndexWriter writer = new IndexWriter(dir, conf);
    LineFileDocs docs = new LineFileDocs(random());
    SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd", Locale.ROOT);
    parser.setTimeZone(TimeZone.getTimeZone("UTC"));
    ParsePosition position = new ParsePosition(0);
    Field dateDVField = null;
    for (int i = 0; i < 50; i++) {
        Document doc = docs.nextDoc();
        String dateString = doc.get("date");
        position.setIndex(0);
        Date date = parser.parse(dateString, position);
        if (position.getErrorIndex() != -1) {
            throw new AssertionError("failed to parse \"" + dateString + "\" as date");
        }
        if (position.getIndex() != dateString.length()) {
            throw new AssertionError("failed to parse \"" + dateString + "\" as date");
        }
        if (dateDVField == null) {
            dateDVField = new NumericDocValuesField("dateDV", 0l);
            doc.add(dateDVField);
        }
        dateDVField.setLongValue(date.getTime());
        if (i == 250) {
            writer.commit();
        }
        writer.addDocument(doc);
    }
    writer.forceMerge(1);
    writer.close();
    dir.close();
}
Also used : Path(java.nio.file.Path) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) BinaryPoint(org.apache.lucene.document.BinaryPoint) DoublePoint(org.apache.lucene.document.DoublePoint) LongPoint(org.apache.lucene.document.LongPoint) IntPoint(org.apache.lucene.document.IntPoint) FloatPoint(org.apache.lucene.document.FloatPoint) Date(java.util.Date) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) SortField(org.apache.lucene.search.SortField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) SortedSetDocValuesField(org.apache.lucene.document.SortedSetDocValuesField) BinaryDocValuesField(org.apache.lucene.document.BinaryDocValuesField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) StringField(org.apache.lucene.document.StringField) DoubleDocValuesField(org.apache.lucene.document.DoubleDocValuesField) FloatDocValuesField(org.apache.lucene.document.FloatDocValuesField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Sort(org.apache.lucene.search.Sort) SimpleDateFormat(java.text.SimpleDateFormat) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory) LineFileDocs(org.apache.lucene.util.LineFileDocs) ParsePosition(java.text.ParsePosition)

Example 74 with ParsePosition

use of java.text.ParsePosition in project lucene-solr by apache.

the class TrecContentSource method getDateFormatInfo.

private DateFormatInfo getDateFormatInfo() {
    DateFormatInfo dfi = dateFormats.get();
    if (dfi == null) {
        dfi = new DateFormatInfo();
        dfi.dfs = new SimpleDateFormat[DATE_FORMATS.length];
        for (int i = 0; i < dfi.dfs.length; i++) {
            dfi.dfs[i] = new SimpleDateFormat(DATE_FORMATS[i], Locale.ENGLISH);
            dfi.dfs[i].setLenient(true);
        }
        dfi.pos = new ParsePosition(0);
        dateFormats.set(dfi);
    }
    return dfi;
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition)

Example 75 with ParsePosition

use of java.text.ParsePosition in project logging-log4j2 by apache.

the class Format method parseObject.

public Object parseObject(final String source) throws ParseException {
    final ParsePosition pos = new ParsePosition(0);
    final Object result = parseObject(source, pos);
    if (pos.getIndex() == 0) {
        throw new ParseException("Format.parseObject(String) failed", pos.getErrorIndex());
    }
    return result;
}
Also used : ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition)

Aggregations

ParsePosition (java.text.ParsePosition)694 Test (org.junit.Test)250 Date (java.util.Date)213 TemporalAccessor (java.time.temporal.TemporalAccessor)163 SimpleDateFormat (java.text.SimpleDateFormat)117 DateTimeFormatter (java.time.format.DateTimeFormatter)94 Test (org.testng.annotations.Test)88 ParseException (java.text.ParseException)71 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)49 SimpleDateFormat (android.icu.text.SimpleDateFormat)39 FieldPosition (java.text.FieldPosition)32 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)25 Calendar (android.icu.util.Calendar)23 ULocale (android.icu.util.ULocale)23 NumberFormat (java.text.NumberFormat)23 Calendar (java.util.Calendar)23 Format (java.text.Format)21 DecimalFormat (java.text.DecimalFormat)19 GregorianCalendar (android.icu.util.GregorianCalendar)18 JapaneseCalendar (android.icu.util.JapaneseCalendar)17