Java Date Parse dateFromString(String value)

Here you can find the source of dateFromString(String value)

Description

date From String

License

Open Source License

Declaration

public static Date dateFromString(String value) 

Method Source Code

//package com.java2s;
/*/*from w ww  . j  a va2 s .  c o m*/
 * (c) Copyright 2010-2011 AgileBirds
 *
 * This file is part of OpenFlexo.
 *
 * OpenFlexo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OpenFlexo 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
 *
 */

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

public class Main {
    /** Specify date format */
    protected static final String _dateFormat = new SimpleDateFormat().toPattern();

    public static Date dateFromString(String value) {
        try {
            StringTokenizer st = new StringTokenizer(value, ",");
            String dateFormat = _dateFormat;
            String dateAsString = null;
            if (st.hasMoreTokens()) {
                dateFormat = st.nextToken();
            }
            if (st.hasMoreTokens()) {
                dateAsString = st.nextToken();
            }
            if (dateAsString != null) {
                return new SimpleDateFormat(dateFormat).parse(dateAsString);
            }
        } catch (ParseException e) {
        }
        SimpleDateFormat formatter = new SimpleDateFormat(_dateFormat);
        Date currentTime = new Date();
        String dateString = formatter.format(currentTime);
        System.err.println(
                "Supplied value is not parsable as a date. " + " Date format should be for example " + dateString);
        return null;
    }
}

Related

  1. dateFromString(String date)
  2. dateFromString(String date, String pattern)
  3. dateFromString(String dateString)
  4. dateFromString(String dateString)
  5. dateFromString(String s)
  6. DateFromStringFormat(final String date, final String format)
  7. dateFromW3CDTF(String date)
  8. dateParse(String date)
  9. dateParse(String date2BeParsed)