Java Parse Date parseDate(Date date)

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

Description

Return a String for the given java.util.Date as per DATE_FORMAT

License

Open Source License

Parameter

Parameter Description
date Date object

Return

String for the given java.util.Date as per DATE_FORMAT, null in case of error

Declaration

public static String parseDate(Date date) 

Method Source Code


//package com.java2s;
import java.util.*;
import java.text.*;

public class Main {
    /**//w  ww.  j a  v a  2  s.c  o m
     * Default Date format
     */
    public static final String DATE_FORMAT = "dd-MM-yyyy";

    /**
     * Return a Date for the DATE_FORMAT format string
     *
     * @param dateStr Date format string
     *
     * @return Java Date object if the input is valid, else null
     */
    public static Date parseDate(String dateStr) {
        // Check input
        if (dateStr == null) {
            return null;
        }

        // Create the formatter object
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

        // Do not parse the string if it do not adhere to the format given
        formatter.setLenient(false);

        // Parse
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(dateStr, pos);

        // Return
        return date;
    }

    /**
     * Return a String for the given java.util.Date as per DATE_FORMAT
     *
     * @param date Date object
     *
     * @return String for the given java.util.Date as per DATE_FORMAT, null in
     * case of error
     */
    public static String parseDate(Date date) {
        // Check input
        if (date == null) {
            return null;
        }

        // Create the formatter object
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);

        // Do not parse the string if it do not adhere to the format given
        formatter.setLenient(false);

        // Parse
        return formatter.format(date);
    }
}

Related

  1. parseDate(Date date)
  2. parseDate(Date date)
  3. parseDate(Date date)
  4. parseDate(Date date)
  5. parseDate(Date date)
  6. parseDate(Date date, int style, Locale local, boolean withTime)
  7. parseDate(Date date, String format)
  8. parseDate(Date date, String pattern)
  9. parseDate(DateFormat df, String dateStr)