Java String to Date StringToDate1(String strDate)

Here you can find the source of StringToDate1(String strDate)

Description

String To Date

License

Apache License

Declaration

public static Date StringToDate1(String strDate) throws ParseException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

import java.util.Date;

public class Main {
    public static Date StringToDate1(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(strDate);
        return new java.util.Date(date.getTime());

    }/*from   www . ja va2  s  . co m*/

    public static Date getTime() {
        try {
            return getDate("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate() {
        try {
            return getDate("yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate(String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = new Date(System.currentTimeMillis());
        return convertStringToDate(df.format(date), format);
    }

    public static Date convertStringToDate(String time, String format)
            throws ParseException {
        if (format == null) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(time);
    }
}

Related

  1. stringToDate(String string, String format)
  2. stringToDate(String text)
  3. StringToDate(String thisdate, Locale locale)
  4. stringToDate(String time, String format)
  5. stringToDate(String vStr, String vPatten)
  6. stringToDate10(String date)
  7. stringToDateAndTime(final String dateAndTimeString)
  8. stringToDateByconf(String str, String formate)
  9. StringToDateByFormat(String dateStr, String formatStr)