The conversion between String and Date in java is done using SimpleDateFormat. When using SimpleDateFormat, remember to import java.text.SimpleDateFormat.
Java.lang.Object
| ----java.text.Format
| ----java.text .DateFormat
##| ->Stringjava.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd "); String s= "2011-07-09 "; Date date = formatter.parse(s);Of course there are many ways to express the SimpleDateFormat format: SimpleDateFormat function syntax:
java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd "); String date = formatter.format(new Date());//格式化数据Common standard writing method "yyyy-MM- dd HH:mm:ss", pay attention to the case. The time is in 24-hour format. To convert the 24-hour format into 12-hour format, you only need to change HH to hh. No additional functions are needed.
> G 年代标志符 y 年 M 月 d 日 h 时 在上午或下午 (1~12) H 时 在一天中 (0~23) m 分 s 秒 S 毫秒 E 星期 D 一年中的第几天 F 一月中第几个星期几 w 一年中第几个星期 W 一月中第几个星期 a 上午 / 下午 标记符 k 时 在一天中 (1~24) K 时 在上午或下午 (0~11) z 时区
The above is the detailed content of How to convert string to date type in java. For more information, please follow other related articles on the PHP Chinese website!