/** * Format date to ISO 8601 formatted string * * @param date Date * @return String */ public String toISO8601(Date date) { return ISO8601DateFormat.format(date); }
/** * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ public Object exec(List args) throws TemplateModelException { Object result = null; if (args.size() == 1) { Object arg0 = args.get(0); if (arg0 instanceof TemplateDateModel) { result = ISO8601DateFormat.format(((TemplateDateModel)arg0).getAsDate()); } else if (arg0 instanceof TemplateScalarModel) { result = ISO8601DateFormat.parse(((TemplateScalarModel)arg0).getAsString()); } } return result != null ? result : ""; } }
/** * Parse date from ISO formatted string * * @param isoDateString String * @return Date */ public Date fromISO8601(String isoDateString) { return ISO8601DateFormat.parse(isoDateString); }
padInt(formatted, calendar.get(Calendar.YEAR), 4); formatted.append('-'); padInt(formatted, calendar.get(Calendar.MONTH) + 1, 2); formatted.append('-'); padInt(formatted, calendar.get(Calendar.DAY_OF_MONTH), 2); formatted.append('T'); padInt(formatted, calendar.get(Calendar.HOUR_OF_DAY), 2); formatted.append(':'); padInt(formatted, calendar.get(Calendar.MINUTE), 2); formatted.append(':'); padInt(formatted, calendar.get(Calendar.SECOND), 2); formatted.append('.'); padInt(formatted, calendar.get(Calendar.MILLISECOND), 3); int minutes = Math.abs((offset / (60 * 1000)) % 60); formatted.append(offset < 0 ? '-' : '+'); padInt(formatted, hours, 2); formatted.append(':'); padInt(formatted, minutes, 2);
/** * Parse date from ISO formatted string * * @param isoDateString String * @return Date */ public Date fromISO8601(String isoDateString) { return ISO8601DateFormat.parse(isoDateString); }
/** * Format date to ISO 8601 formatted string * * @param date Date * @return String */ public String toISO8601(Date date) { return ISO8601DateFormat.format(date); }
/** * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ public Object exec(List args) throws TemplateModelException { Object result = null; if (args.size() == 1) { Object arg0 = args.get(0); if (arg0 instanceof TemplateDateModel) { result = ISO8601DateFormat.format(((TemplateDateModel)arg0).getAsDate()); } else if (arg0 instanceof TemplateScalarModel) { result = ISO8601DateFormat.parse(((TemplateScalarModel)arg0).getAsString()); } } return result != null ? result : ""; } }
o = ISO8601DateFormat.parse((String)o);
/** * Format timeInMillis to ISO 8601 formatted string * * @param timeInMillis long * @return String */ public String toISO8601(long timeInMillis) { return ISO8601DateFormat.format(new Date(timeInMillis)); }
/** * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ public Object exec(List args) throws TemplateModelException { Object result = null; if (args.size() == 1) { Object arg0 = args.get(0); if (arg0 instanceof TemplateDateModel) { result = ISO8601DateFormat.format(((TemplateDateModel)arg0).getAsDate()); } else if (arg0 instanceof TemplateScalarModel) { result = ISO8601DateFormat.parse(((TemplateScalarModel)arg0).getAsString()); } } return result != null ? result : ""; } }
o = ISO8601DateFormat.parse((String)o);
/** * Format timeInMillis to ISO 8601 formatted string * * @param timeInMillis long * @return String */ public String toISO8601(long timeInMillis) { return ISO8601DateFormat.format(new Date(timeInMillis)); }
/** * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ public Object exec(List args) throws TemplateModelException { Object result = null; if (args.size() == 1) { Object arg0 = args.get(0); if (arg0 instanceof TemplateDateModel) { result = ISO8601DateFormat.format(((TemplateDateModel)arg0).getAsDate()); } else if (arg0 instanceof TemplateScalarModel) { result = ISO8601DateFormat.parse(((TemplateScalarModel)arg0).getAsString()); } } return result != null ? result : ""; } }
o = ISO8601DateFormat.parse((String)o);
private JSONObject getJsonDate(Date date) throws JSONException { JSONObject isoDate = new JSONObject(); isoDate.put("iso8601", ISO8601DateFormat.format(date)); return isoDate; }
/** * @see freemarker.template.TemplateMethodModel#exec(java.util.List) */ public Object exec(List args) throws TemplateModelException { Object result = null; if (args.size() == 1) { Object arg0 = args.get(0); if (arg0 instanceof TemplateDateModel) { result = ISO8601DateFormat.format(((TemplateDateModel)arg0).getAsDate()); } else if (arg0 instanceof TemplateScalarModel) { result = ISO8601DateFormat.parse(((TemplateScalarModel)arg0).getAsString()); } } return result != null ? result : ""; } }
o = ISO8601DateFormat.parse((String)o);
private JSONObject getJsonDate(Date date) throws JSONException { JSONObject isoDate = new JSONObject(); isoDate.put("iso8601", ISO8601DateFormat.format(date)); return isoDate; }
public void testConversion() { String test = "2005-09-16T17:01:03.456+01:00"; // convert to a date Date date = ISO8601DateFormat.parse(test); // get the string form String strDate = ISO8601DateFormat.format(date); // convert back to a date from the converted string Date dateAfter = ISO8601DateFormat.parse(strDate); // make sure the date objects match, test this instead of the // string as the string form will be different in different // locales assertEquals(date, dateAfter); } }