Search in sources :

Example 36 with Date

use of java.util.Date in project jetty.project by eclipse.

the class JSONTest method testConvertor.

/* ------------------------------------------------------------ */
@Test
public void testConvertor() {
    // test case#1 - force timezone to GMT
    JSON json = new JSON();
    json.addConvertor(Date.class, new JSONDateConvertor("MM/dd/yyyy HH:mm:ss zzz", TimeZone.getTimeZone("GMT"), false));
    json.addConvertor(Object.class, new JSONObjectConvertor());
    Woggle w0 = new Woggle();
    Gizmo g0 = new Gizmo();
    w0.name = "woggle0";
    w0.nested = g0;
    w0.number = 100;
    g0.name = "woggle1";
    g0.nested = null;
    g0.number = -101;
    g0.tested = true;
    HashMap map = new HashMap();
    Date dummyDate = new Date(1);
    map.put("date", dummyDate);
    map.put("w0", w0);
    StringBuffer buf = new StringBuffer();
    json.append(buf, map);
    String js = buf.toString();
    assertTrue(js.indexOf("\"date\":\"01/01/1970 00:00:00 GMT\"") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
    assertTrue(js.indexOf("\"tested\":true") >= 0);
    // test case#3
    TimeZone tzone = TimeZone.getTimeZone("JST");
    String tzone3Letter = tzone.getDisplayName(false, TimeZone.SHORT);
    String format = "EEE MMMMM dd HH:mm:ss zzz yyyy";
    Locale l = new Locale("ja", "JP");
    if (l != null) {
        json.addConvertor(Date.class, new JSONDateConvertor(format, tzone, false, l));
        buf = new StringBuffer();
        json.append(buf, map);
        js = buf.toString();
        //assertTrue(js.indexOf("\"date\":\"木 1月 01 09:00:00 JST 1970\"")>=0);
        assertTrue(js.indexOf(" 01 09:00:00 JST 1970\"") >= 0);
        assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
        assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
        assertTrue(js.indexOf("\"tested\":true") >= 0);
    }
    // test case#4
    json.addConvertor(Date.class, new JSONDateConvertor(true));
    w0.nested = null;
    buf = new StringBuffer();
    json.append(buf, map);
    js = buf.toString();
    assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
    map = (HashMap) json.parse(new JSON.StringSource(js));
    assertTrue(map.get("date") instanceof Date);
    assertTrue(map.get("w0") instanceof Woggle);
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) HashMap(java.util.HashMap) Date(java.util.Date) Test(org.junit.Test)

Example 37 with Date

use of java.util.Date in project jetty.project by eclipse.

the class RolloverFileOutputStream method setFile.

/* ------------------------------------------------------------ */
private synchronized void setFile() throws IOException {
    // Check directory
    File file = new File(_filename);
    _filename = file.getCanonicalPath();
    file = new File(_filename);
    File dir = new File(file.getParent());
    if (!dir.isDirectory() || !dir.canWrite())
        throw new IOException("Cannot write log directory " + dir);
    Date now = new Date();
    // Is this a rollover file?
    String filename = file.getName();
    int i = filename.toLowerCase(Locale.ENGLISH).indexOf(YYYY_MM_DD);
    if (i >= 0) {
        file = new File(dir, filename.substring(0, i) + _fileDateFormat.format(now) + filename.substring(i + YYYY_MM_DD.length()));
    }
    if (file.exists() && !file.canWrite())
        throw new IOException("Cannot write log file " + file);
    // Do we need to change the output stream?
    if (out == null || !file.equals(_file)) {
        // Yep
        _file = file;
        if (!_append && file.exists())
            file.renameTo(new File(file.toString() + "." + _fileBackupFormat.format(now)));
        OutputStream oldOut = out;
        out = new FileOutputStream(file.toString(), _append);
        if (oldOut != null)
            oldOut.close();
    //if(log.isDebugEnabled())log.debug("Opened "+_file);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FilterOutputStream(java.io.FilterOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date)

Example 38 with Date

use of java.util.Date in project jetty.project by eclipse.

the class RFC2616BaseTest method assertDate.

protected void assertDate(String msg, Calendar expectedTime, long actualTime) {
    SimpleDateFormat sdf = new SimpleDateFormat("EEEE, d MMMM yyyy HH:mm:ss:SSS zzz");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    String actual = sdf.format(new Date(actualTime));
    String expected = sdf.format(expectedTime.getTime());
    Assert.assertEquals(msg, expected, actual);
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 39 with Date

use of java.util.Date in project elasticsearch by elastic.

the class SimpleJodaTests method testWriteAndParse.

public void testWriteAndParse() {
    DateTimeFormatter dateTimeWriter = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC);
    DateTimeFormatter formatter = ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC);
    Date date = new Date();
    assertThat(formatter.parseMillis(dateTimeWriter.print(date.getTime())), equalTo(date.getTime()));
}
Also used : DateTimeFormatter(org.joda.time.format.DateTimeFormatter) FormatDateTimeFormatter(org.elasticsearch.common.joda.FormatDateTimeFormatter) Date(java.util.Date)

Example 40 with Date

use of java.util.Date in project elasticsearch by elastic.

the class IngestDocumentTests method testIngestMetadataTimestamp.

public void testIngestMetadataTimestamp() throws Exception {
    long before = System.currentTimeMillis();
    IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
    long after = System.currentTimeMillis();
    Date timestamp = (Date) ingestDocument.getIngestMetadata().get(IngestDocument.TIMESTAMP);
    assertThat(timestamp, notNullValue());
    assertThat(timestamp.getTime(), greaterThanOrEqualTo(before));
    assertThat(timestamp.getTime(), lessThanOrEqualTo(after));
}
Also used : IngestDocumentMatcher.assertIngestDocument(org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument) Date(java.util.Date)

Aggregations

Date (java.util.Date)33499 Test (org.junit.Test)8112 SimpleDateFormat (java.text.SimpleDateFormat)4502 ArrayList (java.util.ArrayList)3177 Calendar (java.util.Calendar)2351 HashMap (java.util.HashMap)1985 IOException (java.io.IOException)1914 File (java.io.File)1649 ParseException (java.text.ParseException)1578 List (java.util.List)1106 DateFormat (java.text.DateFormat)1022 Map (java.util.Map)983 GregorianCalendar (java.util.GregorianCalendar)884 Test (org.junit.jupiter.api.Test)853 HashSet (java.util.HashSet)576 Test (org.testng.annotations.Test)527 Timestamp (java.sql.Timestamp)458 BigDecimal (java.math.BigDecimal)436 LocalDate (java.time.LocalDate)423 DateTime (org.joda.time.DateTime)391