Example usage for javax.xml.bind DatatypeConverter printDateTime

List of usage examples for javax.xml.bind DatatypeConverter printDateTime

Introduction

In this page you can find the example usage for javax.xml.bind DatatypeConverter printDateTime.

Prototype

public static String printDateTime(java.util.Calendar val) 

Source Link

Document

Converts a Calendar value into a string.

Usage

From source file:org.eclipse.skalli.core.rest.admin.StatisticsQueryTest.java

@Test
public void testFromToNow() throws Exception {
    Calendar cal = Calendar.getInstance();
    long now = cal.getTimeInMillis();

    StatisticsQuery query = new StatisticsQuery(getParams(null, "now", null), now);
    Assert.assertEquals(0, query.getFrom());
    Assert.assertEquals(now, query.getTo());

    query = new StatisticsQuery(getParams("-1h", "now", null), now);
    Assert.assertEquals(now - TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS), query.getFrom());
    Assert.assertEquals(now, query.getTo());

    long fromMillis = now - TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
    cal.setTimeInMillis(fromMillis);/*  ww w .j a  v  a  2s  .  co  m*/
    String fromStr = DatatypeConverter.printDateTime(cal);
    query = new StatisticsQuery(getParams(fromStr, "now", null), now);
    Assert.assertEquals(fromMillis, query.getFrom());
    Assert.assertEquals(now, query.getTo());

    query = new StatisticsQuery(getParams("now", null, null), now);
    Assert.assertEquals(now, query.getFrom());
    Assert.assertEquals(now, query.getTo());

    query = new StatisticsQuery(getParams("now", "now", null), now);
    Assert.assertEquals(now, query.getFrom());
    Assert.assertEquals(now, query.getTo());

    // period is ignored, if from and to are specified
    query = new StatisticsQuery(getParams("now", "now", "1d"), now);
    Assert.assertEquals(now, query.getFrom());
    Assert.assertEquals(now, query.getTo());
}

From source file:org.eclipse.skalli.core.rest.admin.StatisticsQueryTest.java

private void assertFromPeriodQuery(String period, int value, TimeUnit unit) {
    Calendar cal = Calendar.getInstance();
    long now = cal.getTimeInMillis();
    long fromMillis = now - TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
    cal.setTimeInMillis(fromMillis);/*from  w  w  w .j  a  va2 s  .co m*/
    String fromStr = DatatypeConverter.printDateTime(cal);
    StatisticsQuery query = new StatisticsQuery(getParams(fromStr, null, period), now);
    Assert.assertEquals(fromMillis, query.getFrom());
    Assert.assertEquals(fromMillis + TimeUnit.MILLISECONDS.convert(value, unit), query.getTo());
}

From source file:org.eclipse.skalli.core.rest.admin.StatisticsQueryTest.java

private void assertToPeriodQuery(String period, int value, TimeUnit unit) {
    Calendar cal = Calendar.getInstance();
    long now = cal.getTimeInMillis();
    long toMillis = now - TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
    cal.setTimeInMillis(toMillis);//from   w w  w  .j a  v  a 2  s.  c  om
    String toStr = DatatypeConverter.printDateTime(cal);
    StatisticsQuery query = new StatisticsQuery(getParams(null, toStr, period), now);
    Assert.assertEquals(toMillis - TimeUnit.MILLISECONDS.convert(value, unit), query.getFrom());
    Assert.assertEquals(toMillis, query.getTo());
}

From source file:org.eclipse.skalli.model.EntityBaseTest.java

@Test
public void testLastModified() {
    TestEntityBase entity = new TestEntityBase();
    Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ENGLISH); //$NON-NLS-1$
    String lastModified = DatatypeConverter.printDateTime(now);
    entity.setLastModified(lastModified);
    assertEquals(lastModified, entity.getLastModified());
    assertEquals(now.getTimeInMillis(), entity.getLastModifiedMillis());
    entity.setLastModified(null);/*w  ww.ja v a  2 s  . com*/
    assertNull(entity.getLastModified());
    assertEquals(-1L, entity.getLastModifiedMillis());
    entity.setLastModified("");
    assertNull(entity.getLastModified());
    assertEquals(-1L, entity.getLastModifiedMillis());
    entity.setLastModified(now.getTimeInMillis());
    assertEquals(now.getTimeInMillis(), entity.getLastModifiedMillis());
    assertEquals(lastModified, entity.getLastModified());
    entity.setLastModified(-4711L);
    assertEquals(-1L, entity.getLastModifiedMillis());
    assertNull(entity.getLastModified());
}

From source file:org.eclipse.skalli.testutil.PropertyTestUtil.java

public static Map<String, Object> getValues() {
    HashMap<String, Object> values = new HashMap<String, Object>();
    values.put(EntityBase.PROPERTY_UUID, TestUUIDs.TEST_UUIDS[0]);
    values.put(EntityBase.PROPERTY_DELETED, Boolean.FALSE);
    TestExtensibleEntityBase parent = new TestExtensibleEntityBase(TestUUIDs.TEST_UUIDS[1]);
    values.put(EntityBase.PROPERTY_PARENT_ENTITY, parent);
    values.put(EntityBase.PROPERTY_PARENT_ENTITY_ID, TestUUIDs.TEST_UUIDS[1]);
    TestExtensibleEntityBase firstChild = new TestExtensibleEntityBase(TestUUIDs.TEST_UUIDS[2]);
    values.put(EntityBase.PROPERTY_FIRST_CHILD, firstChild);
    TestExtensibleEntityBase nextSibling = new TestExtensibleEntityBase(TestUUIDs.TEST_UUIDS[3]);
    values.put(EntityBase.PROPERTY_NEXT_SIBLING, nextSibling);
    Calendar now = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ENGLISH); //$NON-NLS-1$
    String lastModified = DatatypeConverter.printDateTime(now);
    values.put(EntityBase.PROPERTY_LAST_MODIFIED, lastModified);
    values.put(EntityBase.PROPERTY_LAST_MODIFIED_BY, "homer"); //$NON-NLS-1$
    return values;
}

From source file:org.elasticsearch.hadoop.hive.HiveWritableValueWriter.java

@Override
public Result write(Writable writable, Generator generator) {
    if (writable instanceof ByteWritable) {
        generator.writeNumber(((ByteWritable) writable).get());
    } else if (writable instanceof DoubleWritable) {
        generator.writeNumber(((DoubleWritable) writable).get());
    } else if (writable instanceof ShortWritable) {
        generator.writeNumber(((ShortWritable) writable).get());
    }//from  w w w. j  ava  2s . c  o  m
    // HiveDecimal - Hive 0.11+
    else if (writable != null && HiveConstants.DECIMAL_WRITABLE.equals(writable.getClass().getName())) {
        generator.writeString(writable.toString());
    }
    // pass the UNIX epoch
    else if (writable instanceof TimestampWritable) {
        long ts = ((TimestampWritable) writable).getTimestamp().getTime();
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(ts);
        generator.writeString(DatatypeConverter.printDateTime(cal));
    }
    // HiveDate - Hive 0.12+
    else if (writable != null && HiveConstants.DATE_WRITABLE.equals(writable.getClass().getName())) {
        generator.writeString(DateWritableWriter.toES(writable));
    }
    // HiveVarcharWritable - Hive 0.12+
    else if (writable != null && HiveConstants.VARCHAR_WRITABLE.equals(writable.getClass().getName())) {
        generator.writeString(writable.toString());
    }
    // HiveChar - Hive 0.13+
    else if (writable != null && HiveConstants.CHAR_WRITABLE.equals(writable.getClass().getName())) {
        generator.writeString(StringUtils.trim(writable.toString()));
    } else {
        return super.write(writable, generator);
    }

    return Result.SUCCESFUL();
}

From source file:org.fcrepo.integration.http.api.AbstractResourceIT.java

protected static String getTTLThatUpdatesServerManagedTriples(final String createdBy, final Calendar created,
        final String modifiedBy, final Calendar modified) {
    final StringBuilder ttl = new StringBuilder();
    if (createdBy != null) {
        addClause(ttl, CREATED_BY.getURI(), "\"" + createdBy + "\"");
    }//  www .j  a  v a2s. co  m
    if (created != null) {
        addClause(ttl, CREATED_DATE.getURI(), "\"" + DatatypeConverter.printDateTime(created)
                + "\"^^<http://www.w3.org/2001/XMLSchema#dateTime>");
    }
    if (modifiedBy != null) {
        addClause(ttl, LAST_MODIFIED_BY.getURI(), "\"" + modifiedBy + "\"");
    }
    if (modified != null) {
        addClause(ttl, LAST_MODIFIED_DATE.getURI(), "\"" + DatatypeConverter.printDateTime(modified)
                + "\"^^<http://www.w3.org/2001/XMLSchema#dateTime>");
    }
    ttl.append(" .\n");
    return ttl.toString();

}

From source file:org.gatein.api.management.Utils.java

public static void set(String name, Date value, ModelObject model) {
    String s = null;/*from  w  w  w.  ja v  a  2s .  co m*/
    if (value != null) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(value);
        s = DatatypeConverter.printDateTime(cal);
    }

    set(name, s, model);
}

From source file:org.kalypso.kalypsomodel1d2d.conv.results.TriangulatedSurfaceDirectTriangleEater.java

@Override
public void setTime(final Date date) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//  w  ww.ja  va 2s. c o m
    final String printedDateTime = DatatypeConverter.printDateTime(calendar);

    m_writer.addProperty(
            new QNameAndString(new QName(UrlCatalog1D2D.MODEL_1D2DResults_NS, "date"), printedDateTime));//$NON-NLS-1$
}

From source file:org.kalypso.simulation.ui.ant.GmlPropertyTask.java

private void addProperty(final GMLWorkspace workspace, final Property property) {
    // validate//from  w  w  w .j a va2s. c  om
    final String name = property.getName();
    if (name == null || name.length() == 0)
        throw new BuildException("Property 'name' is not set.");

    final String featureProperty = property.getFeatureProperty();
    if (featureProperty == null || featureProperty.length() == 0)
        throw new BuildException("Property 'featureProperty' is not set.");

    final String featureID = property.getFeatureID();
    final String featurePath = property.getFeaturePath();
    if ((featureID == null || featureID.length() == 0) && featurePath == null)
        throw new BuildException("Neither 'featureID' nor 'featurePath' is set.");

    // find feature
    final Feature f;
    if (featureID != null) {
        f = workspace.getFeature(featureID);
        if (f == null)
            throw new BuildException("No feature for id: " + featureID);
    } else {
        final Object featureFromPath = workspace.getFeatureFromPath(featurePath);
        if (featureFromPath instanceof Feature)
            f = (Feature) featureFromPath;
        else
            throw new BuildException("No feature found with path: " + featurePath);
    }

    final Object value = f.getProperty(featureProperty);
    if (value instanceof XMLGregorianCalendar) {
        // special handling for Date
        // We write Date as String in XML-Format, in order to correctly transfer timezone information
        final Date date = DateUtilities.toDate(value);
        Calendar cal = Calendar.getInstance(KalypsoCorePlugin.getDefault().getTimeZone());
        cal.setTime(date);

        //      Calendar cal = ((XMLGregorianCalendar) value).toGregorianCalendar();
        //      cal.setTimeZone( KalypsoCorePlugin.getDefault().getTimeZone() );
        final Integer dateoffset = property.getDateoffset();
        final String dateoffsetfield = property.getDateoffsetfield();
        final String dateTruncField = property.getDateTruncField();

        if (dateoffset != null && dateoffsetfield != null)
            cal.add(CalendarUtilities.getCalendarField(dateoffsetfield), dateoffset.intValue());

        if (dateTruncField != null)
            cal = DateUtils.truncate(cal, Integer.parseInt(dateTruncField));

        final String dateString = DatatypeConverter.printDateTime(cal);
        m_propertyAdder.addProperty(name, dateString, null);
    } else if (value != null)
        m_propertyAdder.addProperty(name, value.toString(), null);
    else {
        final Project project2 = getProject();
        if (project2 != null)
            project2.log("No value for feature with id " + f.getId() + " in property: " + featureProperty,
                    Project.MSG_DEBUG);
        final String defaultValue = property.getDefaultValue();
        if (defaultValue != null) {
            if (project2 != null)
                project2.log("Using defualt value: " + defaultValue, Project.MSG_DEBUG);
            m_propertyAdder.addProperty(name, defaultValue, null);
        }
    }
}