从字符串“ yyyyddmm hh:mm:ss”转换日期到字符串“ DD / MM / YYYY”;在JAVA中 [英] Convert date from string "yyyyddmm hh:mm:ss" to String "DD/MM/YYYY" in JAVA

查看:272
本文介绍了从字符串“ yyyyddmm hh:mm:ss”转换日期到字符串“ DD / MM / YYYY”;在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 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆