这个问题在这里已有答案:
我正在使用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,这也适用
在一个(长期)行中按要求:
long t = LocalDate.parse("2019-01-01").atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli();
逻辑与the very good answer by acarlstein相同,除了他从纪元(1 546 300 800)开始获得秒数,我得到毫秒(1 546 300 800 000)。