Home > Java > javaTutorial > body text

How to convert string to date type in java

藏色散人
Release: 2019-05-27 11:31:57
Original
19002 people have browsed it

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);
Copy after login
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());//格式化数据
Copy after login
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 时区
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template