gpt4 book ai didi

java - java 8中从日期字符串到utc long

转载 作者:行者123 更新时间:2023-12-01 17:46:14 24 4
gpt4 key购买 nike

我正在使用 java 8。我收到一个日期字符串,例如“2019-01-01”,我必须将其转换为 UTC 长整型。我无法先将其转换为日期,因为它已转换为本地时区。

因此,在条目中我有“2019-01-01”,在退出中我需要 1546300800000

我需要在一行中完成此操作(使用 talend...)

欢迎任何帮助。

最佳答案

这是您要找的吗?

import java.time.LocalDate;
import java.time.ZoneId;

// one class needs to have a main() method
public class HelloWorld
{
// arguments are passed using the text field below this editor
public static void main(String[] args)
{

String strDate = "2019-01-01";

LocalDate localDate = LocalDate.parse(strDate);

// Replace <Continent> and <City> with correct values such as: Europe/Paris
// ZoneId zoneId = ZoneId.of("<Continent>/<City>");
ZoneId zoneId = ZoneId.systemDefault();

ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
long epoch = zdt.toEpochSecond();

System.out.println(epoch);
}
}

注意:如果您使用 LocalDateTime,这也适用

关于java - java 8中从日期字符串到utc long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55046919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com