Java String to Date getStringToDate(String str)

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

Description

get String To Date

License

Open Source License

Declaration

public static Date getStringToDate(String str) 

Method Source Code


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

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

import java.util.Date;

public class Main {
    public static Date getStringToDate(String str) {
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date parse = null;/*  www  . j  a  v a2 s  . c  o  m*/
        str = str.replace("T", " ");
        try {
            parse = date.parse(str);
        } catch (ParseException e) {

            e.printStackTrace();
        }
        return parse;
    }

    public static Date parse(String source, String format) throws ParseException {
        if (source == null) {
            return null;
        }

        DateFormat df = null;
        if (format != null) {
            df = new SimpleDateFormat(format);
        } else {
            df = DateFormat.getDateInstance(DateFormat.DEFAULT);
        }

        return df.parse(source);

    }
}

Related

  1. addTimeToDate(String date, int value, String unit, String sFormat)
  2. getStringToDate(String date, String format)
  3. getStringToDate(String date, String inputDateFormat)
  4. getStringToDate(String pValue, String pDateFormat)
  5. getStringToDateFull(String da)
  6. getStringToDateTime(String dateStr, String formatStr)
  7. getTodateString()
  8. humanDateToDate(String date)