Example usage for org.joda.time.format DateTimeFormatter print

List of usage examples for org.joda.time.format DateTimeFormatter print

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatter print.

Prototype

public String print(ReadablePartial partial) 

Source Link

Document

Prints a ReadablePartial to a new String.

Usage

From source file:org.egov.infra.workflow.entity.contract.StateInfoBuilder.java

License:Open Source License

public StateInfoBuilder refDate(Date refDate) {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy hh:mm a");
    this.refDate = formatter.print(new DateTime(refDate));
    return this;
}

From source file:org.egov.pgr.entity.Complaint.java

License:Open Source License

@Override
public String getStateDetails() {
    DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-yyyy hh:mm a");
    return String.format("Complaint Number %s for %s filed on %s. Date of resolution %s", this.getCrn(),
            this.getComplaintType().getName(), formatter.print(new DateTime(this.getCreatedDate())),
            formatter.print(this.getEscalationDate()));
}

From source file:org.egov.pgr.entity.ComplaintRestAdaptor.java

License:Open Source License

@Override
public JsonElement serialize(final Complaint complaint, final Type type, final JsonSerializationContext jsc) {
    final DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MM-yyyy hh:mm a");
    String resolutionDate = "";
    resolutionDate = formatter.print(complaint.getEscalationDate());
    final JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("complaintnumber", complaint.getCrn());
    jsonObject.addProperty("timetoresolve", "" + resolutionDate);
    jsonObject.addProperty("message",
            "Thank you for your submission. We apologize for the inconvenience caused to you. We will resolve the complaint by :"
                    + resolutionDate);//from  w  w w .ja v  a 2 s  . c o m
    return jsonObject;
}

From source file:org.elasticsearch.common.xcontent.BaseXContentTestCase.java

License:Apache License

public void testReadableInstant() throws Exception {
    assertResult("{'instant':null}",
            () -> builder().startObject().field("instant", (ReadableInstant) null).endObject());
    assertResult("{'instant':null}",
            () -> builder().startObject().field("instant").value((ReadableInstant) null).endObject());

    final DateTime t1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC);

    String expected = "{'t1':'2016-01-01T00:00:00.000Z'}";
    assertResult(expected, () -> builder().startObject().field("t1", t1).endObject());
    assertResult(expected, () -> builder().startObject().field("t1").value(t1).endObject());

    final DateTime t2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC);

    expected = "{'t2':'2016-12-25T07:59:42.213Z'}";
    assertResult(expected, () -> builder().startObject().field("t2", t2).endObject());
    assertResult(expected, () -> builder().startObject().field("t2").value(t2).endObject());

    final DateTimeFormatter formatter = randomFrom(ISODateTimeFormat.basicDate(),
            ISODateTimeFormat.dateTimeNoMillis());
    final DateTime t3 = DateTime.now();

    expected = "{'t3':'" + formatter.print(t3) + "'}";
    assertResult(expected, () -> builder().startObject().field("t3", t3, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t3").value(t3, formatter).endObject());

    final DateTime t4 = new DateTime(randomDateTimeZone());

    expected = "{'t4':'" + formatter.print(t4) + "'}";
    assertResult(expected, () -> builder().startObject().field("t4", t4, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t4").value(t4, formatter).endObject());

    long date = Math.abs(randomLong() % (2 * (long) 10e11)); // 1970-01-01T00:00:00Z - 2033-05-18T05:33:20.000+02:00
    final DateTime t5 = new DateTime(date, randomDateTimeZone());

    expected = "{'t5':'" + XContentBuilder.DEFAULT_DATE_PRINTER.print(t5) + "'}";
    assertResult(expected, () -> builder().startObject().field("t5", t5).endObject());
    assertResult(expected, () -> builder().startObject().field("t5").value(t5).endObject());

    expected = "{'t5':'" + formatter.print(t5) + "'}";
    assertResult(expected, () -> builder().startObject().field("t5", t5, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("t5").value(t5, formatter).endObject());

    Instant i1 = new Instant(1451606400000L); // 2016-01-01T00:00:00.000Z
    expected = "{'i1':'2016-01-01T00:00:00.000Z'}";
    assertResult(expected, () -> builder().startObject().field("i1", i1).endObject());
    assertResult(expected, () -> builder().startObject().field("i1").value(i1).endObject());

    Instant i2 = new Instant(1482652782213L); // 2016-12-25T07:59:42.213Z
    expected = "{'i2':'" + formatter.print(i2) + "'}";
    assertResult(expected, () -> builder().startObject().field("i2", i2, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("i2").value(i2, formatter).endObject());

    expectNonNullFormatterException(() -> builder().startObject().field("t3", t3, null).endObject());
    expectNonNullFormatterException(() -> builder().startObject().field("t3").value(t3, null).endObject());
}

From source file:org.elasticsearch.common.xcontent.BaseXContentTestCase.java

License:Apache License

public void testDate() throws Exception {
    assertResult("{'date':null}", () -> builder().startObject().field("date", (Date) null).endObject());
    assertResult("{'date':null}", () -> builder().startObject().field("date").value((Date) null).endObject());

    final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
    assertResult("{'d1':'2016-01-01T00:00:00.000Z'}",
            () -> builder().startObject().field("d1", d1).endObject());
    assertResult("{'d1':'2016-01-01T00:00:00.000Z'}",
            () -> builder().startObject().field("d1").value(d1).endObject());

    final Date d2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC).toDate();
    assertResult("{'d2':'2016-12-25T07:59:42.213Z'}",
            () -> builder().startObject().field("d2", d2).endObject());
    assertResult("{'d2':'2016-12-25T07:59:42.213Z'}",
            () -> builder().startObject().field("d2").value(d2).endObject());

    final DateTimeFormatter formatter = randomFrom(ISODateTimeFormat.basicDate(),
            ISODateTimeFormat.dateTimeNoMillis());
    final Date d3 = DateTime.now().toDate();

    String expected = "{'d3':'" + formatter.print(d3.getTime()) + "'}";
    assertResult(expected, () -> builder().startObject().field("d3", d3, formatter).endObject());
    assertResult(expected, () -> builder().startObject().field("d3").value(d3, formatter).endObject());

    expectNonNullFormatterException(() -> builder().startObject().field("d3", d3, null).endObject());
    expectNonNullFormatterException(() -> builder().startObject().field("d3").value(d3, null).endObject());
    expectNonNullFormatterException(() -> builder().value(null, 1L));
}

From source file:org.elasticsearch.common.xcontent.BaseXContentTestCase.java

License:Apache License

public void testObjects() throws Exception {
    Map<String, Object[]> objects = new HashMap<>();
    objects.put("{'objects':[false,true,false]}", new Object[] { false, true, false });
    objects.put("{'objects':[1,1,2,3,5,8,13]}",
            new Object[] { (byte) 1, (byte) 1, (byte) 2, (byte) 3, (byte) 5, (byte) 8, (byte) 13 });
    objects.put("{'objects':[1.0,1.0,2.0,3.0,5.0,8.0,13.0]}",
            new Object[] { 1.0d, 1.0d, 2.0d, 3.0d, 5.0d, 8.0d, 13.0d });
    objects.put("{'objects':[1.0,1.0,2.0,3.0,5.0,8.0,13.0]}",
            new Object[] { 1.0f, 1.0f, 2.0f, 3.0f, 5.0f, 8.0f, 13.0f });
    objects.put("{'objects':[{'lat':45.759429931640625,'lon':4.8394775390625}]}",
            new Object[] { GeoPoint.fromGeohash("u05kq4k") });
    objects.put("{'objects':[1,1,2,3,5,8,13]}", new Object[] { 1, 1, 2, 3, 5, 8, 13 });
    objects.put("{'objects':[1,1,2,3,5,8,13]}", new Object[] { 1L, 1L, 2L, 3L, 5L, 8L, 13L });
    objects.put("{'objects':[1,1,2,3,5,8]}",
            new Object[] { (short) 1, (short) 1, (short) 2, (short) 3, (short) 5, (short) 8 });
    objects.put("{'objects':['a','b','c']}", new Object[] { "a", "b", "c" });
    objects.put("{'objects':['a','b','c']}",
            new Object[] { new Text("a"), new Text(new BytesArray("b")), new Text("c") });
    objects.put("{'objects':null}", null);
    objects.put("{'objects':[null,null,null]}", new Object[] { null, null, null });
    objects.put("{'objects':['OPEN','CLOSE']}", IndexMetaData.State.values());
    objects.put("{'objects':[{'f1':'v1'},{'f2':'v2'}]}",
            new Object[] { singletonMap("f1", "v1"), singletonMap("f2", "v2") });
    objects.put("{'objects':[[1,2,3],[4,5]]}", new Object[] { Arrays.asList(1, 2, 3), Arrays.asList(4, 5) });

    final String paths = Constants.WINDOWS ? "{'objects':['a\\\\b\\\\c','d\\\\e']}"
            : "{'objects':['a/b/c','d/e']}";
    objects.put(paths, new Object[] { PathUtils.get("a", "b", "c"), PathUtils.get("d", "e") });

    final DateTimeFormatter formatter = XContentBuilder.DEFAULT_DATE_PRINTER;
    final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
    final Date d2 = new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
    objects.put("{'objects':['" + formatter.print(d1.getTime()) + "','" + formatter.print(d2.getTime()) + "']}",
            new Object[] { d1, d2 });

    final DateTime dt1 = DateTime.now();
    final DateTime dt2 = new DateTime(2016, 12, 25, 7, 59, 42, 213, DateTimeZone.UTC);
    objects.put("{'objects':['" + formatter.print(dt1) + "','2016-12-25T07:59:42.213Z']}",
            new Object[] { dt1, dt2 });

    final Calendar c1 = new DateTime(2012, 7, 7, 10, 23, DateTimeZone.UTC).toCalendar(Locale.ROOT);
    final Calendar c2 = new DateTime(2014, 11, 16, 19, 36, DateTimeZone.UTC).toCalendar(Locale.ROOT);
    objects.put("{'objects':['2012-07-07T10:23:00.000Z','2014-11-16T19:36:00.000Z']}", new Object[] { c1, c2 });

    final ToXContent x1 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", 2)
            .array("f3", 3, 4, 5).endObject();
    final ToXContent x2 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", x1)
            .endObject();/* w  ww  . jav a  2  s.  co m*/
    objects.put(
            "{'objects':[{'f1':'v1','f2':2,'f3':[3,4,5]},{'f1':'v1','f2':{'f1':'v1','f2':2,'f3':[3,4,5]}}]}",
            new Object[] { x1, x2 });

    for (Map.Entry<String, Object[]> o : objects.entrySet()) {
        final String expected = o.getKey();
        assertResult(expected, () -> builder().startObject().field("objects", o.getValue()).endObject());
        assertResult(expected, () -> builder().startObject().field("objects").value(o.getValue()).endObject());
        assertResult(expected, () -> builder().startObject().field("objects").values(o.getValue()).endObject());
        assertResult(expected, () -> builder().startObject().array("objects", o.getValue()).endObject());
    }
}

From source file:org.elasticsearch.common.xcontent.BaseXContentTestCase.java

License:Apache License

public void testObject() throws Exception {
    Map<String, Object> object = new HashMap<>();
    object.put("{'object':false}", Boolean.FALSE);
    object.put("{'object':13}", (byte) 13);
    object.put("{'object':5.0}", 5.0d);
    object.put("{'object':8.0}", 8.0f);
    object.put("{'object':{'lat':45.759429931640625,'lon':4.8394775390625}}", GeoPoint.fromGeohash("u05kq4k"));
    object.put("{'object':3}", 3);
    object.put("{'object':2}", 2L);
    object.put("{'object':1}", (short) 1);
    object.put("{'object':'string'}", "string");
    object.put("{'object':'a'}", new Text("a"));
    object.put("{'object':'b'}", new Text(new BytesArray("b")));
    object.put("{'object':null}", null);
    object.put("{'object':'OPEN'}", IndexMetaData.State.OPEN);
    object.put("{'object':'NM'}", DistanceUnit.NAUTICALMILES);
    object.put("{'object':{'f1':'v1'}}", singletonMap("f1", "v1"));
    object.put("{'object':{'f1':{'f2':'v2'}}}", singletonMap("f1", singletonMap("f2", "v2")));
    object.put("{'object':[1,2,3]}", Arrays.asList(1, 2, 3));

    final String path = Constants.WINDOWS ? "{'object':'a\\\\b\\\\c'}" : "{'object':'a/b/c'}";
    object.put(path, PathUtils.get("a", "b", "c"));

    final DateTimeFormatter formatter = XContentBuilder.DEFAULT_DATE_PRINTER;
    final Date d1 = new DateTime(2016, 1, 1, 0, 0, DateTimeZone.UTC).toDate();
    object.put("{'object':'" + formatter.print(d1.getTime()) + "'}", d1);

    final DateTime d2 = DateTime.now();
    object.put("{'object':'" + formatter.print(d2) + "'}", d2);

    final Calendar c1 = new DateTime(2010, 1, 1, 0, 0, DateTimeZone.UTC).toCalendar(Locale.ROOT);
    object.put("{'object':'2010-01-01T00:00:00.000Z'}", c1);

    final ToXContent x1 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", 2)
            .array("f3", 3, 4, 5).endObject();
    final ToXContent x2 = (builder, params) -> builder.startObject().field("f1", "v1").field("f2", x1)
            .endObject();//from  w  ww  . ja v  a  2 s .c  om
    object.put("{'object':{'f1':'v1','f2':{'f1':'v1','f2':2,'f3':[3,4,5]}}}", x2);

    for (Map.Entry<String, Object> o : object.entrySet()) {
        final String expected = o.getKey();
        assertResult(expected,
                () -> builder().humanReadable(true).startObject().field("object", o.getValue()).endObject());
        assertResult(expected, () -> builder().humanReadable(true).startObject().field("object")
                .value(o.getValue()).endObject());
    }

    assertResult("{'objects':[null,null,null]}",
            () -> builder().startObject().array("objects", null, null, null).endObject());
}

From source file:org.elasticsearch.common.xcontent.XContentBuilder.java

License:Apache License

public XContentBuilder value(ReadableInstant date, DateTimeFormatter dateTimeFormatter) throws IOException {
    if (date == null) {
        return nullValue();
    }//from w  w w.j  a v a2  s.co m
    return value(dateTimeFormatter.print(date));
}

From source file:org.elasticsearch.common.xcontent.XContentBuilder.java

License:Apache License

public XContentBuilder value(Date date, DateTimeFormatter dateTimeFormatter) throws IOException {
    if (date == null) {
        return nullValue();
    }//from w ww . j  ava2  s. c om
    return value(dateTimeFormatter.print(date.getTime()));
}

From source file:org.elasticsearch.ingest.common.DateIndexNameProcessor.java

License:Apache License

@Override
public void execute(IngestDocument ingestDocument) throws Exception {
    String date = ingestDocument.getFieldValue(field, String.class);

    DateTime dateTime = null;/*  ww  w.  j  a v a2s .  com*/
    Exception lastException = null;
    for (Function<String, DateTime> dateParser : dateFormats) {
        try {
            dateTime = dateParser.apply(date);
        } catch (Exception e) {
            //try the next parser and keep track of the exceptions
            lastException = ExceptionsHelper.useOrSuppress(lastException, e);
        }
    }

    if (dateTime == null) {
        throw new IllegalArgumentException("unable to parse date [" + date + "]", lastException);
    }

    DateTimeFormatter formatter = DateTimeFormat.forPattern(indexNameFormat);
    StringBuilder builder = new StringBuilder().append('<').append(indexNamePrefix).append('{')
            .append(formatter.print(dateTime)).append("||/").append(dateRounding).append('{')
            .append(indexNameFormat).append('|').append(timezone).append('}').append('}').append('>');
    String dynamicIndexName = builder.toString();
    ingestDocument.setFieldValue(IngestDocument.MetaData.INDEX.getFieldName(), dynamicIndexName);
}