public static String format(Date date) { return format(date, Z); }
public static Date toDate(String s) { return new DateIso8601().parse(s); } }
public static long getTimeStamp(int year, int month, int day, int hour, int minute, int second, int milliseconds) { long timestamp = dateToMillis(year, month, day) + timeToMillis(hour, minute, second); return timestamp + milliseconds; }
public static long dateToMillis(int year, int month, int day) { if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) { int[] daysToMonth = daysToMonth(year); if (day >= 1 && day <= daysToMonth[month] - daysToMonth[month - 1]) { return (daysToYear(year) + daysToMonth[month - 1] + day - 1) * 86400000L; } } throw new IllegalArgumentException(); }
int i = length == chars.length ? 0 : StringReflection.offset(s); int year, month, day; year = getInt4(chars, i); i = 4; char c = chars[i]; month = getInt2(chars, i, c); i += 2; day = getInt2(chars, i, c); i += 2; return new Date(dateToMillis(year, month, day)); checkOr(chars[i], 'T', ' '); hour = getInt2(chars, i + 1); long timeStamp = getTimeStamp(year, month, day, hour, 0, 0, 0); return new Date(timeStamp - tz.getOffset(timeStamp)); if (isInt(c)) { minute = getInt2(chars, i, c); i += 2; long timeStamp = getTimeStamp(year, month, day, hour, minute, 0, 0); return new Date(timeStamp - tz.getOffset(timeStamp)); if (isInt(c)) { second = getInt2(chars, i, c);
if (days < daysToYear(year)) year--; days -= daysToYear(year); day = days - daysToMonth(year, month) + 1; if (day <= 0) day = days - daysToMonth(year, month - 1) + 1; else { month++; if (day > 28) { int temp = days - daysToMonth(year, month) + 1; if (temp > 0) { day = temp; int dd = days - daysToYear(year); if (dd <= 0 || dd >= (isLeapYear(year) ? 366 : 365)) dd = days - daysToYear(--year); day = days - daysToMonth(year, month); if (day <= 0) day = days - daysToMonth(year, month - 1); else { month++; if (day > 28) { int temp = days - daysToMonth(year, month); if (temp > 0) { day = temp;
@Test public void testFormat() { TimeZone timeZone = TimeZone.getTimeZone("Europe/Berlin"); assertEquals("1900-10-19T00:00:00.000Z", format(new DateIso8601().parse("1900-10-19"))); assertEquals("1901-01-01T00:00:00.000Z", format(new DateIso8601().parse("1901-01-01"))); assertEquals("1901-01-01T23:59:59.999Z", format(new DateIso8601().parse("1901-01-01T23:59:59.999Z"))); assertEquals("1900-01-01T14:30:10.123Z", format(new DateIso8601().parse("1900-01-01T14:30:10.123Z"))); assertEquals("1900-12-31T14:30:10.123Z", format(new DateIso8601().parse("1900-12-31T14:30:10.123Z"))); assertEquals("2007-12-31T14:30:10.123Z", format(new DateIso8601().parse("2007-12-31T14:30:10.123Z"))); assertEquals("2007-01-01T14:30:10.123Z", format(new DateIso8601().parse("2007-01-01T14:30:10.123Z"))); assertEquals("2007-01-01T23:59:59.999Z", format(new DateIso8601().parse("2007-01-01T23:59:59.999Z"))); assertEquals("2007-01-01T00:00:00.000Z", format(new DateIso8601().parse("2007-01-01"))); assertEquals("2007-04-05T19:00:10.123+0200", format(new DateIso8601().parse("2007-04-05T14:30:10.123-0230"), timeZone)); String s = "2007-04-05T14:30:10.123Z"; assertEquals(s, format(new DateIso8601().parse(s))); s = "2007-04-05T14:30:10.123-0230"; assertEquals("2007-04-05T17:00:10.123Z", format(new DateIso8601().parse(s))); assertEquals("2007-04-05T19:00:10.123+0200", format(new DateIso8601().parse(s), timeZone)); for (int i = 1900; i <= 2100; i++) { assertEquals(i + "-01-01T14:30:10.123Z", format(new DateIso8601().parse(i + "-01-01T14:30:10.123Z"))); assertEquals(i + "-12-31T14:30:10.123Z", format(new DateIso8601().parse(i + "-12-31T14:30:10.123Z"))); assertEquals(i + "-06-15T14:30:10.123Z", format(new DateIso8601().parse(i + "-06-15T14:30:10.123Z"))); assertEquals(i + "-02-28T14:30:10.123Z", format(new DateIso8601().parse(i + "-02-28T14:30:10.123Z"))); Date date = new DateIso8601().parse(i + "-02-05T23:30:10.123Z"); if (timeZone.inDaylightTime(date)) assertEquals(i + "-02-06T01:30:10.123+0200", format(date, timeZone)); else assertEquals(i + "-02-06T00:30:10.123+0100", format(date, timeZone)); } }
private static int daysToMonth(int year, int month) { return daysToMonth(year)[month]; }
int i = length == chars.length ? 0 : StringReflection.offset(s); int year, month, day; year = getInt4(chars, i); i = 4; char c = chars[i]; month = getInt2(chars, i, c); i += 2; day = getInt2(chars, i, c); i += 2; return new Date(dateToMillis(year, month, day)); checkOr(chars[i], 'T', ' '); hour = getInt2(chars, i + 1); long timeStamp = getTimeStamp(year, month, day, hour, 0, 0, 0); return new Date(timeStamp - tz.getOffset(timeStamp)); if (isInt(c)) { minute = getInt2(chars, i, c); i += 2; long timeStamp = getTimeStamp(year, month, day, hour, minute, 0, 0); return new Date(timeStamp - tz.getOffset(timeStamp)); if (isInt(c)) { second = getInt2(chars, i, c);
if (days < daysToYear(year)) year--; days -= daysToYear(year); day = days - daysToMonth(year, month) + 1; if (day <= 0) day = days - daysToMonth(year, month - 1) + 1; else { month++; if (day > 28) { int temp = days - daysToMonth(year, month) + 1; if (temp > 0) { day = temp; int dd = days - daysToYear(year); if (dd <= 0 || dd >= (isLeapYear(year) ? 366 : 365)) dd = days - daysToYear(--year); day = days - daysToMonth(year, month); if (day <= 0) day = days - daysToMonth(year, month - 1); else { month++; if (day > 28) { int temp = days - daysToMonth(year, month); if (temp > 0) { day = temp;
public static long dateToMillis(int year, int month, int day) { if (year >= 1 && year <= 9999 && month >= 1 && month <= 12) { int[] daysToMonth = daysToMonth(year); if (day >= 1 && day <= daysToMonth[month] - daysToMonth[month - 1]) { return (daysToYear(year) + daysToMonth[month - 1] + day - 1) * 86400000L; } } throw new IllegalArgumentException(); }
private static int daysToMonth(int year, int month) { return daysToMonth(year)[month]; }
public static String format(Date date) { return format(date, Z); }
@Test public void test_2() { TimeZone tz = TimeZone.getTimeZone("Europe/Berlin"); TimeZone gmt = TimeZone.getTimeZone("GMT"); for (int i = 1900; i <= 2100; i++) { assertEquals(create(i, 1, 5, 14, 30, 10, 123, tz), new DateIso8601(tz).parse(i + "-01-05T14:30:10.123")); assertEquals(create(i, 7, 5, 14, 30, 10, 123, tz), new DateIso8601(tz).parse(i + "-07-05T14:30:10.123")); assertEquals(create(i, 1, 5, 14, 30, 10, 123), new DateIso8601(gmt).parse(i + "-01-05T14:30:10.123")); assertEquals(create(i, 7, 5, 14, 30, 10, 123), new DateIso8601(gmt).parse(i + "-07-05T14:30:10.123")); assertEquals(create(i, 1, 5, 14, 30, 10, 123), new DateIso8601(tz).parse(i + "-01-05T14:30:10.123Z")); assertEquals(create(i, 7, 5, 14, 30, 10, 123), new DateIso8601(tz).parse(i + "-07-05T14:30:10.123Z")); } }
public static long getTimeStamp(int year, int month, int day, int hour, int minute, int second, int milliseconds) { long timestamp = dateToMillis(year, month, day) + timeToMillis(hour, minute, second); return timestamp + milliseconds; }
@Test public void testFormatComplex() { TimeZone timeZone = TimeZone.getTimeZone("Europe/Berlin"); Calendar c = GregorianCalendar.getInstance(); c.setTimeZone(timeZone); c.setTime(create(1900, 1, 1, 3, 4, 5, 6)); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); simpleDateFormat.setTimeZone(timeZone); for (int i = 0; i < 365 * 200; i++) { assertEquals(simpleDateFormat.format(c.getTime()), DateIso8601.format(c.getTime(), timeZone)); c.add(Calendar.DATE, 1); } } }
@Test public void test() { TimeZone tz = TimeZone.getTimeZone("Europe/Berlin"); assertEquals(create(2007, 4, 5, 12, 0, 10, 123), new DateIso8601(tz).parse("2007-04-05T14:30:10.123+02:30")); assertEquals(create(2007, 4, 5, 17, 0, 10, 123), new DateIso8601(tz).parse("2007-04-05T14:30:10.123-0230")); assertEquals(create(2007, 4, 5, 14, 30, 10, 123), new DateIso8601(tz).parse("2007-04-05T14:30:10.123Z")); assertEquals(create(2007, 4, 5, 12, 30, 10, 123), new DateIso8601(tz).parse("2007-04-05T14:30:10.123")); assertEquals(create(2007, 4, 5, 14, 30, 10, 0), new DateIso8601(tz).parse("2007-04-05T14:30:10Z")); assertEquals(create(2007, 4, 5, 12, 30, 10, 0), new DateIso8601(tz).parse("2007-04-05T14:30:10")); assertEquals(create(2007, 4, 5, 14, 30, 10, 0), new DateIso8601(tz).parse("20070405T143010Z")); assertEquals(create(2007, 4, 5, 12, 30, 10, 0), new DateIso8601(tz).parse("20070405T143010")); assertEquals(create(2007, 4, 5, 14, 30, 0, 0), new DateIso8601(tz).parse("2007-04-05T14:30Z")); assertEquals(create(2007, 4, 5, 12, 30, 0, 0), new DateIso8601(tz).parse("2007-04-05T14:30")); assertEquals(create(2007, 4, 5, 14, 30, 0, 0), new DateIso8601(tz).parse("20070405T1430Z")); assertEquals(create(2007, 4, 5, 12, 30, 0, 0), new DateIso8601(tz).parse("20070405T1430")); assertEquals(create(2007, 4, 5, 14, 0, 0, 0), new DateIso8601(tz).parse("2007-04-05T14Z")); assertEquals(create(2007, 4, 5, 12, 0, 0, 0), new DateIso8601(tz).parse("2007-04-05T14")); assertEquals(create(2007, 4, 5, 14, 0, 0, 0), new DateIso8601(tz).parse("20070405T14Z")); assertEquals(create(2007, 4, 5, 12, 0, 0, 0), new DateIso8601(tz).parse("20070405T14")); assertEquals(create(2007, 4, 5, 12, 0, 0, 0), new DateIso8601(tz).parse("20070405T14+02")); assertEquals(create(2007, 4, 5, 0, 0, 0, 0), new DateIso8601(tz).parse("2007-04-05")); assertEquals(create(2007, 4, 5, 0, 0, 0, 0), new DateIso8601(tz).parse("20070405")); }
@Test public void test_handle_dir() throws IOException, InterruptedException, NoSuchMethodException, NoSuchFieldException, ClassNotFoundException { File subDir = new File(testDir, "sub"); subDir.mkdirs(); FileTools.text(new File(subDir, "foo"), "foo"); FileTools.text(new File(subDir, "bar"), "bar"); handler = new FileTreeHandler(testDir, ""); Assert.assertEquals("<!DOCTYPE html><html><header><meta charset=\"utf-8\">" + "<title>/sub/</title>" + "</meta></header>" + "<body>" + "<h1><a href=\"/\">root: </a><a href=\"/\"></a>/<a href=\"/sub/\">sub</a>/</h1>" + "<table border=\"0\">" + "<tr><th><a href=\"/sub/?sort=name&order=desc\">Name</a></th><th><a href=\"/sub/?sort=modified&order=desc\">Last modified</a></th><th><a href=\"/sub/?sort=size&order=desc\">Size</a></th></tr>\n" + "<tr><td><a href=\"bar\">bar</a></td><td>" + DateIso8601.format(new Date(new File(subDir, "bar").lastModified())) + "</td><td align=\"right\">3</td></tr>\n" + "<tr><td><a href=\"foo\">foo</a></td><td>" + DateIso8601.format(new Date(new File(subDir, "foo").lastModified())) + "</td><td align=\"right\">3</td></tr>\n" + "</table></body></html>", makeRequest("/sub").get().asString()); } }
protected Tag render(String path, File dir, File[] files, String sort, String order, T handlerContext) { HtmlBuilder html = new HtmlBuilder(); html.add(header().add(Meta.charset("utf-8").add(title(path)))); html.add(body() .add(createHeader(dir, h(1))) .add(table() .attr("border", "0") .add(createTableHeader(path, sort, order)) .text("\n") .each(files, (file, table) -> { String url = generateUrl(file, handlerContext); table.add(tr() .add(td().add(a().href(url).text(file.getName() + (file.isDirectory() ? "/" : "")))) .add(td().text(DateIso8601.format(new Date(file.lastModified())))) .add(td().attr("align", "right").text(formatFileSize(file.length()))) ).text("\n"); })) ); return html; }