Here you can find the source of getTimeFromCalendar(final Calendar calendar)
Parameter | Description |
---|---|
calendar | a parameter |
public static Time getTimeFromCalendar(final Calendar calendar)
//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); } }