java Joda 日期格式化程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25122796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Joda date formatter
提问by Hubert Solecki
Does anyone know how to parse a date such as: Mon Aug 04 16:07:00 CEST 2014
to dd/MM/YYYY HH:MM:SS
using DateTime
formatter from Joda.
I've tried that:
没有人知道如何解析的日期,例如:Mon Aug 04 16:07:00 CEST 2014
对dd/MM/YYYY HH:MM:SS
使用DateTime
格式化从乔达。我试过了:
final DateTimeFormatter sdf = DateTimeFormat.forPattern(DATE_FORMAT);
DateTime lastDateOnline = sdf.parseDateTime(lastCommunicationToDisplay.getDateOnLine().toString());
return lastDateOnline.toString();
DATE_FORMAT = dd/MM/YYYY HH:MM:SS
and
lastCommunicationToDisplay.getDateOnLine().toString() = Mon Aug 04 16:07:00 CEST 2014
DATE_FORMAT = dd/MM/YYYY HH:MM:SS
和
lastCommunicationToDisplay.getDateOnLine().toString() = Mon Aug 04 16:07:00 CEST 2014
I can't find clear explanations about that library. I'm requested to use that instead of SimpleDateFormat
because it's not threadsafe.
我找不到有关该库的明确解释。我被要求使用它而不是SimpleDateFormat
因为它不是线程安全的。
回答by Cristina
Solutions
解决方案
If all you have to do is convert a LocalDate to a string respecting the pattern: "dd/MM/YYYY HH:mm:ss", then you can do it in a simpler way, using the overloaded toString() methods on LocalDate:
如果您所要做的就是将 LocalDate 转换为符合模式的字符串:“dd/MM/YYYY HH:mm:ss”,那么您可以使用 LocalDate 上的重载 toString() 方法以更简单的方式完成:
a) the one which receives the format string directly:
a) 直接接收格式字符串的那个:
LocalDate date = lastCommunicationToDisplay.getDateOnLine();
System.out.println(date.toString("dd/MM/YYYY HH:mm:ss"));
b) the one which receives a DateTimeFormatter initialized with the aforementioned string:
b) 接收用上述字符串初始化的 DateTimeFormatter 的那个:
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm:ss");
LocalDate date = lastCommunicationToDisplay.getDateOnLine();
System.out.println(date.toString(dtf));
What went wrong in your code
你的代码出了什么问题
The format string you are using is not compatible with the date string you are sending as input. The way you used DateTimeFormatter is used for parsing strings that are in that format to LocalDates, not the other way around.
您使用的格式字符串与您作为输入发送的日期字符串不兼容。您使用 DateTimeFormatter 的方式用于将该格式的字符串解析为 LocalDates,而不是相反。
The format would be appropriate if your input string would look like the following: 04/08/2014 22:44:33
如果您的输入字符串如下所示,则格式将是合适的:04/08/2014 22:44:33
Since yours looks differently, the following value of the format is compatible (provided your timezone is always CEST):
由于您的看起来不同,因此格式的以下值是兼容的(前提是您的时区始终为 CEST):
DATE_FORMAT = "E MMM dd HH:mm:ss 'CEST' YYYY";
So the entire code should look like this:
所以整个代码应该是这样的:
String dateString = "Mon Aug 04 16:07:00 CEST 2014";
DateTimeFormatter dtf = DateTimeFormat.forPattern("E MMM dd HH:mm:ss 'CEST' YYYY");
LocalDate date = dtf.parseLocalDate(dateString);
System.out.println(date.toString("MM/dd/yyyy")); // or use toString(DateTimeFormatter) and use your pattern with a small adjusment here (dd/MM/YYYY HH:mm:ss)
However, I recommend one of the first 2 suggestions.
但是,我推荐前两个建议之一。