字符串到日期转换mm / dd / yy到java中的YYYY-MM-DD [英] String to Date Conversion mm/dd/yy to YYYY-MM-DD in java
本文介绍了字符串到日期转换mm / dd / yy到java中的YYYY-MM-DD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将mm / dd / yy格式的String值转换为YYYY-MM-DD Date。如何进行此转换?
I want to convert String values in the format of mm/dd/yy to YYYY-MM-DD Date. how to do this conversion?
输入参数为: 03/01/18
将字符串转换为日期的代码如下所示
Code to convert String to Date is given below
public static Date stringToDateLinen(String dateVlaue) {
Date date = null;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
date = formatter.parse(dateVlaue);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
尝试使用此方法进行转换时,显示以下错误
When tried to convert using this method it shows the following error
java.text.ParseException: Unparseable date: "03/01/18"
推荐答案
正如您所说的输入格式不同,首先将String转换为有效的Date对象。获得Date对象后,可以根据需要将其格式化为不同的类型,检查。
As you say the input is in a different format, first convert the String to a valid Date object. Once you have the Date object you can format it into different types , as you want, check.
转换为日期,
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");
date = formatter.parse(dateVlaue);
要以其他格式打印出来,
To Print it out in the other format,
SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
dateString = formatter1.format(date)
这篇关于字符串到日期转换mm / dd / yy到java中的YYYY-MM-DD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文