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);
}
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);
}
}
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);
}
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()));
}
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));
}
Aggregations