从字符串“ yyyyddmm hh:mm:ss”转换日期到字符串“ DD / MM / YYYY”;在JAVA中 [英] Convert date from string "yyyyddmm hh:mm:ss" to String "DD/MM/YYYY" in JAVA
问题描述
有人可以帮助我进行此转换吗?
Can someone help me with this conversion ?
String dateTime="20140505 03:23:50"
DateFormat formatter=new SimpleDateFormat("DD/MM/yyyy");
String date=formatter.format(dateTime);
我希望输出格式为 DD / MM / YYYY,该格式应为字符串。
I want the output to be in this format - "DD/MM/YYYY" which should be a string .
推荐答案
您必须这样做:
String dateTime="20140505 03:23:50";
DateFormat formatter=new SimpleDateFormat("yyyyMMdd HH:mm:ss");
Date date=formatter.parse(dateTime);
formatte.applyPattern("dd/MM/yyyy");
String dateStr = formatter.format(date);
首先,您必须将 String
转换为根据您的 String $ c $使用
formatter.parse(string)
的 Date
对象c>模式,然后可以通过更改将
模式。日期
更改为任何 String
格式SimpleDateFormat
First you have to convert your String
to Date
object using formatter.parse(string)
based on your String
pattern, and then you can change your Date
to any String
format by changing your SimpleDateFormat
pattern.
注意: D:
每年的一天和 d:
每月的一天,因此请使用 d
代替 D
。
Note: D:
day in year and d:
day in month, So use d
instead of D
.
您可以根据需要在Java中使用范式验证日期格式。
You can use verity of patterns in java to format date as per your requirement.
这篇关于从字符串“ yyyyddmm hh:mm:ss”转换日期到字符串“ DD / MM / YYYY”;在JAVA中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!