package com.test; import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; public class ConvertLongToInstantUtil { public static void main(String[] args) { System.out.println(parse("2018-09-18T19:01:51.821-04:00")); System.out.println(parseToInstant(1545955200000L)); System.out.println(parseInstantToDefaltDateTime(Instant.now())); } private static String parseInstantToDefaltDateTime(Instant instance) { DateTimeFormatter target = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").withZone(ZoneId.systemDefault()); return target.format(instance); } private static OffsetDateTime parseToOffsetDateTime(long instance) { return Instant.ofEpochMilli(instance).atOffset(ZoneOffset.ofHours(-4)); } private static Instant parseToInstant(long instance) { return Instant.ofEpochMilli(instance); } private static long parse(String instance) { OffsetDateTime time = OffsetDateTime.parse(instance); return time.toInstant().toEpochMilli(); } }