Search in sources :

Example 26 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project CoreNLP by stanfordnlp.

the class JodaTimeUtils method combine.

public static Partial combine(Partial p1, Partial p2) {
    if (p1 == null)
        return p2;
    if (p2 == null)
        return p1;
    Partial p = p1;
    for (int i = 0; i < p2.size(); i++) {
        DateTimeFieldType fieldType = p2.getFieldType(i);
        if (fieldType == DateTimeFieldType.year()) {
            if (p.isSupported(DateTimeFieldType.yearOfCentury())) {
                if (!p.isSupported(DateTimeFieldType.centuryOfEra())) {
                    int yoc = p.get(DateTimeFieldType.yearOfCentury());
                    int refYear = p2.getValue(i);
                    int century = refYear / 100;
                    int y2 = yoc + century * 100;
                    // TODO: Figure out which way to go
                    if (refYear < y2) {
                        y2 -= 100;
                    }
                    p = p.without(DateTimeFieldType.yearOfCentury());
                    p = p.with(DateTimeFieldType.year(), y2);
                }
                continue;
            } else if (p.isSupported(DateTimeFieldType.centuryOfEra())) {
                continue;
            }
        } else if (fieldType == DateTimeFieldType.yearOfCentury()) {
            if (p.isSupported(DateTimeFieldType.year())) {
                continue;
            }
        } else if (fieldType == DateTimeFieldType.centuryOfEra()) {
            if (p.isSupported(DateTimeFieldType.year())) {
                continue;
            }
        }
        if (!p.isSupported(fieldType)) {
            p = p.with(fieldType, p2.getValue(i));
        }
    }
    if (!p.isSupported(DateTimeFieldType.year())) {
        if (p.isSupported(DateTimeFieldType.yearOfCentury()) && p.isSupported(DateTimeFieldType.centuryOfEra())) {
            int year = p.get(DateTimeFieldType.yearOfCentury()) + p.get(DateTimeFieldType.centuryOfEra()) * 100;
            p = p.with(DateTimeFieldType.year(), year);
            p = p.without(DateTimeFieldType.yearOfCentury());
            p = p.without(DateTimeFieldType.centuryOfEra());
        }
    }
    if (p.isSupported(DateTimeFieldType.halfdayOfDay())) {
        int hour = -1;
        if (p.isSupported(DateTimeFieldType.hourOfHalfday())) {
            hour = p.get(DateTimeFieldType.hourOfHalfday());
            p = p.without(DateTimeFieldType.hourOfHalfday());
        } else if (p.isSupported(DateTimeFieldType.clockhourOfHalfday())) {
            hour = p.get(DateTimeFieldType.clockhourOfHalfday()) - 1;
            p = p.without(DateTimeFieldType.clockhourOfHalfday());
        } else if (p.isSupported(DateTimeFieldType.clockhourOfDay())) {
            hour = p.get(DateTimeFieldType.clockhourOfDay()) - 1;
            p = p.without(DateTimeFieldType.clockhourOfDay());
        } else if (p.isSupported(DateTimeFieldType.hourOfDay())) {
            hour = p.get(DateTimeFieldType.hourOfDay());
            p = p.without(DateTimeFieldType.hourOfDay());
        }
        if (hour >= 0) {
            if (p.get(DateTimeFieldType.halfdayOfDay()) == SUTime.HALFDAY_PM) {
                if (hour < 12) {
                    hour = hour + 12;
                }
            } else if (hour == 12) {
                hour = 0;
            }
            if (hour < 24) {
                p = p.with(DateTimeFieldType.hourOfDay(), hour);
            } else {
                p = p.with(DateTimeFieldType.clockhourOfDay(), hour);
            }
        }
    }
    return p;
}
Also used : DateTimeFieldType(org.joda.time.DateTimeFieldType)

Example 27 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project joda-time by JodaOrg.

the class TestISODateTimeFormat_Fields method testForFields_datetime_YMDH.

//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public void testForFields_datetime_YMDH() {
    DateTimeFieldType[] fields = new DateTimeFieldType[] { DateTimeFieldType.year(), DateTimeFieldType.monthOfYear(), DateTimeFieldType.dayOfMonth(), DateTimeFieldType.hourOfDay() };
    int[] values = new int[] { 2005, 6, 25, 12 };
    List types = new ArrayList(Arrays.asList(fields));
    DateTimeFormatter f = ISODateTimeFormat.forFields(types, true, true);
    assertEquals("2005-06-25T12", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, true, false);
    assertEquals("2005-06-25T12", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, true);
    assertEquals("20050625T12", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, false);
    assertEquals("20050625T12", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
}
Also used : Partial(org.joda.time.Partial) DateTimeFieldType(org.joda.time.DateTimeFieldType) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 28 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project joda-time by JodaOrg.

the class TestISODateTimeFormat_Fields method testForFields_weekBased_D.

//-----------------------------------------------------------------------
public void testForFields_weekBased_D() {
    DateTimeFieldType[] fields = new DateTimeFieldType[] { DateTimeFieldType.dayOfWeek() };
    int[] values = new int[] { 5 };
    List types = new ArrayList(Arrays.asList(fields));
    DateTimeFormatter f = ISODateTimeFormat.forFields(types, true, true);
    assertEquals("-W-5", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, true, false);
    assertEquals("-W-5", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, true);
    assertEquals("-W-5", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, false);
    assertEquals("-W-5", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
}
Also used : Partial(org.joda.time.Partial) DateTimeFieldType(org.joda.time.DateTimeFieldType) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 29 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project joda-time by JodaOrg.

the class TestISODateTimeFormat_Fields method testForFields_time_H.

//-----------------------------------------------------------------------
public void testForFields_time_H() {
    DateTimeFieldType[] fields = new DateTimeFieldType[] { DateTimeFieldType.hourOfDay() };
    int[] values = new int[] { 10 };
    List types = new ArrayList(Arrays.asList(fields));
    DateTimeFormatter f = ISODateTimeFormat.forFields(types, true, true);
    assertEquals("10", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, true, false);
    assertEquals("10", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, true);
    assertEquals("10", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, false);
    assertEquals("10", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
}
Also used : Partial(org.joda.time.Partial) DateTimeFieldType(org.joda.time.DateTimeFieldType) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 30 with DateTimeFieldType

use of org.joda.time.DateTimeFieldType in project joda-time by JodaOrg.

the class TestISODateTimeFormat_Fields method testForFields_calBased_Y.

//-----------------------------------------------------------------------
public void testForFields_calBased_Y() {
    DateTimeFieldType[] fields = new DateTimeFieldType[] { DateTimeFieldType.year() };
    int[] values = new int[] { 2005 };
    List types = new ArrayList(Arrays.asList(fields));
    DateTimeFormatter f = ISODateTimeFormat.forFields(types, true, true);
    assertEquals("2005", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, true, false);
    assertEquals("2005", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, true);
    assertEquals("2005", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
    types = new ArrayList(Arrays.asList(fields));
    f = ISODateTimeFormat.forFields(types, false, false);
    assertEquals("2005", f.print(new Partial(fields, values)));
    assertEquals(0, types.size());
}
Also used : Partial(org.joda.time.Partial) DateTimeFieldType(org.joda.time.DateTimeFieldType) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

DateTimeFieldType (org.joda.time.DateTimeFieldType)47 ArrayList (java.util.ArrayList)38 List (java.util.List)38 Partial (org.joda.time.Partial)38 DurationFieldType (org.joda.time.DurationFieldType)3 DateTime (org.joda.time.DateTime)2 Period (org.joda.time.Period)2 ScaledDurationField (org.joda.time.field.ScaledDurationField)2 DateTimeField (org.joda.time.DateTimeField)1 ISOChronology (org.joda.time.chrono.ISOChronology)1