简体   繁体   English

如何将日期格式化为以下格式day.month.year?

[英]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.

相关问题 如何在jtextfield中以日月(字)和年的格式获取日期 - How to get date in jtextfield in format of day month(in words) and year 有没有一种简单的方法可以将Date(sql)转换为以下格式Month(3个字符)day(int),year(2014) - is there a simple way to convert Date(sql) to following format Month(3 character) day(int) , year(2014) Java-格式化给定日期的日,月和年整数 - Java - Format a date given day, month and year integers Java将字符串(日/月/年)格式转换为日期 - Java Converting String(day/month/year) format to Date 如何在Joda中格式化带有日期和日期的日期 - How to format a date w/ Month and Day in Joda 如何更改日期的json字符串并将其格式化为仅显示不带年,月,日或时间的日期 - How to change json string of date and format it to only show the day without, year, month, or time 如何从时间戳(长格式)中检索日月和年 - how to retrieve day month and year from Timestamp(long format) 如何拼贴预订日期输入并以 LocalDate 格式返回,并使用布尔值验证年/月/日值? - How do I collage booking date inputs and return as LocalDate format., and validate year/month/day values with a boolean? 如何以MM / YY作为日期(字符串格式)查找月份的最后一天? - How to find the last day of month with MM/YY as date (in string format)? 如何在Java中将日期格式化为仅月,日和小时? - How can I format a Date in Java as only month, day and hour?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM