Android Date String to Date Convert stringToDate(String src, String format)

Here you can find the source of stringToDate(String src, String format)

Description

string To Date

Declaration

public static java.util.Date stringToDate(String src, String format) 

Method Source Code

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

public class Main {
    public static final String VN_DATE_FORMAT = "dd/MM/yyyy";

    public static java.util.Date stringToDate(String src, String format) {

        try {/*from w  ww .  j ava 2  s  .com*/
            src = src.trim();
            if (format == null)
                format = VN_DATE_FORMAT;
            SimpleDateFormat ts = new SimpleDateFormat(format);

            return ts.parse(src);
        } catch (Exception e) {
            //        System.out.println("Time Error:>"+src+"="+format+"<");
            //      e.printStackTrace();
            return null;
        }
    }

    public static java.util.Date parse(String src) {
        try {
            return DateFormat.getDateInstance().parse(src);
        } catch (ParseException e) {
            return DateFormat.getInstance().getCalendar().getTime();
        }
    }
}

Related

  1. getDate(String sDate, String dateFormat)
  2. convertStringToDate(String dateString, String currentFormat)
  3. dateToString(Date date, String format)
  4. parseStringToDate(String dateString, String sourceDateFormat, Locale locale)
  5. parse2Date(SimpleDateFormat s, Object d)
  6. toDateFromLongFormat(String date)
  7. parse(String aFormat, String date)
  8. getDate(String date)
  9. getDateFromString(String dateStr, String pattern)