Java String to Date strToDate(String date, String pattern)

Here you can find the source of strToDate(String date, String pattern)

Description

str To Date

License

Apache License

Declaration

public static Date strToDate(String date, String pattern) 

Method Source Code


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

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

import java.util.Date;
import java.util.Locale;

public class Main {
    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";

    public static Date strToDate(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {/*from  w  w w. j a  v a 2  s.  c o m*/
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse(String date) {
        SimpleDateFormat format = new SimpleDateFormat(yyyyMMddHHmmss);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String parse(String date, String format) {
        SimpleDateFormat fmt = new SimpleDateFormat(format, Locale.US);
        try {
            Date datetime = fmt.parse(date);
            return dateToStr(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String dateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(yyyyMMddHHmmss);
        return format.format(date);
    }

    public static String dateToStr(Date date, String formatstr) {
        SimpleDateFormat format = new SimpleDateFormat(formatstr);
        return format.format(date);
    }
}

Related

  1. strMSShortToDate(String str_date)
  2. strToDate(final String str)
  3. strToDate(String _date, String format)
  4. strToDate(String date)
  5. strToDate(String date)
  6. StrToDate(String dateFormat)
  7. strToDate(String dateInString)
  8. strToDate(String dateStr, String dateFormat)
  9. strToDate(String dateStr, String format)