/** * TODO handling of SQL timezone * * @param input * @return */ private java.sql.Time toSqlTime( Object input ) { if ( input instanceof java.sql.Time ) { return (java.sql.Time) input; } java.sql.Time value = null; if ( input instanceof java.util.Date ) { java.util.Date date = (java.util.Date) input; value = new java.sql.Time( date.getTime() ); } else if ( input instanceof Temporal ) { Temporal timeInstant = (Temporal) input; value = new java.sql.Time( timeInstant.getTimeInMilliseconds() ); } else { String s = input.toString(); Time timeInstant = parseTime( s ); value = toSqlTime( timeInstant ); } return value; }
/** * TODO handling of SQL timezone * * @param input * @return */ protected java.sql.Date toSqlDate( Object input ) { if ( input instanceof java.sql.Date ) { return (java.sql.Date) input; } java.sql.Date value = null; if ( input instanceof java.util.Date ) { java.util.Date date = (java.util.Date) input; value = new java.sql.Date( date.getTime() ); } else if ( input instanceof Temporal ) { Temporal timeInstant = (Temporal) input; value = new java.sql.Date( timeInstant.getTimeInMilliseconds() ); } else { String s = input.toString(); Date timeInstant = parseDate( s ); value = toSqlDate( timeInstant ); } return value; }
/** * TODO handling of SQL timezone * * @param input * @return */ protected Timestamp toSqlTimestamp( Object input ) { if ( input instanceof Timestamp ) { return (Timestamp) input; } Timestamp value = null; if ( input instanceof java.util.Date ) { java.util.Date date = (java.util.Date) input; value = new Timestamp( date.getTime() ); } else if ( input instanceof Temporal ) { Temporal timeInstant = (Temporal) input; value = new Timestamp( timeInstant.getTimeInMilliseconds() ); } else { String s = input.toString(); if ( s.isEmpty() ) { return null; } DateTime timeInstant = parseDateTime( s ); value = toSqlTimestamp( timeInstant ); } return value; }
case DATE: sqlValue = new java.sql.Date( ( (Temporal) value ).getTimeInMilliseconds() ); break; case DATE_TIME: sqlValue = new Timestamp( ( (Temporal) value ).getTimeInMilliseconds() ); break; case TIME: sqlValue = new Time( ( (Temporal) value ).getTimeInMilliseconds() ); break; case DECIMAL:
/** * 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; }