Java Regex Date Vaidate parseDate(String dateStr)

Here you can find the source of parseDate(String dateStr)

Description

parse Date

License

Open Source License

Declaration

public static Calendar parseDate(String dateStr) 

Method Source Code


//package com.java2s;
/*/*from   w  w w . j a  va  2s.  com*/
 * This file is part of Pustefix.
 *
 * Pustefix 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 2 of the License, or
 * (at your option) any later version.
 *
 * Pustefix 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 Pustefix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    final static Pattern DATE_PATTERN = Pattern.compile("new Date\\((0|[1-9][0-9]*)\\)");
    final static Pattern DATE_UTC_PATTERN = Pattern
            .compile("new Date\\(Date.UTC\\(((0|[1-9]([0-9])*)(,(0|[1-9]([0-9])*))*)\\)\\)");
    final static Pattern COMMA_SPLIT_PATTERN = Pattern.compile(",");
    final static int[] CALENDAR_FIELDS = { Calendar.YEAR, Calendar.MONTH, Calendar.DATE, Calendar.HOUR_OF_DAY,
            Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND };

    public static Calendar parseDate(String dateStr) {
        Calendar cal = parseUTCDate(dateStr);
        if (cal == null)
            cal = parseUTCFuncDate(dateStr);
        return cal;
    }

    public static Calendar parseUTCDate(String dateStr) {
        Matcher mat = DATE_PATTERN.matcher(dateStr);
        if (mat.matches()) {
            long time = Long.parseLong(mat.group(1));
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(time);
            return cal;
        }
        return null;
    }

    public static Calendar parseUTCFuncDate(String dateStr) {
        Calendar cal = Calendar.getInstance();
        Matcher mat = DATE_UTC_PATTERN.matcher(dateStr);
        if (mat.matches()) {
            String[] vals = COMMA_SPLIT_PATTERN.split(mat.group(1));
            for (int i = 0; i < vals.length; i++) {
                int val = Integer.parseInt(vals[i]);
                cal.set(CALENDAR_FIELDS[i], val);
            }
        }
        return cal;
    }
}

Related

  1. dateTimeMatcher(String dt)
  2. dateTimePattern()
  3. dateToString(Date date)
  4. parseDate(final String date, final DateTimeFormatter parser)
  5. parseDate(String date)
  6. parseDate(String entry)
  7. parseDate(String text)
  8. parseDateDiff(final String time)
  9. parseDateDiff(String time)