Search in sources :

Example 1 with PartialDate

use of ezvcard.util.PartialDate in project ez-vcard by mangstadt.

the class DateOrTimePropertyScribe method _writeText.

@Override
protected String _writeText(T property, WriteContext context) {
    VCardVersion version = context.getVersion();
    Date date = property.getDate();
    if (date != null) {
        boolean extended = (version == VCardVersion.V3_0);
        return date(date).time(property.hasTime()).extended(extended).utc(false).write();
    }
    if (version == VCardVersion.V4_0) {
        String text = property.getText();
        if (text != null) {
            return VObjectPropertyValues.escape(text);
        }
        PartialDate partialDate = property.getPartialDate();
        if (partialDate != null) {
            return partialDate.toISO8601(false);
        }
    }
    return "";
}
Also used : VCardVersion(ezvcard.VCardVersion) PartialDate(ezvcard.util.PartialDate) Date(java.util.Date) PartialDate(ezvcard.util.PartialDate)

Example 2 with PartialDate

use of ezvcard.util.PartialDate in project ez-vcard by mangstadt.

the class DateOrTimePropertyScribe method _writeJson.

@Override
protected JCardValue _writeJson(T property) {
    Date date = property.getDate();
    if (date != null) {
        boolean hasTime = property.hasTime();
        String value = date(date).time(hasTime).extended(true).utc(false).write();
        return JCardValue.single(value);
    }
    PartialDate partialDate = property.getPartialDate();
    if (partialDate != null) {
        String value = partialDate.toISO8601(true);
        return JCardValue.single(value);
    }
    String text = property.getText();
    if (text != null) {
        return JCardValue.single(text);
    }
    return JCardValue.single("");
}
Also used : PartialDate(ezvcard.util.PartialDate) Date(java.util.Date) PartialDate(ezvcard.util.PartialDate)

Example 3 with PartialDate

use of ezvcard.util.PartialDate in project ez-vcard by mangstadt.

the class DateOrTimePropertyScribe method _writeXml.

@Override
protected void _writeXml(T property, XCardElement parent) {
    Date date = property.getDate();
    if (date != null) {
        boolean hasTime = property.hasTime();
        String value = date(date).time(hasTime).extended(false).utc(false).write();
        VCardDataType dataType = hasTime ? VCardDataType.DATE_TIME : VCardDataType.DATE;
        parent.append(dataType, value);
        return;
    }
    PartialDate partialDate = property.getPartialDate();
    if (partialDate != null) {
        VCardDataType dataType;
        if (partialDate.hasTimeComponent() && partialDate.hasDateComponent()) {
            dataType = VCardDataType.DATE_TIME;
        } else if (partialDate.hasTimeComponent()) {
            dataType = VCardDataType.TIME;
        } else if (partialDate.hasDateComponent()) {
            dataType = VCardDataType.DATE;
        } else {
            dataType = VCardDataType.DATE_AND_OR_TIME;
        }
        parent.append(dataType, partialDate.toISO8601(false));
        return;
    }
    String text = property.getText();
    if (text != null) {
        parent.append(VCardDataType.TEXT, text);
        return;
    }
    parent.append(VCardDataType.DATE_AND_OR_TIME, "");
}
Also used : PartialDate(ezvcard.util.PartialDate) VCardDataType(ezvcard.VCardDataType) Date(java.util.Date) PartialDate(ezvcard.util.PartialDate)

Example 4 with PartialDate

use of ezvcard.util.PartialDate in project ez-vcard by mangstadt.

the class DateOrTimePropertyTest method set_value.

@Test
public void set_value() {
    DateOrTimePropertyImpl property = new DateOrTimePropertyImpl();
    Date date = date("2016-01-14");
    property.setDate(date, true);
    assertEquals(date, property.getDate());
    assertTrue(property.hasTime());
    assertNull(property.getPartialDate());
    assertNull(property.getText());
    property.setDate(null, true);
    assertNull(property.getDate());
    assertFalse(property.hasTime());
    assertNull(property.getPartialDate());
    assertNull(property.getText());
    PartialDate partialDate = new PartialDate.Builder().month(1).date(14).build();
    property.setPartialDate(partialDate);
    assertNull(property.getDate());
    assertFalse(property.hasTime());
    assertEquals(partialDate, property.getPartialDate());
    assertNull(property.getText());
    String text = "text";
    property.setText(text);
    assertNull(property.getDate());
    assertFalse(property.hasTime());
    assertNull(property.getPartialDate());
    assertEquals(text, property.getText());
    property.setDate(date, true);
    assertEquals(date, property.getDate());
    assertTrue(property.hasTime());
    assertNull(property.getPartialDate());
    assertNull(property.getText());
    property.setPartialDate(null);
    assertNull(property.getDate());
    assertFalse(property.hasTime());
    assertNull(property.getPartialDate());
    assertNull(property.getText());
}
Also used : PartialDate(ezvcard.util.PartialDate) Date(java.util.Date) PartialDate(ezvcard.util.PartialDate) Test(org.junit.Test)

Example 5 with PartialDate

use of ezvcard.util.PartialDate in project ez-vcard by mangstadt.

the class DateOrTimePropertyTest method constructors.

@Test
public void constructors() throws Exception {
    DateOrTimePropertyImpl property = new DateOrTimePropertyImpl();
    assertNull(property.getDate());
    assertFalse(property.hasTime());
    assertNull(property.getPartialDate());
    assertNull(property.getText());
    Date date = date("2016-01-14");
    property = new DateOrTimePropertyImpl(date, true);
    assertEquals(date, property.getDate());
    assertTrue(property.hasTime());
    assertNull(property.getPartialDate());
    assertNull(property.getText());
    property = new DateOrTimePropertyImpl(date, false);
    assertEquals(date, property.getDate());
    assertFalse(property.hasTime());
    assertNull(property.getPartialDate());
    assertNull(property.getText());
    PartialDate partialDate = new PartialDate.Builder().month(1).date(14).build();
    property = new DateOrTimePropertyImpl(partialDate);
    assertNull(property.getDate());
    assertFalse(property.hasTime());
    assertEquals(partialDate, property.getPartialDate());
    assertNull(property.getText());
    partialDate = new PartialDate.Builder().month(1).date(14).hour(12).build();
    property = new DateOrTimePropertyImpl(partialDate);
    assertNull(property.getDate());
    assertTrue(property.hasTime());
    assertEquals(partialDate, property.getPartialDate());
    assertNull(property.getText());
    String text = "text";
    property = new DateOrTimePropertyImpl(text);
    assertNull(property.getDate());
    assertFalse(property.hasTime());
    assertNull(property.getPartialDate());
    assertEquals(text, property.getText());
}
Also used : PartialDate(ezvcard.util.PartialDate) Date(java.util.Date) PartialDate(ezvcard.util.PartialDate) Test(org.junit.Test)

Aggregations

PartialDate (ezvcard.util.PartialDate)5 Date (java.util.Date)5 Test (org.junit.Test)2 VCardDataType (ezvcard.VCardDataType)1 VCardVersion (ezvcard.VCardVersion)1