Copy codeThe Code is as follows: import java. SQL. Timestamp;
Import java. text. DateFormat;
Import java. text. ParseException;
Import java. text. SimpleDateFormat;
Import java. util. Date;
Public class DateIO {
Public static void main (String [] args ){
Date date = new DateIO (). strToDate ("2013-04-01 ");
String strdate = new DateIO (). dateToStr (date );
String srrdate = new DateIO (). timestampToStr (System. currentTimeMillis ());
Timestamp ts = new DateIO (). strToTimestamp (new Date ());
}
// Convert String to Date
Public Date strToDate (String strdate ){
DateFormat format = new SimpleDateFormat ("yyyy-MM-dd ");
Date date = null;
Try {
Date = format. parse (strdate );
} Catch (ParseException e ){
E. printStackTrace ();
}
System. out. println ("date:" + date );
Return date;
}
// Convert Date to String
Public String dateToStr (Date date ){
// Year, month, and day ****-**-**
DateFormat format = new SimpleDateFormat ("yyyy-MM-dd ");
String str = format. format (date );
System. out. println ("str:" + str );
// Year, month, and day **-*-*
Format = DateFormat. getDateInstance (DateFormat. SHORT );
Str = format. format (date );
System. out. println (str );
// Year, month, and day ****-*-*
Format = DateFormat. getDateInstance (DateFormat. MEDIUM );
Str = format. format (date );
System. out. println (str );
// ***** Month day week *
Format = DateFormat. getDateInstance (DateFormat. FULL );
Str = format. format (date );
System. out. println (str );
Return str;
}
// Convert Timestamp to String
Public String timestampToStr (Long timestamp ){
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // defines the format, not in milliseconds
String str = df. format (timestamp );
System. out. println (str );
Return str;
}
// Convert Date to Timestamp
Public Timestamp strToTimestamp (Date date ){
SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
String time = df. format (date );
Timestamp ts = Timestamp. valueOf (time );
System. out. println (ts );
Return ts;
}
}