在将其存储在数据库中之前,我使用以下服务器代码从用户获取时间戳的文本表示。
String timestampString = request.getParameter("timestamp");
String timezoneID = request.getParameter("timezone");
System.out.println(TAG + " newTestIdString: " + newTestIdString
+ "\n" + TAG + " timestampString: " + timestampString);
Integer newTestId = Integer.parseInt(newTestIdString);
try{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone(timezoneID));
Date parsedDate = dateFormat.parse(timestampString);
System.out.println(TAG + "timezone-JP: " + dateFormat.format(parsedDate));
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(TAG + "timezone-UTC: " + dateFormat.format(parsedDate));
dateFormat.format(parsedDate);
timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println(TAG + "timestamp: " + timestamp.toString());
}catch(Exception e){
e.printStackTrace();
}
但是,如果用户在7:29发送包含Timestamp字符串的数据,则会得到以下输出:
INFO | jvm 1 | 2015/09/17 18:29:59 | ProcessTest newTestIdString: 1109
INFO | jvm 1 | 2015/09/17 18:29:59 | ProcessTest timestampString: 2015-09-18 07:29:58
INFO | jvm 1 | 2015/09/17 18:29:59 | ProcessTest timezone-JP: 2015-09-18 07:29:58
INFO | jvm 1 | 2015/09/17 18:29:59 | ProcessTest timezone-UTC: 2015-09-17 10:29:58
INFO | jvm 1 | 2015/09/17 18:29:59 | ProcessTest timestamp: 2015-09-17 18:29:58.0
为什么时间戳会更改最后一行中的日期?
如何创建带有UTC日期的时间戳记?
一个TimeStamp
或一个Date
没有任何时区与之关联...
完成后timestamp.toString()
,它将在JVM的默认时区中给出Date的字符串表示形式。
如果您不想在特定时区使用format()
它,就像在其他Date
对象上一样。
System.out.println(TAG + "timestamp: " + dateFormat.format(timestamp));
PS:java.sql.Timestamp
扩展java.util.Date
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句