Java Date Parse convertStringToDate(String date)

Here you can find the source of convertStringToDate(String date)

Description

convert String To Date

License

Open Source License

Declaration

public static Date convertStringToDate(String date) 

Method Source Code

//package com.java2s;
/*//from  ww  w . j  a  v a 2s.c o m
Unisens Library - library for a universal sensor data format
Copyright (C) 2008 FZI Research Center for Information Technology, Germany
           Institute for Information Processing Technology (ITIV),
       KIT, Germany
    
This file is part of the Unisens Library. For more information, see
<http://www.unisens.org>
    
The Unisens Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
The Unisens Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with the Unisens Library. If not, see <http://www.gnu.org/licenses/>. 
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

public class Main {
    public static Date convertStringToDate(String date) {
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
            SimpleDateFormat simpleDateFormatWithMs = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
            String pattern = "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}";

            if (Pattern.matches(pattern, date)) {
                return simpleDateFormatWithMs.parse(date);
            }
            return simpleDateFormat.parse(date);
        } catch (ParseException pe) {
            pe.printStackTrace();
            return null;
        }
    }
}

Related

  1. convertStringToDate(String aMask, String strDate)
  2. convertStringToDate(String arg)
  3. convertStringToDate(String data)
  4. convertStringToDate(String date)
  5. convertStringToDate(String date)
  6. convertStringToDate(String dateAsString)
  7. convertStringToDate(String dateFormat, String date)
  8. convertStringToDate(String dateInput, String formatter)
  9. convertStringToDate(String dateStr, String format)