Java SQL Time Create getTimeFromCalendar(final Calendar calendar)

Here you can find the source of getTimeFromCalendar(final Calendar calendar)

Description

A utility to return the Time part of a calendar.

License

Open Source License

Parameter

Parameter Description
calendar a parameter

Return

java.sql.Time

Declaration


public static Time getTimeFromCalendar(final Calendar calendar) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.sql.Time;

import java.util.Calendar;

public class Main {
    /***********************************************************************************************
     * A utility to return the Time part of a calendar.
     * The Date part is set to 1970-01-01.
     *//w w  w.jav  a  2  s. c  om
     * @param calendar
     *
     * @return java.sql.Time
     */

    public static Time getTimeFromCalendar(final Calendar calendar) {
        // Parse the Time back again to avoid more problems with Java Dates...
        // Specifically, Time.toString() doesn't give the same hours back!
        return (Time.valueOf(toTimeString(calendar)));
    }

    /***********************************************************************************************
     * Show the Time value of a Calendar as a String.
     * This is putting right more problems with Java Dates...
     *
     * @param calendar
     *
     * @return String
     */

    public static String toTimeString(final Calendar calendar) {
        String strHour;
        String strMinute;
        String strSecond;

        strHour = "";
        strMinute = "";
        strSecond = "";

        if (calendar != null) {
            if (calendar.get(Calendar.HOUR_OF_DAY) < 10) {
                strHour = "0" + calendar.get(Calendar.HOUR_OF_DAY);
            } else {
                strHour = Integer.toString(calendar
                        .get(Calendar.HOUR_OF_DAY));
            }

            if (calendar.get(Calendar.MINUTE) < 10) {
                strMinute = "0" + calendar.get(Calendar.MINUTE);
            } else {
                strMinute = Integer.toString(calendar.get(Calendar.MINUTE));
            }

            if (calendar.get(Calendar.SECOND) < 10) {
                strSecond = "0" + calendar.get(Calendar.SECOND);
            } else {
                strSecond = Integer.toString(calendar.get(Calendar.SECOND));
            }
        }

        return (strHour + ":" + strMinute + ":" + strSecond);
    }
}

Related

  1. getTime(ResultSet rs, String column)
  2. getTime(String time)
  3. getTimeAndDateStamp(String line)
  4. getTimeBy(int hour)
  5. getTimeFormat()
  6. getTimeList(ResultSet resultSet, String columnName)
  7. getTimeLong(ResultSet resultSet)
  8. getTimeNumberFromSqlTime(Time time)
  9. getTimesByStr(String dataStr)