Java SQL Time getFullDateTime(ResultSet rs, String column)

Here you can find the source of getFullDateTime(ResultSet rs, String column)

Description

Returns the value of a DATETIME column in the current row of an SQL result set, formatted as a date.

License

Mozilla Public License

Parameter

Parameter Description
rs The result set to look in.
column The name of the column to be returned.

Return

The value of the specified column, expressed as a date.

Declaration

public static final java.util.Date getFullDateTime(ResultSet rs, String column) throws SQLException 

Method Source Code


//package com.java2s;
/*//from  ww w  .  j  a va2  s  .co m
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@users.sf.net>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2006 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */

import java.sql.*;
import java.util.*;

public class Main {
    /** Used to convert dates and times to UTC for sending to SQL. */
    private static final SimpleTimeZone utc = new SimpleTimeZone(0, "UTC");

    /**
     * Returns the value of a DATETIME column in the current row of an SQL result set, formatted as a date.
     *
     * @param rs The result set to look in.
     * @param column The name of the column to be returned.
     * @return The value of the specified column, expressed as a date.
     * @exception java.sql.SQLException If the column could not be retrieved or converted.
     */
    public static final java.util.Date getFullDateTime(ResultSet rs, String column) throws SQLException {
        return convertDateTimeString(rs.getString(column));

    }

    /**
     * Returns the value of a DATETIME column in the current row of an SQL result set, formatted as a date.
     *
     * @param rs The result set to look in.
     * @param column The 1-based index of the column to be returned.
     * @return The value of the specified column, expressed as a date.
     * @exception java.sql.SQLException If the column could not be retrieved or converted.
     */
    public static final java.util.Date getFullDateTime(ResultSet rs, int column) throws SQLException {
        return convertDateTimeString(rs.getString(column));

    }

    /**
     * Converts a date-and-time string (an SQL DATETIME value) to a standard Java date.
     *
     * @param dstr The date-and-time string to convert, presumed to be in UTC.
     * @return The converted date and time.
     * @exception java.sql.SQLException The date and time string was in an invalid format.
     */
    private static final java.util.Date convertDateTimeString(String dstr) throws SQLException {
        if (dstr == null)
            return null; // null values are the same

        try { // do almost the reverse process of formatting it into a string
            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)));
            cal.set(Calendar.MINUTE, Integer.parseInt(dstr.substring(14, 16)));
            cal.set(Calendar.SECOND, Integer.parseInt(dstr.substring(17, 19)));
            return cal.getTime();

        } // end try
        catch (NumberFormatException e) { // the NumberFormatException becomes an SQLException
            throw new SQLException("invalid DATETIME field format");

        } // end catch

    }
}

Related

  1. getClassForSqlType(String sqlType, Class dateTimeClass)
  2. getClosestIDByTime(Connection con, String table, long time)
  3. getDateTimeValue(ResultSet result, String strField, String strDateFormat)
  4. getDBTime(String yyyyMMddHHmmss)
  5. getFormatDateTimeDesc(long longDate)
  6. getFutureTime(int month)
  7. getGrandfatheredTime()
  8. getIntervalDays(Date time1, Date time2)
  9. getJoinedSysDateTime()