Java SQL Time serializeSqlTime(Time time)

Here you can find the source of serializeSqlTime(Time time)

Description

Serialize time to standard text.

License

Open Source License

Parameter

Parameter Description
time time to be converted

Return

converted time text

Declaration

public static String serializeSqlTime(Time time) 

Method Source Code

//package com.java2s;

import java.sql.Time;

public class Main {
    /** Number of milliseconds in a minute. */
    private static final int MSPERMINUTE = 60000;
    /** Number of milliseconds in an hour. */
    private static final int MSPERHOUR = MSPERMINUTE * 60;

    /**/*from w  w w.  jav a 2  s. co  m*/
     * Serialize time to standard text. Time values are formatted in W3C XML
     * Schema standard format as hh:mm:ss, with optional trailing seconds
     * decimal, as necessary. The standard conversion does not append a time
     * zone indication.
     *
     * @param time time to be converted
     * @return converted time text
     */
    public static String serializeSqlTime(Time time) {
        StringBuffer buff = new StringBuffer(12);
        serializeTime((int) time.getTime(), buff);
        return buff.toString();
    }

    /**
     * Serialize time to general time text in buffer. Time values are formatted
     * in W3C XML Schema standard format as hh:mm:ss, with optional trailing
     * seconds decimal, as necessary. This form uses a supplied buffer to
     * support flexible use, including with dateTime combination values.
     *
     * @param time time to be converted, as milliseconds in day
     * @param buff buffer for appending time text
     */
    public static void serializeTime(int time, StringBuffer buff) {

        // append the hour, minute, and second
        formatTwoDigits(time / MSPERHOUR, buff);
        time = time % MSPERHOUR;
        buff.append(':');
        formatTwoDigits(time / MSPERMINUTE, buff);
        time = time % MSPERMINUTE;
        buff.append(':');
        formatTwoDigits(time / 1000, buff);
        time = time % 1000;

        // check if decimals needed on second
        if (time > 0) {
            buff.append('.');
            buff.append(time / 100);
            time = time % 100;
            if (time > 0) {
                buff.append(time / 10);
                time = time % 10;
                if (time > 0) {
                    buff.append(time);
                }
            }
        }
    }

    /**
     * Format a positive number as two digits. This uses an optional leading
     * zero digit for values less than ten.
     *
     * @param value number to be formatted (<code>0</code> to <code>99</code>)
     * @param buff text formatting buffer
     */
    protected static void formatTwoDigits(int value, StringBuffer buff) {
        if (value < 10) {
            buff.append('0');
        }
        buff.append(value);
    }
}

Related

  1. resetNetworkTimeout(Connection rawConnection, Executor executor, int oldTimeout)
  2. roundDatetimeValue(Object value)
  3. roundSmallDateTimeValue(Object value)
  4. saveGameScores(String gameName, String player1, String player2, String player3, String player4, short score1, short score2, short score3, short score4, java.util.Date startTime)
  5. serialize(LocalDateTime date)
  6. setTime(PreparedStatement statement, int index, Date value)
  7. sqlDateTimeAdd(java.util.Date date, long milliseconds)
  8. sqlTime()
  9. str2dateTime(String handedate)