在Android中更改日期格式出现ParseException错误。

huangapple go评论74阅读模式
英文:

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&#39;m new in Android and Java development. 

I have this day type `&quot;Fri Jan 27 00:00:00 GMT+00:00 1995&quot;`. &lt;-- (input)

I want to get `&quot;1995/27/01&quot;`   &lt;-- (output)

I&#39;m using this code : 
```lang-java
String inputPattern = &quot;EEE MMM dd HH:mm:ss z yyyy&quot;;
String outputPattern = &quot;yyyy-MM-dd&quot;;

SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

Date date = inputFormat.parse(&quot;Fri Jan 27 00:00:00 GMT+00:00 1995&quot;);
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 = &quot;Fri Jan 27 00:00:00 GMT+00:00 1995&quot;;
	// define a pattern that parses the format of the input String
	String inputPattern = &quot;EEE MMM dd HH:mm:ss O uuuu&quot;;
	// 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(&quot;date, time and zone (ISO):\t&quot;
						+ 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(&quot;date only as desired:\t\t&quot;
						+ dateOnly.format(DateTimeFormatter.ofPattern(&quot;uuuu/dd/MM&quot;)));
}

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 = &quot;EEE MMM dd HH:mm:ss z yyyy&quot;;
String outputPattern = &quot;yyyy-MM-dd&quot;;

SimpleDateFormat sdf = new SimpleDateFormat(inputPattern);
Date date = sdf.parse(&quot;Fri Jan 27 00:00:00 GMT+00:00 1995&quot;);

sdf.applyPattern(outputPattern);
String str = sdf.format(date);

huangapple
  • 本文由 发表于 2020年10月7日 19:17:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64242927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定