【SimpleDateFormat字符串转换时间不准确】
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了【SimpleDateFormat字符串转换时间不准确】相关的知识,希望对你有一定的参考价值。
看看如下代码:
public static void main(String[] args)
SimpleDateFormat s1 = new SimpleDateFormat("yyyyMMddHHmm");
try
System.out.println(s1.parse("20130123120501"));
System.out.println(s1.parse("20130123120000"));
catch (ParseException e)
e.printStackTrace();
分和秒都为0才会解析正确,这是为什么?谁能说下SimpleDateFormat 解析原理吗?
秒没有解析出来是因为你格式的问题
SimpleDateFormat s1 = new SimpleDateFormat("yyyyMMddHHmmss");
这样设置,再输出试一下!
字母 表示
日期或时间元素
表示
示例
G Era 标志符 Text AD
y 年 Year 1996; 96
M 年中的月份 Month July; Jul; 07
w 年中的周数 Number 27
W 月份中的周数 Number 2
D 年中的天数 Number 189
d 月份中的天数 Number 10
F 月份中的星期 Number 2
E 星期中的天数 Text Tuesday; Tue
a Am/pm 标记 Text PM
H 一天中的小时数(0-23) Number 0
k 一天中的小时数(1-24) Number 24
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800 参考技术A 因为你的解析匹配模式没有告诉解析器解析秒。把解析格式换成
SimpleDateFormat s1 = new SimpleDateFormat("yyyyMMddHHmmss");就可以解析秒了!本回答被提问者采纳 参考技术B SimpleDateFormat s1 = new SimpleDateFormat("yyyy-MM-dd").parse(s1); 参考技术C yyyyMMddHHmm小于需要解析数字的长度。
使用 SimpleDateFormat 将字符串转换为日期返回不正确的格式 [重复]
【中文标题】使用 SimpleDateFormat 将字符串转换为日期返回不正确的格式 [重复]【英文标题】:Converting String to Date using SimpleDateFormat returns incorrect format [duplicate] 【发布时间】:2020-10-05 10:40:44 【问题描述】:我有一个字符串s
日期,格式如下05-10-2020
我想将此字符串转换为以下格式的日期dd-MM-yyyy
所以我可以在下面的查询中使用它来查询我的 Sqlite db
SimpleDateFormat simpledateformat = new SimpleDateFormat("dd-MM-yyyy");
String s = app.getDate(); // returns "05-10-2020"
Date date = simpledateformat.parse(s);
String selectQuery = "SELECT * FROM User Where Date !=" + date;
但 date 是以以下格式返回日期 Mon Oct 05 00:00:00 GMT+03:00 2020
【问题讨论】:
您还期待什么?这是正确的日期。 我在 db 中的日期是 DateTime 类型,我需要将它与相同的数据类型进行比较,这也是我正在尝试做的 DateTime。将字符串转换为日期对象,以便我可以比较两者。如果我错了,请纠正我 你可以试试simpledateformat.format(s);
,只是一个想法
@aryanagarwal format() 返回一个字符串
顺便考虑扔掉长期过时且臭名昭著的麻烦SimpleDateFormat
和朋友。看看您是否可以使用desugaring 或将ThreeTenABP 添加到您的Android 项目中,以便使用现代Java 日期和时间API 的java.time。使用起来感觉好多了。
【参考方案1】:
您应该像这样使用PreparedStatement
和LocalDate
:
LocalDate ld = LocalDate.parse(app.getDate(),DateTimeFormatter.ofPattern("dd-MM-uuuu"));
String selectQuery = "SELECT * FROM User Where Date != ?";
try (PreparedStatement ps = con.prepareStatement(selectQuery))
ps.setObject (1, ld);
// ...
catch (SQLException e)
e.printStackTrace();
【讨论】:
为什么我必须使用 PreparedStatement @r_via 避免 SQL 注入和语法错误 @r_via - 检查SQL Injection。另外,这个答案教你另一个好东西try-with-resources。【参考方案2】:您应该为 RDBMS 工作使用准备好的语句。从第三行开始,尝试:
PreparedStatement selectQuery = connectionObj.prepareStatement("SELECT * FROM User Where Date != ?");
selectQuery.setDate(1, date);
ResultSet res = selectQuery.executeQuery();
res.next();
// 随心所欲地操作结果集
【讨论】:
【参考方案3】:它将提供精益解决方案。您可以根据您的代码动态调整它。
String dateStr = "Mon Oct 05 00:00:00 GMT+03:00 2020";
DateFormat formatOne= new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
DateFormat formatSecond= new SimpleDateFormat("dd-MM-yyyy");
System.out.println(formatSecond.format(formatOne.parse(dateStr)));
您可以在项目的相关位置使用它。
DEMO
【讨论】:
我的字符串采用以下格式“05-10-2020”,上面的代码也返回 04-10-2020 它应该是 05-10-2020以上是关于【SimpleDateFormat字符串转换时间不准确】的主要内容,如果未能解决你的问题,请参考以下文章
关于SimpleDateFormat日期格式与字符串时间戳之间的转换
关于SimpleDateFormat日期格式与字符串时间戳之间的转换
java SimpleDateFormat转换日期格式问题: