Java Date Parse getDateFromString(String stringDate, String format)

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

Description

Purpose:
getDateFromString

License

Open Source License

Parameter

Parameter Description
stringDate a parameter
format a parameter

Return


Declaration

public static Date getDateFromString(String stringDate, String format) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static final int EARLIEST_YEAR = 0001;

    /**/*from   w  ww .jav  a2 s  . co m*/
     * Purpose:
     * <br>
     * getDateFromString<br>
     * <br>
     * @param stringDate
     * @param format
     * @return<br>
     */
    public static Date getDateFromString(String stringDate, String format) {
        Date date;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(stringDate);
        } catch (ParseException ex) {
            date = getNoDate();
        }
        return date;
    }

    /**
     * Purpose:
     * <br>
     * getNoDate<br>
     * <br>
     * @return<br>
     */
    public static java.util.Date getNoDate() {
        java.util.Calendar vCalendar = java.util.Calendar.getInstance();
        vCalendar.set(java.util.Calendar.DAY_OF_MONTH, 1);
        vCalendar.set(java.util.Calendar.YEAR, EARLIEST_YEAR);
        vCalendar.set(java.util.Calendar.MONTH, java.util.Calendar.JANUARY);
        return killTime(vCalendar).getTime();
    }

    /**
     * Purpose:
     * <br>
     * killTime<br>
     * <br>
     * @param aCalendar
     * @return<br>
     */
    public static java.util.Calendar killTime(java.util.Calendar aCalendar) {
        if (aCalendar == null) {
            return null;
        }
        java.util.Calendar vCalendar = (java.util.Calendar) aCalendar.clone();
        vCalendar.set(java.util.Calendar.MILLISECOND, 0);
        vCalendar.set(java.util.Calendar.SECOND, 0);
        vCalendar.set(java.util.Calendar.MINUTE, 0);
        vCalendar.set(java.util.Calendar.HOUR_OF_DAY, 0);
        return vCalendar;
    }

    /**
     * Purpose:
     * <br>
     * killTime<br>
     * <br>
     * @param aDate
     * @return<br>
     */
    public static java.util.Date killTime(java.util.Date aDate) {
        if (aDate == null) {
            return null;
        }
        java.util.Calendar vCalendar = java.util.Calendar.getInstance();
        vCalendar.setTime(aDate);
        return killTime(vCalendar).getTime();
    }
}

Related

  1. getDateFromString(String dateString, String format)
  2. getDateFromString(String dateString, String format)
  3. getDateFromString(String input, String format)
  4. getDateFromString(String iso, String format)
  5. getDateFromString(String string)
  6. getDateFromStringTest(String date)
  7. getDateFromText(String dateStr)
  8. getDateFromTimestamp(String time, String pattern)
  9. getDateFromUPnP(String dateString)