Java SQL Date Parse stringToDate(String date, Class dateType)

Here you can find the source of stringToDate(String date, Class dateType)

Description

string To Date

License

Apache License

Declaration

public static Date stringToDate(String date, Class<?> dateType) 

Method Source Code

//package com.java2s;
/************************************************************************
 * Copyright (c) 2014-2015 IoT-Solutions e.U.
 * //  www .j  a  v  a  2 s  . c o m
 * 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.
 ************************************************************************/

import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    private static SimpleDateFormat simpleDateFormat;

    public static Date stringToDate(String date, Class<?> dateType) {
        try {
            Date dat = getSimpleDateFormat().parse(date);
            if (dateType.equals(java.sql.Date.class))
                return new java.sql.Date(dat.getTime());
            else if (dateType.equals(Time.class))
                return new Time(dat.getTime());
            else if (dateType.equals(Timestamp.class))
                return new Timestamp(dat.getTime());
            return dat;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    private static SimpleDateFormat getSimpleDateFormat() {
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy-HH:mm:ss:SSS", new Locale("de", "AT"));
        }
        return simpleDateFormat;
    }
}

Related

  1. parseString2Date(String s, SimpleDateFormat format)
  2. parseUTCDateFromString(String dt)
  3. parseUtilDateToSqlDate(Date date)
  4. stringToDate(final String dateString)
  5. stringToDate(String date)
  6. stringToDate(String dateStr)
  7. stringToDate(String dta)
  8. stringToDate(String str)
  9. stringToDate(String str)