始终使用 SimpleDateFormat 指定区域设置,并为自定义格式指定 DateTimeFormatter
问题:
解析日期-使用 SimpleDateFormat 的时间字符串在特定设备上失败,导致“无法解析的日期”错误。
分析:
SimpleDateFormat 需要一个 Locale 参数来指定日期解析的语言和文化约定。如果未指定区域设置,则使用系统的默认区域设置。这可能会导致在具有不同区域设置的设备上解析不一致。
解决方案:
- 在创建 SimpleDateFormat 实例时显式指定所需的区域设置。
- 使用现代的 DateTimeFormatter API 代替 SimpleDateFormat。 它提供了特定于语言环境的解析和格式化,提高了性能和灵活性。
详细说明:
给定日期-时间字符串(“24 Oct 2016 7:31 pm”)是英文的。但是,如果系统的默认 Locale 不是英语(例如,法国电话),默认的 SimpleDateFormat 实例将无法解析它。
Java 8 (DateTimeFormatter):
在 Java 8 中,使用具有显式 Locale 的 DateTimeFormatter 来解析自定义日期时间字符串。避免使用预定义的格式化程序,它们不接受区域设置。例如:
DateTimeFormatter dtf = new DateTimeFormatterBuilder() .parseCaseInsensitive() .appendPattern("d MMM uuuu h:m a") .toFormatter(Locale.ENGLISH); LocalDateTime ldt = LocalDateTime.parse(strDateTime, dtf);
Java 6/7 (ThreeTen-Backport):
如果无法使用 Java 6 或 Java 7,请使用 ThreeTen-Backport 向后移植DateTimeFormatter 功能。
其他注意:
- 错误消息“Unparseable date: "24 Oct 2016 7:31 pm" (at offset 3)”表示解析失败,因为第二个字符(“c”) )位于“Oct”(偏移量 3 处的字符)。这可能是由于月份名称中特定于区域设置的差异造成的。
- 强烈建议切换到现代 DateTimeFormatter API 来进行所有日期时间处理,因为它在功能、性能和一致性方面更优越与 SimpleDateFormat 相比。
以上是为什么我的 SimpleDateFormat 无法解析日期,如何使用 Locale 和 DateTimeFormatter 修复它?的详细内容。更多信息请关注PHP中文网其他相关文章!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java实现“一次编写,到处运行”通过编译成字节码并在Java虚拟机(JVM)上运行。1)编写Java代码并编译成字节码。2)字节码在任何安装了JVM的平台上运行。3)使用Java原生接口(JNI)处理平台特定功能。尽管存在挑战,如JVM一致性和平台特定库的使用,但WORA大大提高了开发效率和部署灵活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

禅工作室 13.0.1
功能强大的PHP集成开发环境

记事本++7.3.1
好用且免费的代码编辑器