java - 如何将字符串时间戳读入形式为 Fri Jul 17 12:41:20 IST 2020 的 Java 日期?
问题描述
我有一个格式为 Fri Jul 17 12:41:20 IST 2020 的字符串,我想将其保存在列类型为 DATETIME 的 MS SQL 服务器表中。
为此,我首先需要将上述字符串转换为 Date POJO。
解决方案
如果需要,您也可以存储 long ,但字符串到Date或Long的基本转换。
//Fri Jul 17 12:41:20 IST 2020
public static Long getDateTime(String input){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
try {
Date date = simpleDateFormat.parse(input);
return date.getTime();
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args){
System.out.println(DateConverter.getDateTime("Fri Jul 17 12:41:20 IST 2020"));
}
推荐阅读
- javascript - sequelize.sync({force:true}) 不起作用并且方法的日志记录为空
- sql - 如何将此查询从相关更改为不相关
- java - 循环直到输入标记值的Java程序
- javascript - 如何使用 React 在对话框(material-ui)中传递组件?
- azure - 我可以使用 Azure MFA 而不使用其 SSO 登录吗?
- python - 打印 0 之前的最后一个结果
- c# - 更新/删除列表视图中包含倒数计时器的行
- javascript - 如何自动复制输入的文本?
- javascript - Nuxt Axios 在页面之间移动时返回错误
- mysql - MySQL 如果行重复,则首选匹配字段值的行