I what to convert selected date to milliseconds without timezone difference. Below is my code.
String selectedDate=Jan 18, 2020;
SimpleDateFormat format = new SimpleDateFormat("MMM dd, yyyy", Locale.US);
Date date = format.parse(selectedDate);
while running I am getting date like Sat Jan 18 00:00:00 GMT+05:30 2020. But I want without timezone difference like Jan 18, 2020.
For a difference of 0 from UTC:
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MMM dd, uuuu", Locale.ENGLISH);
String selectedDate = "Jan 18, 2020";
ZonedDateTime zdt = LocalDate.parse(selectedDate, dateFormatter)
.atStartOfDay(ZoneOffset.UTC);
System.out.println(zdt);
long millisSinceEpoch = zdt.toInstant().toEpochMilli();
System.out.println(millisSinceEpoch);
Output from this snpipet is:
2020-01-18T00:00Z 1579305600000
I am using java.time, the modern Java date and time API. The classes that you used, SimpleDateFormat
and Date
, are poorly designed and long outdated, and no one should use them anymore.
java.time works nicely on both older and newer Android devices. It just requires at least Java 6 .
org.threeten.bp
with subpackages.java.time
was first described. java.time
to Java 6 and 7 (ThreeTen for JSR-310).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.