Java Parse Date Pattern YYYY parse(String str)

Here you can find the source of parse(String str)

Description

parse

License

Apache License

Declaration

public static Date parse(String str) 

Method Source Code


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

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_PATTERN = "yyyy-MM-dd";

    public static Date parse(String str, String formatPattern) {
        try {//from w w  w .  ja  va  2 s. c o  m
            return new SimpleDateFormat(formatPattern).parse(str);
        } catch (ParseException e) {
            throw new RuntimeException("could not parse date: " + str + " LEGACY_FORMAT = "
                    + new SimpleDateFormat(formatPattern).toPattern(), e);
        }
    }

    public static Date parse(String str) {
        if (str.length() != DATE_PATTERN.length() && str.length() != DATE_TIME_PATTERN.length()) {
            return null;
        }
        String formatPattern = DATE_PATTERN;
        if (str.length() > 10) {
            formatPattern = DATE_TIME_PATTERN;
        }

        try {
            return new SimpleDateFormat(formatPattern).parse(str);
        } catch (ParseException e) {
            throw new RuntimeException("could not parse date: " + str + " LEGACY_FORMAT = "
                    + new SimpleDateFormat(formatPattern).toPattern(), e);
        }
    }
}

Related

  1. parse(String s, String formatPattern)
  2. parse(String source)
  3. parse(String source)
  4. parse(String source)
  5. parse(String source, Date defaultValue)
  6. parse(String str)
  7. parse(String str)
  8. parse(String str)
  9. parse(String str)