java - 如何在 Java 中将字符串 AM/PM 日期转换为时间戳?
问题描述
我想解析一个包含从字符串到时间戳的 AM/PM 格式的日期。
虽然当我尝试解析这个日期时:Dec 31 2017 12:00AM
我得到一个
Exception in thread "main" java.text.ParseException: Unparseable date: "Dec 31 2017 12:00AM"
at java.text.DateFormat.parse(Unknown Source)
at test.DatteTest.main(DatteTest.java:16)
我的代码:
String endDate = "Dec 31 2017 12:00AM";
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mmaa");
Date parsedDate = dateFormat.parse(endDate);
Timestamp timestamp = new Timestamp(parsedDate.getTime());
System.out.println(endDate);
System.out.println(timestamp);
我在 simpledateformat 中做错了吗?“MMM dd yyyy hh:mmaa”?
解决方案
输入日期包含一个英文单词。您必须提供语言环境:
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd yyyy hh:mmaa", Locale.ENGLISH);
推荐阅读
- types - 我如何获取feetype的ID列表,后跟学生ID
- css - 从菜单和链接中删除下划线
- ibm-integration-bus - 如何从属性文件中读取数据并将其存储在 ibm mb 的缓存节点中
- python - 在 OpenCV 和/或 PIL 中叠加两个图像时出错
- android - 如何将附加音频文件上传到 Firebase?
- git - 也许从 GIT 项目运行命令“git lfs install”
- java - 使用 selenium 在 java 中使用 tesng 进行并行测试
- java - 无法在 Java 中运行用于 websocket 实现的客户端端点代码(找不到类文件错误)
- java - javax.imageio.IIOException 读取输入文件时
- emacs - 在 Emacs 中以特定宽度设置连续的新帧