Java SQL Time createTime(int hour, int minute, int second)

Here you can find the source of createTime(int hour, int minute, int second)

Description

Replaces deprecated java.sql.Time constructor.

License

Open Source License

Parameter

Parameter Description
hour 0 to 23
minute 0 to 59
second 0 to 59

Return

A Time

Declaration

public static Time createTime(int hour, int minute, int second) 

Method Source Code

//package com.java2s;
/*/*w  w  w.j  a  v  a2 s.  com*/
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

import java.sql.Time;

import java.util.Calendar;
import java.util.TimeZone;

public class Main {
    private static ThreadLocal<Calendar> CAL = new ThreadLocal<Calendar>() {
        @Override
        protected Calendar initialValue() {
            return Calendar.getInstance();
        }
    };

    /**
     * Replaces deprecated <code>java.sql.Time</code> constructor.
     *
     * @param hour 0 to 23
     * @param minute 0 to 59
     * @param second 0 to 59
     * @see java.sql.Time
     * @return A <code>Time</code>
     */
    public static Time createTime(int hour, int minute, int second) {
        primeCalendar();
        CAL.get().set(Calendar.HOUR_OF_DAY, hour);
        CAL.get().set(Calendar.MINUTE, minute);
        CAL.get().set(Calendar.SECOND, second);
        return new Time(CAL.get().getTimeInMillis());
    }

    private static void primeCalendar() {
        CAL.get().setTimeZone(TimeZone.getDefault());
        CAL.get().clear();
    }
}

Related

  1. compareTime(String time1, String time2)
  2. dateTime2str(java.sql.Date date)
  3. dateTimeFormat(Date date,String pattern)
  4. DateTimeSpace(String starttime, String endtime)
  5. dateTimeToStr(java.util.Date date)