Package test;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat; public class TestDate {public static void main (string[] args) throws ParseException {java.util.Date udate = new Java . util.
Date ();
Java.sql.Date sdate = new Java.sql.Date (0);
SimpleDateFormat sdfdate = new SimpleDateFormat ("Yyyy-mm-dd");
SimpleDateFormat sdftime = new SimpleDateFormat ("HH:mm:ss");
SimpleDateFormat sdfdatetime = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Converts the Date in the Util package to the Sql.date udate.gettime ();
Gets a long sdate = new Java.sql.Date (Udate.gettime ()); GetParameter is a string that converts the string date "2017-08-08" to Java.sql.Date//1. Converts the string date to Java.util.Date//2 through Simpledateformat.parse (String str). Get long//3 via Java.util.Date.getTime ().
New Java.sql.Date (Java.util.Date.getTime ());
Java.util.Date judate = Sdfdate.parse ("2017-8-1");
Long ldate = Judate.gettime ();
Java.sql.Date jsdate = new Java.sql.Date (ldate);
System.out.println (judate);
System.out.println (ldate); SYstem.out.println (jsdate);
SYSTEM.OUT.PRINTLN (Sdatetosqldate (null, "Yyyy-mm-dd HH:mm:ss", "1990-1-1 0:0:0")); public static Java.sql.Timestamp Sdatetosqldate (string date, string pattern, string defaultdate) {try {Simpleda
Teformat SDF = new SimpleDateFormat (pattern);
Long ldate = sdf.parse (date!= null? date:defaultdate). GetTime ();
return new Java.sql.Timestamp (ldate);
catch (ParseException e) {e.printstacktrace ();
return null;
}
}The Java.sql.Date type dates the method of construction: Date (long date), which requires a long date dated, and the date we do the item fetch (getparameter) from the page is a string type.
The first step requires us to convert the string date to the date of the Util type, which requires the format of the date: SimpleDateFormat
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Java.util.Date judate = Sdf.parse ("2017-8-1 20:32:25");
The second step converts date dates of the Util type to long date
Judate.gettime ()
The third step is to convert a long date to a SQL type using the method of constructing SQL type Date
New Java.sql.Date (Judate.gettime ()); Common methods of SimpleDateFormat class
| NO |
Method |
type |
Description |
| 1 |
Public SimpleDateFormat (String pattern) |
Structure |
The build uses the default format locale to set the given pattern and default date format symbol SimpleDateFormat. |
| 2 |
Public final String format (date date) |
Ordinary |
Format a date As String data |
| 3 |
Public Date Parse (String source) throws ParseException |
Ordinary |
Format a string as date data |