Java和Android中的SimpleDateFormat是否不同? [英] Is SimpleDateFormat in Java and Android different?
问题描述
-
当我们从JVM调用
parseJavaAndroid("2018-04-27T22:31:07.962-05:00")
时,它给出ParseException
,而从Emulator/Android Real设备中进行调用时,它可以正常工作.
When we call
parseJavaAndroid("2018-04-27T22:31:07.962-05:00")
from JVM it givesParseException
while a call from Emulator/Android Real device it works fine.
当我们在Android中将SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ", Locale.ENGLISH)
更改为SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH)
时,它会抛出:
When we change SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ", Locale.ENGLISH)
to SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH)
in Android, it throws:
带有消息未知模式字符'X'方法"的IllegalArgumentException.
IllegalArgumentException with message "Unknown pattern character 'X' method".
它在JVM中运行良好,并在Emulator/Android Real设备中提供ParseException
.
It works fine in JVM and gives ParseException
in Emulator/Android Real device.
public static Date parseJavaAndroid(String fromDate){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ", Locale.ENGLISH);
Date date=null;
try {
date = format.parse(fromDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
该怎么办,它才能在JVM和android实时设备( minSdkVersion:19 )中工作.我想要用于"2018-04-27T22:31:07.962-05:00"的模式,该模式可同时在Android和Java上使用.
What should have to do then it works in both JVM and android real time device(minSdkVersion:19). I want pattern for "2018-04-27T22:31:07.962-05:00" which works on both Android as well as Java.
注意:个人模式有效,因此请不要为个人提供建议.
Note: The individual pattern is working, so Please don't suggest for individuals.
推荐答案
我无法讲述Android,但是在JVM中,Z
模式仅识别没有:
的偏移量,例如-0500
,而X
模式可以识别-05:00
: https ://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
I can't tell about Android, but in the JVM, the Z
pattern recognizes only offsets without :
, such as -0500
, while the X
pattern can recognize -05:00
: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
也许Android具有不同的实现方式,其中Z
还可以识别:
的偏移量,例如-05:00
.
Perhaps Android has a different implementation, where Z
also recognizes offsets with :
, such as -05:00
.
恐怕没有一种完全适合所有人的解决方案:Z
在JVM中不起作用,X
在Android中不起作用,如何使其在两种环境中都能起作用?我认为您最好的办法是尝试两种模式,如下所示(用伪代码):
I'm afraid there's no one-pattern-fits-all solution: Z
doesn't work in JVM, X
doesn't work in Android, how to make it work in both? I think the best you can do is to try both patterns, something like this (in pseudocode):
try {
parse with X
} catch(Exception e) {
X didn't work, parse with Z
}
更好的另一种选择是使用三个反向端口,使用 ThreetenABP 配置-或使用
A much better alternative is to use the threeten backport and configure with ThreetenABP - or use java.time
classes if your API level is 26:
// parse input
OffsetDateTime odt = OffsetDateTime.parse("2018-04-27T22:31:07.962-05:00");
// convert to java.util.Date (threeten backport)
Date date = DateTimeUtils.toDate(odt.toInstant());
// or, if you have java.time (API level 26)
Date date = date.from(odt.toInstant());
这篇关于Java和Android中的SimpleDateFormat是否不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!