[英]Java: milliseconds parsed wrongly when converted from string to timestamp
I have trouble while parsing the given string into a timestamp. 将给定的字符串解析为时间戳时遇到麻烦。 The milliseconds is parsed wrongly or let me know if I am missing something. 毫秒解析错误,或者如果我遗漏了某些东西,请告诉我。
I get a string from the request as: 我从请求中得到一个字符串为:
String selectedTimeStamp = request.getParameter("selectTime");
System.out.println("selectedTimeStamp: "+selectedTimeStamp);
and then I use simpleDateFormat to parse and format the string: 然后我使用simpleDateFormat解析和格式化字符串:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
Date parsedDate = sdf.parse(selectedTimeStamp);
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println("createdTime: " +timestamp);
The output I get is: 我得到的输出是:
selectedTimeStamp: 2016-07-04 21:09:47.66
createdTime: 2016-07-04 21:09:47.066
Not sure why the millisecond is converted from 66 to 066 ? 不确定为什么将毫秒从66转换为066吗? It should be 660 any idea? 660应该有什么主意吗?
Let's break it down step-by-step 让我们逐步分解
Your code(effectively) 您的代码(有效)
String selectedTimeStamp = "2016-07-04 21:09:47.6";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
Date parsedDate = sdf.parse(selectedTimeStamp);
System.out.println("parsedDate: " + parsedDate);
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println("createdTime: " +timestamp);
I am assuming that these are the imports you have made: 我假设这些是您进口的产品:
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
The output of which is 其输出是
parsedDate: Mon Jul 04 21:09:47 IST 2016 日期:2016年7月4日星期一21:09:47
createdTime: 2016-07-04 21:09:47.006 createdTime:2016-07-04 21:09:47.006
So even though I passed 6 ms as argument, it got parsed to 600 ms, this is most likely due to the fact that we are using, java library for parsing date and sql library for time stamping it. 因此,即使我将6毫秒作为参数传递,它也被解析为600毫秒,这很可能是由于我们正在使用Java库来解析日期,而sql库来为其时间戳记的事实。 The process for treating date objects is different in both the languages, hence the inconsistency. 两种语言中处理日期对象的过程不同,因此不一致。 Also millisecond is treated with a number of 1000th precision(as 1000ms = 1s) therefore SQL automatically converts the Java stored 6 to 006 (or 66 to 066 in your case). 毫秒也以千分之一的精度进行处理(因为1000ms = 1s),因此SQL自动将Java存储的6转换为006(在您的情况下为66到066)。
I simple workaround this problem will be to check selectedTimeStamp
using selectedTimeStamp.length() - selectedTimeStamp.lastIndexOf('.')
and concatenating the remaining zeroes, ie if it is 2 then 2 zeroes, if 3 then 1 zero, and if 4 then no zeroes. 我可以通过一个简单的解决方法来解决此问题,即使用selectedTimeStamp.length() - selectedTimeStamp.lastIndexOf('.')
检查selectedTimeStamp
并串联剩余的零,即如果是2则2零,如果3则1零,如果4则没有零。
This will give you correct result. 这将为您提供正确的结果。
Add this after the String selectesTimeStamp = "2016-07-04 21:09:47.6" line 在String selectesTimeStamp =“ 2016-07-04 21:09:47.6”行之后将其添加
int x = selectedTimeStamp.length() - selectedTimeStamp.lastIndexOf('.');
if (x==2)
selectedTimeStamp += "00";
if (x==3)
selectedTimeStamp += '0';
Cheers!!! 干杯!!!
PS: Changing SSS to SS will not work. PS:将SSS更改为SS将不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.