简体   繁体   English

Java:从字符串转换为时间戳时,毫秒解析错误

[英]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.

相关问题 毫秒未从 java new Date() function 正确转换为带有时区的 postgres 时间戳 - Milliseconds are not converted properly from java new Date() function to the postgres Timestamp with time Zone 在Java中将字符串转换为Timestamp时,添加了夏令时 - Daylight savings time being added when string is converted to Timestamp in Java 将时间戳以毫秒为单位转换为 Java 中的字符串格式时间 - Convert timestamp in milliseconds to string formatted time in Java 如何将时间戳字符串转换为java中的日期 - How to converted timestamp string to a date in java 在 Java 中将毫秒转换为时间戳 - Converting milliseconds to Timestamp in Java 比较毫秒时java.util.Timestamp.after()错误吗? - java.util.Timestamp.after() wrong when comparing milliseconds? 在Java中将哈希图的哈希图转换为json。 当发送到jsp时,我在javascript中对其进行了解析。 但是在javascript中没有正确解析 - Hashmap of hashmaps converted to json in java. When sent to jsp, i parsed it in javascript. But it is not parsed correct in javascript 错误:无法将来自映射的String类的对象转换为[class java.sql.Timestamp] - Error: The object of class String, from mapping could not be converted to [class java.sql.Timestamp] 在Java中转换为StringEntity时,如何从字符串中删除编码错误的字符? - How can I remove wrongly encoded characters from a string when converting to a StringEntity in Java? 如何从Java中的毫秒时间戳中提取一天中的时间段? - How to extract the period of the day from milliseconds timestamp in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM