Java String to Date stringToDate(final String dateString)

Here you can find the source of stringToDate(final String dateString)

Description

string To Date

License

Open Source License

Declaration

public static Date stringToDate(final String dateString) 

Method Source Code

//package com.java2s;
/**/*from w w w . j a v a  2 s .c o  m*/
 *
 * Licensed under the GNU General Public License (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.gnu.org/licenses/gpl.txt
 *
 * 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.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

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

public class Main {
    private static String[] formats = { "yyyy-MM-dd'T'HH:mm:ssz", //
            "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "MM/dd/yyyy", "MM/dd/yyyy h:mm a" };

    public static Date stringToDate(final String dateString) {
        Date ret = null;
        for (final String format : formats) {
            try {
                final SimpleDateFormat frm = new SimpleDateFormat(format);
                ret = frm.parse(dateString);
                break;
            } catch (final ParseException e) {
                continue;
            }
        }
        return ret;
    }
}

Related

  1. strDateConvertToDate(String strDate, String fFormatStr)
  2. stringArrayTODateArray(String[] dates_s, SimpleDateFormat dateformat)
  3. stringDateToDate(String str)
  4. stringDateToDate(String StrDate)
  5. stringFechaToDate(String fechaS)
  6. stringToDate(final String dateTimeStr)
  7. stringToDate(final String str, final String dateFormat)
  8. stringToDate(final String string, final String format)
  9. stringToDate(String data)