A
A
Artem2015-05-20 21:11:19
Java
Artem, 2015-05-20 21:11:19

How to make sure that the time in Date does not change when changing the time zone?

Situation: There are two fragments in my application. In the first snippet, the user selects a date, which is stored in a variable of type Date. Further, the user changes the time zone in the phone settings, presses the button. The recorded date is passed to the second fragment and displayed there in the TextView.
Problem: The dates on the first and second fragments do not match. That is, if the user had a time zone of -11 when choosing a date and +13 when viewing the second fragment, and on the first fragment he selected June 1, then on the second fragment he will see June 2.
How to make sure that the date does not change? Why is this happening?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2015-05-20
@bartwell

Why is this happening?
Date stores a moment in time. The problem is that at the same moment in different time zones, the clock has different times and the calendar has a different date. There are exactly 24 hours between -11 and +13, so any time will have a different date between those zones.
How to make sure that the date does not change?
Use Calendar (GregorianCalendar) or SimpleDateFormat: anything that saves timezone information.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question