Java SQL Time roundSmallDateTimeValue(Object value)

Here you can find the source of roundSmallDateTimeValue(Object value)

Description

Utility method for a datetime value

License

Open Source License

Parameter

Parameter Description
value a parameter

Declaration

public static Object roundSmallDateTimeValue(Object value) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

public class Main {
    /**/*w ww  .j a  v a 2s.  c  o  m*/
     * Utility method for a datetime value
     * 
     * @param value
     * @return
     */
    public static Object roundSmallDateTimeValue(Object value) {
        if (value == null) {
            return null;
        }

        Calendar cal;
        java.sql.Timestamp ts = null;
        int nanos = -1;

        if (value instanceof Calendar) {
            cal = (Calendar) value;
        } else {
            ts = (java.sql.Timestamp) value;
            cal = Calendar.getInstance();
            cal.setTimeInMillis(ts.getTime());
            nanos = ts.getNanos();
        }

        // round to the nearest minute
        double seconds = cal.get(Calendar.SECOND)
                + (nanos == -1 ? ((double) cal.get(Calendar.MILLISECOND) / 1000) : ((double) nanos / 1000000000));
        if (seconds > 29.998) {
            cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 1);
        }
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        nanos = 0;

        // required to force computation
        cal.getTimeInMillis();

        // return appropriate value
        if (value instanceof Calendar) {
            return cal;
        } else {
            ts.setTime(cal.getTimeInMillis());
            ts.setNanos(nanos);
            return ts;
        }
    }
}

Related

  1. readTime(ByteBuffer buffer)
  2. removeTimefromDate(Date inputDate)
  3. resetDayTime(Time date)
  4. resetNetworkTimeout(Connection rawConnection, Executor executor, int oldTimeout)
  5. roundDatetimeValue(Object value)
  6. saveGameScores(String gameName, String player1, String player2, String player3, String player4, short score1, short score2, short score3, short score4, java.util.Date startTime)
  7. serialize(LocalDateTime date)
  8. serializeSqlTime(Time time)
  9. setTime(PreparedStatement statement, int index, Date value)