Java SQL Date Parse StrToDate(String val)

Here you can find the source of StrToDate(String val)

Description

Convert a string date in the YYYY-MM-DD format into a java.sql.Date object.

License

Open Source License

Parameter

Parameter Description
val The date string to be converted

Return

A date object matching the string sent in

Declaration

static public java.util.Date StrToDate(String val) 

Method Source Code


//package com.java2s;
import java.sql.*;
import java.util.Calendar;
import java.util.StringTokenizer;

public class Main {
    /**/*from  w w  w.  j a  v  a2  s  .com*/
     * Convert a string date in the YYYY-MM-DD format into a java.sql.Date
     * object.
     *
     * @param val The date string to be converted
     * @return A date object matching the string sent in
     */
    static public java.util.Date StrToDate(String val) {
        Calendar cal = Calendar.getInstance();

        if (val != null) {
            StringTokenizer st = new StringTokenizer(val, "-");

            int temp = Integer.parseInt(st.nextToken());
            if (temp < 100) {
                if (temp > 70) {
                    temp += 1900;
                } else {
                    temp += 2000;
                }
            }
            cal.set(Calendar.YEAR, temp);
            cal.set(Calendar.MONTH, Integer.parseInt(st.nextToken()) - 1);
            cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(st.nextToken()));
        }

        return new Date(cal.getTimeInMillis());
    }
}

Related

  1. stringToSqlDate(String str)
  2. StrToDate(String Formate, String date)
  3. strToDate(String str)
  4. strToDate(String strDate, String formator)
  5. strToDate(String stringDate)
  6. strToDate(String value, String pattern)
  7. strToSqlDate(String format, String value)
  8. strToUtilDate(String fecha, String hora)