Java Utililty Methods SQL Date From

List of utility methods to do SQL Date From

Description

The list of methods to do SQL Date From are organized into topic(s).

Method

TimestampcalendarToSqlDate(Calendar c)
Converts the given calendar to a SQL Timestamp.
return c == null ? null : new Timestamp(c.getTimeInMillis());
java.sql.DatecalendarToSqlDate(final Calendar dt)
calendar To Sql Date
return new java.sql.Date(dt.getTime().getTime());
java.util.DateconvertDateTimeString(String dstr)
Converts a date-and-time string (an SQL DATETIME value) to a standard Java date.
if (dstr == null)
    return null; 
try { 
    GregorianCalendar cal = new GregorianCalendar(utc);
    cal.set(Calendar.YEAR, Integer.parseInt(dstr.substring(0, 4)));
    cal.set(Calendar.MONTH, Integer.parseInt(dstr.substring(5, 7)) - 1 + Calendar.JANUARY);
    cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(dstr.substring(8, 10)));
    cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(dstr.substring(11, 13)));
...
TimeconvertDateToTime(Date date, boolean am)
The JSF DateTimeConverter can not convert into java.sql.Time (or into java.util.Calendar, for that matter!).
if (date == null) {
    return null;
Calendar cal = new GregorianCalendar();
cal.setTime(date);
int hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
if (am) {
    if (hourOfDay > 11) {
...
TimeconvertDateToTime(final Date date)
Convert date to time.
final Time time = new Time(date.getTime());
time.setSeconds(0);
return time;
StringconvertIntoDuration(Long time)
convert Into Duration
int day = (int) TimeUnit.SECONDS.toDays(time);
long hours = TimeUnit.SECONDS.toHours(time) - (day * 24);
long minute = TimeUnit.SECONDS.toMinutes(time) - (TimeUnit.SECONDS.toHours(time) * 60);
long second = TimeUnit.SECONDS.toSeconds(time) - (TimeUnit.SECONDS.toMinutes(time) * 60);
StringBuilder sb = new StringBuilder();
sb.append(day).append("d ").append(hours).append("h ").append(minute).append("m ").append(second)
        .append("s");
return sb.toString();
...
ObjectconvertJavaTimeToJavaSql(Object x)
JDBC 4.2 Helper methods.
if (x instanceof LocalDate) {
    return Date.valueOf((LocalDate) x);
} else if (x instanceof LocalDateTime) {
    return Timestamp.valueOf((LocalDateTime) x);
} else if (x instanceof LocalTime) {
    return Time.valueOf((LocalTime) x);
return x;
...
TimeconvertNanoToTime(long nanos)
Convert a time value to a time, using the default timezone.
long millis = nanos / 1000000;
long s = millis / 1000;
millis -= s * 1000;
long m = s / 60;
s -= m * 60;
long h = m / 60;
m -= h * 60;
long ms = getMillis(TimeZone.getDefault(), 1970, 1, 1, (int) (h % 24), (int) m, (int) s, (int) millis);
...
StringconvertTime(long time)
Convert time.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(new Timestamp(time));
TimestampconvertTime(String time)
Convert the string to Timestamp.
SimpleDateFormat formatter = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.MONTH_FIELD,
        DateFormat.LONG, Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone(TIME_ZONE_GMT));
Date date = null;
try {
    date = formatter.parse(time);
} catch (ParseException e) {
    date = new Date();
...