繁体   English   中英

使用 UTC 将 LocalDate 转换为 TimeStamp

[英]Convert LocalDate to TimeStamp with UTC

我将 LocalDate 作为输入,并希望将其转换为这种格式以在 oracle DB 中搜索。

input - "2010-10-10"

Output- 10-OCT-10 07.39.02.713000000 AM UTC

我尝试使用 TimeStamp 和 DateTime 但分别以这些格式获取日期。

2020-10-10 00:00:00.0 
2020-10-10T00:00:00.000+05:30

我用了

Timestamp.valueOf(startDate.atStartOfDay());
DateTime.parse(startDate.toString());

你能帮我么? 先感谢您

更新。

将给定的字符串解析为LocalDate并使用LocalDate#atStartOfDay(ZoneId)将其转换为ZonedDateTime

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Testing {
    public static void main(String[] args) {
        LocalDate date = LocalDate.parse("2010-10-10");
        ZonedDateTime zdt = date.atStartOfDay(ZoneId.of("Etc/UTC"));
        System.out.println(zdt);

        // Custom format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS a zzz", Locale.ENGLISH);
        System.out.println(dtf.format(zdt));
    }
}

Output:

2010-10-10T00:00Z[Etc/UTC]
2010-10-10 00:00:00.000 AM UTC

Trail: Date Time了解更多关于java.time现代日期时间 API *的信息。


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API级别仍然不符合 Java-8,请检查Java 8+ API 可通过脱糖如何在 Android 项目中使用 ThreeTenABP

Don't pass date and timestamp to oracle as strings (varchar2), just use bind variables of required data types:oracle.sql.DATE or oracle.sql.TIMESTAMP .

您可以在 Oracle 中转换(转换)它们。 您还可以使用SYS_EXTRACT_UTCTO_UTC_TIMESTAMP_TZAT TIME ZONE更改时区

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM