Android calendar picking system date instead of time-zoned date

31 Views Asked by At

Requirement: I need to show current date and time according to given date time. For this, in device settings I disabled automatically setting of timezone and changed the date and time to let say: 2023-12-25 4:00 PM , but the correct date mostly in all timezones is 2024-02-14 , please see below code that I used, from this code I always get correct time according to the timezone but the date is not correct, I always get the date which is set in the system. How to get correct current date according to time-zone & not depending on system settings date?


        // Get the current date and time in the target time zone
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone(targetTimeZone));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone(targetTimeZone));

        String formattedDate = sdf.format(calendar.getTime());
        System.out.println("Current date and time in " + targetTimeZone + ": " + formattedDate);```

Yes I have debugged and checked that timezone is correctly set in calendar instance.

0

There are 0 best solutions below