在 Java 中使用不同的格式将字符串解析为日期 [英] Parse String to Date with Different Format in Java
本文介绍了在 Java 中使用不同的格式将字符串解析为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将String
转换成不同格式的Date
.
I want to convert String
to Date
in different formats.
例如
我从用户那里得到,
String fromDate = "19/05/2009"; // i.e. (dd/MM/yyyy) format
我想将此fromDate
转换为"yyyy-MM-dd"
格式的Date对象
I want to convert this fromDate
as a Date object of "yyyy-MM-dd"
format
我该怎么做?
推荐答案
看看 SimpleDateFormat
.代码是这样的:
Take a look at SimpleDateFormat
. The code goes something like this:
SimpleDateFormat fromUser = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
String reformattedStr = myFormat.format(fromUser.parse(inputString));
} catch (ParseException e) {
e.printStackTrace();
}
这篇关于在 Java 中使用不同的格式将字符串解析为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文