【问题标题】:Convert FormatDate.MEDIUM to other format (LocalDate) in Java将 FormatDate.MEDIUM 转换为 Java 中的其他格式(LocalDate)
【发布时间】:2014-06-10 00:28:45
【问题描述】:
我在用 Java 转换其他格式的日期时遇到问题(我正在使用 JodaTime)。
事实上,我有一个格式化的本地日期:
24/apr/14 (Italian format date...but other local formats are possible)
我想将日、月和年分开并在输出中查看:
gg: 24
MM: 04
yyyy: 2014
如何检索这些数据?
谢谢!!
【问题讨论】:
标签:
android
date
converter
jodatime
【解决方案1】:
将您的假设“24/apr/14”更正为意大利语(JodaTime 和 JDK 都说:d-MMM-yyyy)我发现了这种方式:
String input = "24-apr-2014";
Locale locale = Locale.ITALY;
DateTimeFormatter dtf = DateTimeFormat.mediumDate().withLocale(locale);
LocalDate date = dtf.parseLocalDate(input);
int dayOfMonth = date.getDayOfMonth();
int month = date.getMonthOfYear();
int year = date.getYear();
DecimalFormat df = new DecimalFormat("00");
String dayOfMonthAsText = df.format(dayOfMonth);
String monthAsText = df.format(month);
String yearAsText = new DecimalFormat("0000").format(year);
System.out.println(dayOfMonthAsText); // 24
System.out.println(monthAsText); // 04
System.out.println(yearAsText); // 2014
顺便说一句,为什么要提取文本组件(导致大量额外的格式化工作 - 请参阅我的代码),而不仅仅是解析的整数值?还是我误会了你?