Java SQL Date Parse parseUTCDateFromString(String dt)

Here you can find the source of parseUTCDateFromString(String dt)

Description

Translates a string of the form yyyyMMddHHmmss (UTC) into asql Date, and returns that date.

License

Open Source License

Parameter

Parameter Description
dt a String formatted yyyyMMddHHmmss (UTC)

Exception

Parameter Description

Return

the parsed java.util.Date

Declaration

public static java.sql.Timestamp parseUTCDateFromString(String dt) throws ParseException 

Method Source Code

//package com.java2s;
/*//from   ww  w  . ja  v  a 2 s. c  om
 * @(#)TimeUtils.java   0.51 1999/07/14
 *
 * Copyright (c) 2004, Pat Farrell, All rights reserved.
 * based on work Copyright (c) 2000, OneBigCD, Inc.  All rights reserved.
 * Copyright (C) 2011 Patrick Farrell   All Rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * It is abstract because all functions are static
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
    /**
     * Translates a string of the form yyyyMMddHHmmss (UTC) into asql Date, and
     * returns that date.  If validation error occurs, returns null. 
     * Processes both 8 and 12 character string lengths.
     * @param dt a String formatted yyyyMMddHHmmss (UTC)
     * @return the parsed java.util.Date
     * @see java.sql.Date
     * @throws java.text.ParseException pass up any parsing problem
     */
    public static java.sql.Timestamp parseUTCDateFromString(String dt) throws ParseException {
        if (dt == null)
            return null;
        SimpleDateFormat utcDateFormat = (dt.length() > 8) ? new SimpleDateFormat("yyyyMMddHHmmss")
                : new SimpleDateFormat("yyyyMMdd");
        java.util.Date pdate = utcDateFormat.parse(dt);
        return new java.sql.Timestamp(pdate.getTime());
    }
}

Related

  1. parseSqlDate(final String value)
  2. parseSqlDate(java.util.Date date)
  3. parseSqlDate(java.util.Date date)
  4. parseSQLDate(String dateStr, String pattern)
  5. parseString2Date(String s, SimpleDateFormat format)
  6. parseUtilDateToSqlDate(Date date)
  7. stringToDate(final String dateString)
  8. stringToDate(String date)
  9. stringToDate(String date, Class dateType)