Java String to Date stringToDate(String date)

Here you can find the source of stringToDate(String date)

Description

string To Date

License

Apache License

Declaration

public static Date stringToDate(String date) 

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 String DATE_PARTTEN = "yyyy-MM-dd";

    public static Date stringToDate(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_PARTTEN);
        Date d = null;/*from   ww  w .  ja v  a2  s. c o  m*/
        try {
            String[] datesl = new String[3];
            if (date.contains("-")) {
                datesl = date.split("-");
            } else if (date.contains("/")) {
                datesl = date.split("/");
            }
            if (datesl[1].length() == 1) {
                datesl[1] = "0" + datesl[1];
            }
            if (datesl[2].length() == 1) {
                datesl[2] = "0" + datesl[2];
            }
            d = sdf.parse(datesl[0] + "-" + datesl[1] + "-" + datesl[2]);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }
}

Related

  1. stringToDate(String date)
  2. stringToDate(String date)
  3. stringToDate(String date)
  4. stringToDate(String date)
  5. StringToDate(String date)
  6. stringToDate(String date)
  7. stringToDate(String date, String format)
  8. stringToDate(String date, String pattren)
  9. stringToDate(String datecontent, String format)