1 Packagecom.java.test;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 7 Public classDatestringtransfrom {8 9 Public Static voidMain (string[] args) {Ten Stringtransfromdateone (); One //stringtransformdatetwo (); A //Datetransformstringone (); - //datetransformstringtwo (); - } the - //yyyy-mm-dd A string converted to a Date object in HH:MM:SS format - Static voidStringtransfromdateone () { -SimpleDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); +Date Date =NULL; - Try { +Date = Dateformat.parse ("2010-10-01 12:00:00"); A}Catch(ParseException e) { at //TODO auto-generated Catch block - e.printstacktrace (); - } -System.out.println ("Date object:" +date); - } - in //Convert a string in YYYY-MM-DD format to a Date object - Static voidStringtransformdatetwo () { toSimpleDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd"); +Date Date =NULL; - Try { theDate = Dateformat.parse ("2010-10-01"); *}Catch(ParseException e) { $ //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); - } theSystem.out.println ("Date object:" +date); + } A the //Convert the current time object to a string + Static voidDatetransformstringone () { -Date Date =NewDate (); $SimpleDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); $String datestring =Dateformat.format (date); -System.out.println ("datestring:" +datestring); - } the - //specifying a Date object to convert to a stringWuyi Static voidDatetransformstringtwo () { the //Create a specified Date object -SimpleDateFormat DateFormat =NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); WuDate Date =NULL; - Try { AboutDate = Dateformat.parse ("2010-10-01 12:00:00"); $}Catch(ParseException e) { - //TODO auto-generated Catch block - e.printstacktrace (); - } A +String datestring =Dateformat.format (date); theSystem.out.println ("datestring:" +datestring); - $ } the the}
The Date object and the string in Java are transferred to each other