Java Regex Date Vaidate parseDate(String date)

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

Description

parse Date

License

Apache License

Declaration

public static Date parseDate(String date) 

Method Source Code


//package com.java2s;
/*//w ww  .  ja v  a  2 s. c o m
 *  Copyright 2010 Vodafone Group Services Ltd.
 *
 *  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.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

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

public class Main {
    public static Date parseDate(String date) {
        if (date == null || date.length() == 0) {
            return null;
        }

        Pattern pattern = Pattern.compile(
                "(\\d{4})(?:-(\\d{2}))?(?:-(\\d{2}))?(?:([Tt])?(?:(\\d{2}))?(?::(\\d{2}))?(?::(\\d{2}))?(?:\\.(\\d{3}))?)?([Zz])?(?:([+-])(\\d{2}):(\\d{2}))?");
        Matcher m = pattern.matcher(date);
        if (m.find()) {
            if (m.group(4) == null)
                throw new IllegalArgumentException("Invalid Date Format");
            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
            int hoff = 0, moff = 0, doff = -1;
            if (m.group(10) != null) {
                doff = m.group(10).equals("-") ? 1 : -1;
                hoff = doff * (m.group(11) != null ? Integer.parseInt(m.group(11)) : 0);
                moff = doff * (m.group(12) != null ? Integer.parseInt(m.group(12)) : 0);
            }
            c.set(Calendar.YEAR, Integer.parseInt(m.group(1)));
            c.set(Calendar.MONTH, m.group(2) != null ? Integer.parseInt(m.group(2)) - 1 : 0);
            c.set(Calendar.DATE, m.group(3) != null ? Integer.parseInt(m.group(3)) : 1);
            c.set(Calendar.HOUR_OF_DAY, m.group(5) != null ? Integer.parseInt(m.group(5)) + hoff : 0);
            c.set(Calendar.MINUTE, m.group(6) != null ? Integer.parseInt(m.group(6)) + moff : 0);
            c.set(Calendar.SECOND, m.group(7) != null ? Integer.parseInt(m.group(7)) : 0);
            c.set(Calendar.MILLISECOND, m.group(8) != null ? Integer.parseInt(m.group(8)) : 0);
            return c.getTime();
        } else {
            throw new IllegalArgumentException("Invalid Date Format: " + date);
        }
    }
}

Related

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