but the page needs to display yyyy-MM-dd HH: mm: ss format
------ Solution ----------------- ---------------------------
public static void main(String[] args) throws Exception {
String date = "20131203";
DateFormat df = new SimpleDateFormat("yyyyMMdd");
Date d = df.parse(date);
df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String newDate = df.format(d);
System.out.println(newDate);
}
------ For reference only ----- ----------------------------------
Date date = new SimpleDateFormat (" yyyy-MM-dd HH: mm: ss "). parse (" 20131203 ");
------ For reference only -------------- -------------------------
20131203 inside the database you no hours minutes and seconds . Your front display yyyy-MM-dd bar
------ For reference only ------------------------ ---------------
final format must also be of type String
------ For reference only ---------------------------------- -----
required date conversion , specifically forgotten.
------ For reference only -------------------------------------- -
in the database conversion easier
TO_CHAR (TO_DATE ('20131203 ',' YYYYMMDD '),' YYYY-MM-DD HH24: MI: SS ')
------ For reference only ------- --------------------------------
if it is fixed yyyyMMdd eight string , then string concatenation direct interception not on the line
public static String test3(String str){
StringBuffer sb = new StringBuffer(str);
sb.insert(6, "-");
sb.insert(4, "-");
sb.append(" 00:00:00");
return sb.toString();
}
or sql query when splicing
------ For reference only -------------------------- -------------
database should be how to write it : I put the database code stickers out :
select date
from t_user
should be added to any place
Also you can control in the page ?
------ For reference only ---------------------------------- -----
this does not work , the database may change
------ For reference only ------------------------- --------------
that you still enumerate all instances it, if the database is not yyyyMMdd is MM -dd- yyyy Year day and so on all kind of situation is not dealt with all the wonderful work , keep it at least have a fixed rule it
没有评论:
发表评论