[英]How to format a date to following format day.month.year?
I need to format a date to following format: 10.12.2014
and I am using the following code but it returns following error 我需要将日期格式化为以下格式: 10.12.2014
,我正在使用以下代码,但返回以下错误
Messages:
Unparseable date: "2014-12-10"
Code 码
SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");
Date date = formatter.parse(param.getFromDate());
String formattedDate = formatter.format(date);
Unparseable date
means your input dateString value is not in the same format as expected. Unparseable date
表示您输入的dateString值格式与预期的格式不同。 For example , if your dateString is 2014-12-10 ( yyyy-MM-dd
) , if you try to format it to dd-MM-yyyy
, then this exception will occur. 例如 ,如果您的dateString为2014-12-10( yyyy-MM-dd
),则如果尝试将其格式化为dd-MM-yyyy
,则将发生此异常。
The following code will help you.! 以下代码将为您提供帮助。
// Existing date is in this format : "2014-12-10"
SimpleDateFormat formatFrom = new SimpleDateFormat("yyyy-MM-dd");
// Required date is in this format : 10.12.2014
SimpleDateFormat formatTo = new SimpleDateFormat("dd.MM.yyyy");
// Convert the String param.getFromDate()==>2014-12-10 to a Date
Date date = formatFrom .parse(param.getFromDate());
// Convert Date to String 10.12.2014
String formattedDate = formatTo .format(date);
Change your date format, you are parsing yyyy-MM-dd
and providing format as yyyy.MMMM.dd
:- 更改日期格式,您正在解析yyyy-MM-dd
并提供格式为yyyy.MMMM.dd
:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = formatter.parse(param.getFromDate());
String formattedDate = formatter.format(date);
Edit:- you updated format input to dd.mm.yyyy
, which again is wrong, and above solution is valid in this case too. 编辑:-您将格式输入更新为dd.mm.yyyy
,这也是错误的,以上解决方案在这种情况下也是有效的。
Also you want the date output as 10.12.2014
, which you can do by creating new formatter and parsing the date to string using it:- 另外,您还希望日期输出为10.12.2014
,您可以通过创建新的格式化程序并使用日期将日期解析为字符串来实现:
formatter = new SimpleDateFormat("dd.MM.yyyy");
String formattedDate2 = formatter.format(date); //
check this out: Java Docs for SimpleDateFormat 检查一下: SimpleDateFormat的Java文档
contains all the pattern letters for date and time, helped me a lot when i was looking onto how to implement date and time in my app. 包含日期和时间的所有模式字母,当我研究如何在应用程序中实现日期和时间时,对我有很大帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.