Java Parse Date parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz)

Here you can find the source of parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz)

Description

parse Date Subset

License

Apache License

Declaration

private static final Date parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz) 

Method Source Code

//package com.java2s;
/**//from   ww w .  j  a va  2 s  .c  o m
 * Copyright (C) 2011 University of Washington
 *
 * 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.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    private static final Date parseDateSubset(String value, String[] parsePatterns, Locale l, TimeZone tz) {
        // borrowed from apache.commons.lang.DateUtils...
        Date d = null;
        SimpleDateFormat parser = null;
        ParsePosition pos = new ParsePosition(0);
        for (int i = 0; i < parsePatterns.length; i++) {
            if (i == 0) {
                if (l == null) {
                    parser = new SimpleDateFormat(parsePatterns[0]);
                } else {
                    parser = new SimpleDateFormat(parsePatterns[0], l);
                }
            } else {
                parser.applyPattern(parsePatterns[i]);
            }
            parser.setTimeZone(tz); // enforce UTC for formats without timezones
            pos.setIndex(0);
            d = parser.parse(value, pos);
            if (d != null && pos.getIndex() == value.length()) {
                return d;
            }
        }
        return d;
    }
}

Related

  1. parseDateString(String pattern, String dateString)
  2. parseDateString(String s)
  3. parseDateString(TimeZone tz, String dateString)
  4. parseDateStringToDate(DateFormat format, String dateString)
  5. parseDateStrShort(String date)
  6. parseDateTime(@Nullable String s)
  7. parseDateTime(final String dateTime)
  8. parseDateTime(String _date)
  9. parseDateTime(String currDate, String format)