Java Parse Date parseDate(String text)

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

Description

parse Date

License

Apache License

Declaration

public static Date parseDate(String text) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final Pattern TZ_REGEX = Pattern.compile("([+-][0-9][0-9]):?([0-9][0-9])$");

    public static Date parseDate(String text) {
        if (text == null)
            return null;
        Matcher matcher = TZ_REGEX.matcher(text);
        TimeZone timeZone = null;
        if (matcher.find()) {
            String tzCode = "GMT" + matcher.group(1) + matcher.group(2); // eg "GMT+0100"
            timeZone = TimeZone.getTimeZone(tzCode);
        }/*w w w.  j a v a 2s . c  o  m*/
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        if (timeZone != null) {
            formatter.setTimeZone(timeZone);
        }
        try {
            Date result = formatter.parse(text);
            return result;
        } catch (ParseException e) {
            return null;
        }
    }
}

Related

  1. parseDate(String strDate, String format)
  2. parseDate(String strFormat, String dateValue)
  3. parseDate(String string)
  4. ParseDate(String string, String format)
  5. parseDate(String t)
  6. parseDate(String text, String[] datePattern, int index)
  7. parseDate(String time)
  8. parseDate(String time)
  9. parseDate(String timeString)