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
DateTime
object tojava.util.Date
correctly. A correct way is to get the milliseconds fromDateTime
object and initialize thejava.util.Date
with the milliseconds.Output:
Note:
java.util.Date
does 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 theSimpleDateFormat
as shown above.I recommend you switch from the outdated and error-prone
java.util
date-time API andSimpleDateFormat
to the modernjava.time
date-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: