/** * Returns an encoding of the given {@link Temporal} that complies with <code>xs:date</code>. * <p> * The returned format is <code<YYYY-MM-DD[TZ]</code>. The timezone is used from the {@link Temporal} object, if * time zone is unknown {@link Temporal#isTimeZoneUnknown()}, no time zone information is appended (= local * time). * </p> * * @param date * point in time to be encoded, must not be <code>null</code> * @return encoded <code>xs:date</code>, never <code>null</code> */ public static String formatDate( Temporal date ) { TimeZone tz = date.getCalendar().getTimeZone(); SimpleDateFormat sdf = new SimpleDateFormat( ISO_8601_2004_FORMAT_DATE ); sdf.setTimeZone( tz ); String s = sdf.format( date.getDate() ); if ( !date.isTimeZoneUnknown() ) { s += getTzString( tz, date.getTimeInMilliseconds() ); } return s; }
/** * Returns an encoding of the given {@link Temporal} that complies with <code>xs:dateTime</code>. * <p> * The returned format is <code<YYYY-MM-DDThh:mm:ss[.SSS][TZ]</code>. The timezone is used from the * {@link Temporal} object, if time zone is unknown {@link Temporal#isTimeZoneUnknown()}, no time zone * information is appended (= local time). * </p> * * @param date * point in time to be encoded, must not be <code>null</code> * @return encoded <code>xs:dateTime</code>, never <code>null</code> */ public static String formatDateTime( Temporal date ) { TimeZone tz = date.getCalendar().getTimeZone(); SimpleDateFormat sdf = null; if ( date.getCalendar().get( MILLISECOND ) == 0 ) { sdf = new SimpleDateFormat( ISO_8601_2004_FORMAT_DATE_TIME_NO_MS ); } else { sdf = new SimpleDateFormat( ISO_8601_2004_FORMAT_DATE_TIME ); } sdf.setTimeZone( tz ); String s = sdf.format( date.getDate() ); if ( !date.isTimeZoneUnknown() ) { s += getTzString( tz, date.getTimeInMilliseconds() ); } return s; }
/** * Returns an encoding of the given {@link Temporal} that complies with <code>xs:time</code>. * <p> * The returned format is <code>hh:mm:ss[.SSS][TZ]</code>. The timezone is used from the {@link Temporal} object, * if time zone is unknown {@link Temporal#isTimeZoneUnknown()}, no time zone information is appended (= local * time). * </p> * * @param date * point in time to be encoded, must not be <code>null</code> * @return encoded <code>xs:date</code>, never <code>null</code> */ public static String formatTime( Temporal date ) { TimeZone tz = date.getCalendar().getTimeZone(); SimpleDateFormat sdf = null; if ( date.getCalendar().get( MILLISECOND ) == 0 ) { sdf = new SimpleDateFormat( ISO_8601_2004_FORMAT_TIME_NO_MS ); } else { sdf = new SimpleDateFormat( ISO_8601_2004_FORMAT_TIME ); } sdf.setTimeZone( tz ); String s = sdf.format( date.getDate() ); if ( !date.isTimeZoneUnknown() ) { s += getTzString( tz, date.getTimeInMilliseconds() ); } return s; }