Java SQL Date From convertDateTimeString(String dstr)

Here you can find the source of convertDateTimeString(String dstr)

Description

Converts a date-and-time string (an SQL DATETIME value) to a standard Java date.

License

Mozilla Public License

Parameter

Parameter Description
dstr The date-and-time string to convert, presumed to be in UTC.

Return

The converted date and time.

Declaration

private static final java.util.Date convertDateTimeString(String dstr) throws SQLException 

Method Source Code


//package com.java2s;
/*//www  .  j a v a 2  s .com
 * 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@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002-03 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */

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

public class Main {
    private static SimpleTimeZone utc = new SimpleTimeZone(0, "UTC");

    /**
     * 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. calendarToSqlDate(Calendar c)
  2. calendarToSqlDate(final Calendar dt)
  3. convertDateToTime(Date date, boolean am)
  4. convertDateToTime(final Date date)
  5. convertIntoDuration(Long time)
  6. convertJavaTimeToJavaSql(Object x)