calendar wrong time after timezone change

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



calendar wrong time after timezone change



I need to get a date 5 month ago from current with the first day of month, I use this code to do it:


val date = Calendar.getInstance()
date.add(Calendar.MONTH, -5)
date.set(Calendar.DAY_OF_MONTH, 1)
date.set(Calendar.HOUR_OF_DAY, 0)
date.set(Calendar.MINUTE, 0)
date.set(Calendar.SECOND, 0)



It normally works fine, making from 2018-08-10T15:46:53+02:00 needed 2018-03-01T00:00:00+01:00


2018-08-10T15:46:53+02:00


2018-03-01T00:00:00+01:00



But if I change timezone to GMT+7, resume app and run this code, it gives me a strange result a few days before needed: 2018-02-28T20:00:00+01:00.


2018-02-28T20:00:00+01:00



If I remove app from memory and start it again after changing timezone, everything works fine.




1 Answer
1



Use library JodaTime then your code will look like this:


DateTime now = DateTime.now();
DateTime fiveMonthsAgo= now.minusMonths(5);



Otherwise, you must manually subtract the year when necessary.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

React Native Navigation and navigating to another Screen problem