I am using this code:
date - Date object from DatePicker, as string Thu Sep 10 00:00:00 GMT+03:00 2020
mDate = DateTime(date)
.withHourOfDay(0)
.withMinuteOfHour(0)
.withSecondOfMinute(0)
.withMillisOfSecond(0)
.toDate()
The result
mDate - Wed Sep 09 03:00:00 GMT+03:00 2020
What wrong with this?
You are not converting the
DateTimeobject tojava.util.Datecorrectly. A correct way is to get the milliseconds fromDateTimeobject and initialize thejava.util.Datewith the milliseconds.Output:
Note:
java.util.Datedoes not represent a Date/Time object. It simply represents the no. of milliseconds from the epoch of1970-01-01T00:00:00Z. It does not have any time-zone or zone-offset information. When you print it, Java prints the string obtained by applying the time-zone of your JVM. If you want to print it in some other timezone, you can do so using theSimpleDateFormatas shown above.I recommend you switch from the outdated and error-prone
java.utildate-time API andSimpleDateFormatto the modernjava.timedate-time API and the corresponding formatting API (package,java.time.format). Learn more about the modern date-time API from Trail: Date Time. If your Android API level is still not compliant with Java8, check How to use ThreeTenABP in Android Project and Java 8+ APIs available through desugaring.The following table shows an overview of modern date-time classes: