I'm trying to convert a java.util.Date
to java.time.LocalDate
:
[java.util.Date instance].toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
However, there is a case when [java.util.Date instance]
has day time of for example: 22:25 and I choose ZoneId like this for example: ZoneId.of("Asia/Singapore")
.
The outcoming LocalDate
will have different day (because that zone adds +6 hrs) as for example Europe/Minsk:
printing java.util.Date: Thu Mar 04 22:25:00 CET 2021
printing converted java.util.Date to java.time.LocalDate with Europe/Minsk: 2021-03-04
printing converted java.util.Date to java.time.LocalDate with Asia/Singapore: 2021-03-05
and I want to preserve same date in the resulting LocalDate
's as in that Date
instance. Some approaches how to solve this? I guess there could be several ways.
Instant
always gives you date-time in UTC
and if you want to maintain the same date, you need to use the zone offset of UTC
.
Demo:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Locale;
public class Main {
public static void main(String[] args) throws ParseException {
String strDateTime = "10/02/2021 22:25";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm", Locale.ENGLISH);
Date date = sdf.parse(strDateTime);
Instant instant = date.toInstant();
ZonedDateTime zdt = instant.atZone(ZoneOffset.UTC);
LocalDate localDate = zdt.toLocalDate();
System.out.println(localDate);
}
}
Output:
2021-10-02
Learn more about the modern date-time API from Trail: Date Time .
The java.util
date-time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern date-time API . For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project .
You can just convert it to instant, set a time zone to UTC, because java.util.Date is using UTC.
public static LocalDate convert (Date date) {
return date.toInstant()
.atZone(ZoneId.of("UTC"))
.toLocalDate();
}
You could simply copy the values of Date
to LocalDate
without transforming the data:
LocalDate.of(date.getYear(), date.getMonth()+1, date.getDate());
I am sorry, you are asking the impossible. Despite its name the outdated Date
class does not represent a date. It represents a point in time. At that point in time there could easily be two different dates in Singapore and Minsk. There is no such thing as to preserve same date since you didn't have a date from the outset.
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.