将带有分隔符“T”的字符串日期转换为 Java 中的 Java.sql 时间戳

Mas*_*aza 0 java timestamp date

我正在尝试将 java 转换String datejava.sql.Timestamp. 我可以通过使用SimpleDateFormatwith String datevalue as来转换它"2021-01-07 02:02:16.172",但是当尝试使用 value as "2021-08-04T00:00:00.000"with seperator时'T',它会给我错误。下面是java代码:

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTest {

    public static void main(String[] args) throws ParseException {
        
        //String date = "2021-08-04T00:00:00.000Z";// How to convert this?
        
        String date = "2021-01-07 02:02:16.172";// conversion successful
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        Date parsedDate = dateFormat.parse(date);
        
        Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
        
        System.out.println(timestamp);
    }
    
}
Run Code Online (Sandbox Code Playgroud)

deH*_*aar 5

您可以使用现代 API 来获取日期、时间和相关信息(例如与 UTC 的偏移量):java.time

String不同格式的s需要不同的处理:

  • 您的第一个示例String采用 ISO 标准格式,因此无需定义自定义格式即可对其进行解析。解析隐式使用 a DateTimeFormatter.ISO_OFFSET_DATE_TIME,这将导致OffsetDateTime

  • 你的秒String缺少'T'日期和时间之间以及偏移量,这意味着你可以直接将其解析为LocalDateTime

java.sql.Timestamp获得了转换为java.time类的方法,至少是与 anInstant和 a之间的转换LocalDateTime。由于 anInstant是一个明确定义的时刻,因此您可以从 an 导出它OffsetDateTime

public static void main(String[] args) throws Exception {
    // your two example datetimes
    String isoDateTime = "2021-08-04T00:00:00.000Z";
    String customDateTime = "2021-01-07 02:02:16.172";
    // you will need a custom formatter for the second one
    DateTimeFormatter customDtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
    // parse the Strings to java.time objects
    // ISO standard, no extra formatter needed for the first one
    OffsetDateTime odt = OffsetDateTime.parse(isoDateTime);
    // the second one requires the formatter defined above
    LocalDateTime ldt = LocalDateTime.parse(customDateTime, customDtf);
    // convert them into Timestamps
    Timestamp tsOne = Timestamp.from(odt.toInstant());
    Timestamp tsTwo = Timestamp.valueOf(ldt);
    // and print them
    System.out.println("First Timestamp:  " + tsOne);
    System.out.println("Second Timestamp: " + tsTwo);
}
Run Code Online (Sandbox Code Playgroud)

这个的输出是

public static void main(String[] args) throws Exception {
    // your two example datetimes
    String isoDateTime = "2021-08-04T00:00:00.000Z";
    String customDateTime = "2021-01-07 02:02:16.172";
    // you will need a custom formatter for the second one
    DateTimeFormatter customDtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS");
    // parse the Strings to java.time objects
    // ISO standard, no extra formatter needed for the first one
    OffsetDateTime odt = OffsetDateTime.parse(isoDateTime);
    // the second one requires the formatter defined above
    LocalDateTime ldt = LocalDateTime.parse(customDateTime, customDtf);
    // convert them into Timestamps
    Timestamp tsOne = Timestamp.from(odt.toInstant());
    Timestamp tsTwo = Timestamp.valueOf(ldt);
    // and print them
    System.out.println("First Timestamp:  " + tsOne);
    System.out.println("Second Timestamp: " + tsTwo);
}
Run Code Online (Sandbox Code Playgroud)

这将是新风格...