Home  >  Article  >  Java  >  How to convert string to date type in java

How to convert string to date type in java

藏色散人
藏色散人Original
2019-05-27 11:31:5719009browse

The conversion between String and Date in java is done using SimpleDateFormat. When using SimpleDateFormat, remember to import java.text.SimpleDateFormat.

How to convert string to date type in java

Java.lang.Object

| ----java.text.Format

| ----java.text .DateFormat

##| ->String

java.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.

Complex example

>    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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:what is eclipse ideNext article:what is eclipse ide