我正在使用 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/