One problem I encountered is the datetime field in SQL Server.
AndDateThe problem of object home Conversion,
For example, in the database: '2017-10-13 22:12:10'
String,
AndDateThe object has a problem of mutual conversion,
For exampleDateConvert to the string format above,
Also, convert the above stringDateObject,
How can this happen?
Urgent!
/**
* @ Param STR format requirements: yyyy, M (mm), D (dd); for example: 2002-01-02,2002-1--2-15
* @ Return: return the success date, and return NULL for failure;
*/
Public staticDateStringtodate (StringStr ){
StringStrformat = "yyyy-mm-dd hh: mm ";
If (STR! = NULL & Str. Length () = 10 ){
Strformat = "yyyy-mm-dd ";
}
Simpledateformat sdformat = new simpledateformat (strformat );
Date Date= NewDate();
Try {
Date= Sdformat. parse (STR );
}
Catch (exception e ){
// System. Out. println ("error =" + E );
Return NULL;
}
ReturnDate;
}
Public staticDateStringtodate (StringSTR,StringStrformat ){
Simpledateformat sdformat = new simpledateformat (strformat );
Date Date= NewDate();
Try {
Date= Sdformat. parse (STR );
}
Catch (exception e ){
Return NULL;
}
ReturnDate;
}
Public staticStringDatetoymd (DateDT ){
Simpledateformat sdformat = new simpledateformat ("yyyy-mm-dd ");
StringSTR = "";
Try {
STR = sdformat. Format (DT );
}
Catch (exception e ){
Return "";
}
If (Str. Equals ("1900-01-01 ")){
STR = "";
}
Return STR;
}
Public staticStringDatetostring (DateDT ){
Simpledateformat sdformat = new simpledateformat ("yyyy-mm-dd hh: mm ");
StringSTR = "";
Try {
STR = sdformat. Format (DT );
}
Catch (exception e ){
Return "";
}
If (Str. Equals ("1900-01-01 00:00 ")){
STR = "";
}
Return STR;
}
Public staticStringDatetostring (DateDT,StringStrformat ){
Simpledateformat sdformat = new simpledateformat (strformat );
StringSTR = "";
Try {
STR = sdformat. Format (DT );
}
Catch (exception e ){
Return "";
}
If (Str. Equals ("1900-01-01 00:00 ")){
STR = "";
}
Return STR;
}
SetDateClass into a string:
Date Date= NewDate();
System. Out. println (Date. Tolocalstring ());
Converts a stringDate
StringSTR = "22:12:10 ";
Simpledateformat dateformat = new simpledateformat ("yyyy-mm-dd hh: mm: SS ");
Date Date= Dateformat. parse (STR );
========================================================== ========================================================== =====
/*
* Dateutil class: provides a series of common operation methods for time operations.
* Create Date: 2004-2-9
* Version 1.0
*/
Import java. SQL .*;
Import java. util. calendar;
Import java. util. date;
Import java. util. gregoriancalendar;
Import java. Text. simpledateformat;
Import java. Text. parseexception;
Public class dateutil {
Public dateutil (){
}
/**
* Obtain the date string based on the format
* @ Param sformat
* @ Return
*/
Public static string getdatestr (string sformat ){
Calendar tcal = calendar. getinstance ();
Timestamp Ts = new timestamp (tcal. gettime (). gettime ());
Java. util. Date = new java. util. Date (TS. gettime ());
Simpledateformat formatter = new simpledateformat (sformat );
String tmpstr = formatter. Format (date );
Return (tmpstr );
}
/**
* Get the formatted display of a specific time according to the given format
* @ Param TS
* @ Param sformat
* @ Return
*/
Public static string getdateformat (timestamp ts, string sformat ){
Date = new date (TS. gettime ());
Simpledateformat formatter
= New simpledateformat (sformat );
String tmpstr = formatter. Format (date );
Return tmpstr;
}
/**
* Format the date
* @ Param TS
* @ Return
*/
Public static string getsdate (timestamp TS ){
Date = new date (TS. gettime ());
Simpledateformat formatter
= New simpledateformat ("yyyy-mm-dd ");
String tmpstr = formatter. Format (date );
Return tmpstr;
}
/**
* Converts a string-type date to a time value.
* @ Param dt
* @ Return
*/
Public static timestamp getts (string DT ){
Date = java. SQL. Date. valueof (DT );
Calendar tcal = calendar. getinstance ();
Tcal. settime (date );
Timestamp Ts = new timestamp (tcal. gettime (). gettime ());
Return ts;
}
/**
* We recommend that you obtain the short date processing method.
* Example: get1_date (10:10:10. 123) = 2004-10-10
* @ Param dt
* @ Return
*/
Public static string get1_date (string DT ){
Try {
Return DT. substring (0, DT. indexof (""));
}
Catch (exception e ){
Return DT;
}
}
/**
*
* Get the current date and time
* @ Return
*/
Public static timestamp getcurrdatetime (){
Calendar tcal = calendar. getinstance ();
Timestamp createdate = new timestamp (tcal. gettime (). gettime ());
Return createdate;
}
/**
* Obtain the most common date format content: year-month-day hour-minute-second
* @ Param TS
* @ Return
*/
Public static string getsdatetime (timestamp TS ){
Return getdateformat (TS, "yyyy-mm-dd hh: mm: SS ");
}
/* Format the date */
Public static string getstime (timestamp TS ){
Return getdateformat (TS, "HH: mm: SS ");
}
/**
* Obtain the date of the current day.
* @ Return
*/
Public static string gettoday (){
Java. SQL. timestamp Ts = new java. SQL. timestamp (system. currenttimemillis ());
Simpledateformat formatter = new simpledateformat ("yyyy-mm-dd ");
Return formatter. Format (TS );
}
// Obtain a random number based on time
Public static string getrnd ()
{
Calendar tcal = calendar. getinstance ();
Timestamp Ts = new timestamp (tcal. gettime (). gettime ());
Java. util. Date = new java. util. Date (TS. gettime ());
Simpledateformat formatter = new simpledateformat ("yyyymmddhhmmss ");
String tmpstr = formatter. Format (date) + math. Round (math. Random () * 1000 + 1 );
Return (tmpstr );
}
/**
* The difference between calculation dates increases by-3-25
* @ Param dt1
* @ Param dt2
* @ Return
*/
Public static int datediff (timestamp dt1, timestamp dt2 ){
Long ldate1 = dt1.gettime ();
Long ldate2 = dt2.gettime ();
Return (INT) (ldate2-ldate1)/(1000*60*60*24 ));
}
/**
* Get tomorrow's date
* @ Return
*/
Public static string gettomorrow (){
Return getnextday (gettoday ());
}
/**
* Obtain the next day of the current date
* @ Param date
* @ Return
*/
Public static string getnextday (string date ){
If (date = NULL | date. Trim (). Length () = 0 ){
Return "";
}
Simpledateformat F = new simpledateformat ("yyyy-mm-dd ");
Calendar calendar = calendar. getinstance ();
Try {
Calendar. settime (F. parse (date ));
}
Catch (parseexception ex ){
Return date;
}
Calendar. Add (5, 1 );
Return F. Format (calendar. gettime ());
}
/**
* Lst
* Num is positive: Num Days after the current date are returned values
* Num is negative: the return value is Num Days before the current date.
* Format of the returned Date: yyyy-mm-dd
*/
Public static string gettheday (INT num ){
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd ");
Gregoriancalendar GC = new gregoriancalendar ();
GC. Add (gregoriancalendar. Date, num );
Date theday = GC. gettime ();
Return SDF. Format (theday );
}
}