英文:
Changing date format in Android getting ParseException
问题
我在Android和Java开发方面是新手。
我有这个日期类型:`"Fri Jan 27 00:00:00 GMT+00:00 1995"`。
我想要得到:`"1995/27/01"`。
我正在使用这段代码:
```lang-java
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy/MM/dd";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern, Locale.ENGLISH);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = inputFormat.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
String str = outputFormat.format(date);
但是我遇到了ParseException
错误。
有任何想法是为什么吗?
<details>
<summary>英文:</summary>
I'm new in Android and Java development.
I have this day type `"Fri Jan 27 00:00:00 GMT+00:00 1995"`. <-- (input)
I want to get `"1995/27/01"` <-- (output)
I'm using this code :
```lang-java
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy-MM-dd";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = inputFormat.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
String str = outputFormat.format(date);
but I get a ParseException
.
Any idea why?
答案1
得分: 3
以下是翻译好的部分:
public static void main(String[] args) {
// 示例输入字符串
String datetime = "Fri Jan 27 00:00:00 GMT+00:00 1995";
// 定义一个解析输入字符串格式的模式
String inputPattern = "EEE MMM dd HH:mm:ss O uuuu";
// 定义一个使用此模式解析输入的格式化程序
DateTimeFormatter parser = DateTimeFormatter.ofPattern(inputPattern, Locale.ENGLISH);
// 使用格式化程序以解析带有时区信息的对象
ZonedDateTime zdt = ZonedDateTime.parse(datetime, parser);
// 然后将其输出为内置的 ISO 格式
System.out.println("日期、时间和时区(ISO):\t" + zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// 然后提取日期部分
LocalDate dateOnly = zdt.toLocalDate();
// 并使用仅包含日期的模式的格式化程序输出该日期部分
System.out.println("仅日期如所需:\t\t" + dateOnly.format(DateTimeFormatter.ofPattern("uuuu/dd/MM")));
}
此代码段的输出为:
日期、时间和时区(ISO): 1995-01-27T00:00:00Z
仅日期如所需: 1995/27/01
英文:
If you were using java.time
(available from Java 8), you could do it like this:
public static void main(String[] args) {
// example input String
String datetime = "Fri Jan 27 00:00:00 GMT+00:00 1995";
// define a pattern that parses the format of the input String
String inputPattern = "EEE MMM dd HH:mm:ss O uuuu";
// define a formatter that uses this pattern for parsing the input
DateTimeFormatter parser = DateTimeFormatter.ofPattern(inputPattern, Locale.ENGLISH);
// use the formatter in order to parse a zone-aware object
ZonedDateTime zdt = ZonedDateTime.parse(datetime, parser);
// then output it in the built-in ISO format once,
System.out.println("date, time and zone (ISO):\t"
+ zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
// then extract the date part
LocalDate dateOnly = zdt.toLocalDate();
// and output that date part using a formatter with a date-only pattern
System.out.println("date only as desired:\t\t"
+ dateOnly.format(DateTimeFormatter.ofPattern("uuuu/dd/MM")));
}
The output of this piece of code is
date, time and zone (ISO): 1995-01-27T00:00:00Z
date only as desired: 1995/27/01
答案2
得分: 0
也许在代码中添加 applyPattern 函数
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(inputPattern);
Date date = sdf.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
sdf.applyPattern(outputPattern);
String str = sdf.format(date);
英文:
Maybe add applyPattern function in code
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(inputPattern);
Date date = sdf.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
sdf.applyPattern(outputPattern);
String str = sdf.format(date);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论