Dim*_*cha 57 java datetime timestamp date
我尝试将String转换为TimeStamp时遇到问题.我有一个格式为日期的数组,yyyy-MM-dd我希望以格式更改yyyy-MM-dd HH:mm:ss.SSS.所以,我使用这段代码:
final String OLD_FORMAT = "yyyy-MM-dd";
final String NEW_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
String oldDateString = createdArray[k];
String newDateString;
DateFormat formatter = new SimpleDateFormat(OLD_FORMAT);
Date d = formatter.parse(oldDateString);
((SimpleDateFormat) formatter).applyPattern(NEW_FORMAT);
newDateString = formatter.format(d);
System.out.println(newDateString);
Timestamp ts = Timestamp.valueOf(newDateString);
System.out.println(ts);
Run Code Online (Sandbox Code Playgroud)
我得到以下结果.
2009-10-20 00:00:00.000
2009-10-20 00:00:00.0
但是当我试着干脆做的时候
String text = "2011-10-02 18:48:05.123";
ts = Timestamp.valueOf(text);
System.out.println(ts);
Run Code Online (Sandbox Code Playgroud)
我得到了正确的结果:
2011-10-02 18:48:05.123
你知道我可能做错了什么吗?谢谢您的帮助.
Zao*_*Bao 98
请按照以下步骤获得正确的结果:
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
Date parsedDate = dateFormat.parse(yourString);
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
} catch(Exception e) { //this generic but you can control another types of exception
// look the origin of excption
}
Run Code Online (Sandbox Code Playgroud)
请注意,.parse(String)可能会抛出一个ParseException.
Har*_*rsh 15
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Util {
public static Timestamp convertStringToTimestamp(String strDate) {
try {
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
// you can change format of date
Date date = formatter.parse(strDate);
Timestamp timeStampDate = new Timestamp(date.getTime());
return timeStampDate;
} catch (ParseException e) {
System.out.println("Exception :" + e);
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Ole*_*.V. 11
我想提出现代的答案.在2013年提出此问题时,使用Timestamp该类是正确的,例如将日期时间存储到数据库中.今天班级已经过时了.在三年半前的2014年春天,Java 8推出了现代Java日期和时间API.我建议你改用它.
根据您的具体情况,有两种自然替代品Timestamp:
Instant是时间线上的一个点.在大多数情况下,我认为使用它最安全.An Instant与时区无关,即使在客户端设备和数据库服务器运行不同时区的情况下也通常可以正常工作.LocalDateTime 是一个没有时区的日期和时间,如2011-10-02 18:48:05.123(引用问题).现代JDBC驱动程序(JDBC 4.2或更高版本)和其他用于数据库访问的现代工具将很乐意将a Instant或a 存储LocalDateTime到数据类型的数据库列中timestamp.我在本答案中使用的类和其他日期时间类都属于称为java.timeJSR-310 的现代API .
将字符串转换为最简单的方法LocalDateTime,让我们先来看看:
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
String text = "2011-10-02 18:48:05.123";
LocalDateTime dateTime = LocalDateTime.parse(text, formatter);
System.out.println(dateTime);
Run Code Online (Sandbox Code Playgroud)
这打印
2011-10-02T18:48:05.123
Run Code Online (Sandbox Code Playgroud)
如果你的字符串是yyyy-MM-dd格式,那么:
String text = "2009-10-20";
LocalDateTime dateTime = LocalDate.parse(text).atStartOfDay();
System.out.println(dateTime);
Run Code Online (Sandbox Code Playgroud)
这打印
2009-10-20T00:00
Run Code Online (Sandbox Code Playgroud)
或者更好的是,从中获取输出LocalDate.parse()并将其存储到数据类型的数据库列中date.
在两种情况下,用于从转换的程序LocalDateTime到一个Instant是:
Instant ts = dateTime.atZone(ZoneId.systemDefault()).toInstant();
System.out.println(ts);
Run Code Online (Sandbox Code Playgroud)
我已经使用JVM的默认时区指定了转换,因为这是过时的类将使用的.但这很脆弱,因为时区设置可能会被程序的其他部分或同一JVM中运行的其他程序改变.如果可以,请改为指定区域/城市格式的时区,例如:
Instant ts = dateTime.atZone(ZoneId.of("Europe/Athens")).toInstant();
Run Code Online (Sandbox Code Playgroud)
将 String 转换为 java.sql.Timestamp 的简单方法:
Timestamp t = new Timestamp(DateUtil.provideDateFormat().parse("2019-01-14T12:00:00.000Z").getTime());
Run Code Online (Sandbox Code Playgroud)
DateUtil.java:
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public interface DateUtil {
String ISO_DATE_FORMAT_ZERO_OFFSET = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
String UTC_TIMEZONE_NAME = "UTC";
static SimpleDateFormat provideDateFormat() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ISO_DATE_FORMAT_ZERO_OFFSET);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone(UTC_TIMEZONE_NAME));
return simpleDateFormat;
}
}
Run Code Online (Sandbox Code Playgroud)
首先,将日期字符串转换为,然后使用以下行将date其转换为:timestamp
final Date date = new Date();
final Timestamp timestamp = new Timestamp(date.getTime()); // Instead of date, put your converted date
final Timestamp myTimeStamp = timestamp;
Run Code Online (Sandbox Code Playgroud)