Class Utc


  • public final class Utc
    extends Object
    UTC time zone manipulator.

    When it's necessary to save date/time to the DB in UTC timezone, use this class:

     new JdbcSession(source)
       .sql("INSERT INTO payment (amount, date) VALUES (?, ?)")
       .set(500)
       .set(new Utc()) // current date to be set, in UTC timezone
       .insert(Outcome.VOID);

    This class also helps during date/time retrieval:

     Date date = new JdbcSession(source)
       .sql("SELECT date FROM payment WHERE id = 555")
       .select(
         new Outcome<Date>() {
           @Override
           public Date handle(final ResultSet rset) throws SQLException {
             return Utc.getTimestamp(rset, 1);
           }
         }
       );

    Timestamp is used because Date supports only dates (without time).

    Since:
    0.1.8
    • Constructor Detail

      • Utc

        public Utc()
        Public ctor, with current date.
      • Utc

        public Utc​(Date when)
        Public ctor.
        Parameters:
        when - The date to use.
    • Method Detail

      • getDate

        public Date getDate()
        Get date that is encapsulated.
        Returns:
        The date
      • setTimestamp

        public void setTimestamp​(PreparedStatement stmt,
                                 int pos)
                          throws SQLException
        Convert date to timestamp and save to the statement.
        Parameters:
        stmt - The statement
        pos - Position in the statement
        Throws:
        SQLException - If some SQL problem inside
      • getTimestamp

        public static Date getTimestamp​(ResultSet rset,
                                        int pos)
                                 throws SQLException
        Retrieve timestamp from the result set.
        Parameters:
        rset - The result set
        pos - Position in the result set
        Returns:
        The date
        Throws:
        SQLException - If some SQL problem inside