Java Date Format dateFormatCheck(String source, String format)

Here you can find the source of dateFormatCheck(String source, String format)

Description

date Format Check

License

Open Source License

Declaration

public static Date dateFormatCheck(String source, String format) throws ParseException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.util.Date;

import java.util.Locale;

public class Main {
    private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date dateFormatCheck(String source, String format) throws ParseException {

        if (source == null) {
            throw new ParseException("date string to check is null", 0);
        }/*from   www  .j a  v  a2s  . c om*/

        if (format == null) {
            throw new ParseException("format string to check date is null", 0);
        }

        SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.KOREA);

        Date date = null;

        try {
            date = formatter.parse(source);
        } catch (ParseException e) {
            throw new ParseException(" wrong date:\"" + source + "\" with format \"" + format + "\"", 0);
        }

        if (!formatter.format(date).equals(source)) {
            throw new ParseException("Out of bound date:\"" + source + "\" with format \"" + format + "\"", 0);
        }

        return date;
    }

    public static Date parse(String datetime) {
        try {
            return FORMAT.parse(datetime);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static String format(Date date) {
        return FORMAT.format(date);
    }
}

Related

  1. dateFormat(String format, String defaultFormat)
  2. dateformat(String text, String format)
  3. dateFormat3(Date date)
  4. dateFormat4yyyyMMdd()
  5. dateFormatCheck(String source)
  6. dateformate(String reg, Date newdate)
  7. dateFormater(String dataStr, String dataFormat)
  8. dateFormatString(Date date)
  9. dateFormatted(Date date)