Java Date Parse convertStringToDate(String aMask, String strDate)

Here you can find the source of convertStringToDate(String aMask, String strDate)

Description

This method generates a string representation of a date/time in the format you specify on input

License

Open Source License

Parameter

Parameter Description
aMask the date pattern the string is in
strDate a string representation of a date

Exception

Parameter Description
ParseException an exception

Return

a converted Date object

Declaration

public static final Date convertStringToDate(String aMask, String strDate) throws ParseException 

Method Source Code

//package com.java2s;

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

import java.util.Date;

public class Main {
    /**/*  w  ww  .  j a v a 2s  . c  o m*/
     * This method generates a string representation of a date/time in the
     * format you specify on input
     * 
     * @param aMask
     *            the date pattern the string is in
     * @param strDate
     *            a string representation of a date
     * @return a converted Date object
     * @see java.text.SimpleDateFormat
     * @throws ParseException
     */
    public static final Date convertStringToDate(String aMask, String strDate) throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            return null;
        }

        return (date);
    }

    /**
     * This method converts a String to a date using the datePattern
     * 
     * @param strDate
     *            the date to convert (in format MM/dd/yyyy)
     * @return a date object
     * 
     * @throws ParseException
     */
    public static Date convertStringToDate(String strDate) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static String parse(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
        String dateStr = sdf.format(d);
        return dateStr;
    }
}

Related

  1. convertDateStringToMilliseconds(String dateString)
  2. convertIntToDate(int date)
  3. convertIntToDatePattern2(int date)
  4. convertLongToDate(final long time)
  5. convertStringToDate(String aMask, String strDate)
  6. convertStringToDate(String arg)
  7. convertStringToDate(String data)
  8. convertStringToDate(String date)
  9. convertStringToDate(String date)