0

Please read to the end‚ I will try to explain in detail.

I have one program‚ it only works for locals‚ in one country. Specifically, it is for one country but can be used for another. However, all hours, minutes and dates are adapted to one country. That's why I think the program should run on the date of that country‚ the date of the program should be taken from the actual date in that country, not from the calendar on the phone.

User open app: App show 21-November. (Date in that country) Phone data 20-November. (In his home country, the date is still November 20)

(the app should display the exact date of that country et regardless of my phone calendar.)

Don't you understand? Then leave your question as a comment‚ please help.

2 Answers2

1

Your question is not clear, but it seems you want to represent a single moment as viewed in two different time zones.

Capture the current moment as seen in UTC (with an offset of zero hours-minutes-seconds).

Instant instant = Instant.now() ;

Generally best to do your thinking, debugging, logging, storage, data-exchange, and most of your business logic in UTC. Adjust to a time zone mainly just for presentation to the user, and for particular business rules as required.

Adjust into one time zone.

ZoneId zEdmonton = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime zdtEdmonton = instant.atZone( zEdmonton ) ;

Adjust to another time zone.

ZoneId zTokyo = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdtTokyo = instant.atZone( zTokyo ) ;

Our three objects, the Instant and the pair of ZonedDateTime objects, all represent the very same moment, the same point on the timeline.

For a particular moment, the date in Tokyo may be “tomorrow” while still “yesterday” in Edmonton.

Basil Bourque
  • 262,936
  • 84
  • 758
  • 1,028
  • the code did not work as I expected. If I change the date in the phone's settings, the date shown in the app also changes. I didn't want the date shown in the program to be the date of the phone call. – JaloliddinPage Nov 27 '21 at 06:13
0

There are plenty of different ways to obtain it, this would be just two of them:

LocalDateTime.now().atOffset(ZoneOffset.of("-05:00")).toZonedDateTime();
LocalDateTime.now().atZone(ZoneId.of("Asia/Kuala_Lumpur"))
gmanjon
  • 1,395
  • 1
  • 10
  • 15
  • Never use `LocalDateTime` to track a moment, a specific point in the timeline. I cannot imagine any scenario where calling `LocalDateTime.now()` is the right thing to do – Basil Bourque Nov 21 '21 at 21:42
  • Hi! What would you use then? Because`Instant` is not TimeZone aware. – gmanjon Nov 21 '21 at 21:45
  • `Instant` represents a moment as seen in UTC, that is, with an offset-from-UTC of zero hours-minutes-seconds. Yes, use `Instant` to track a moment. – Basil Bourque Nov 21 '21 at 22:12
  • I totally agree that that is the proper way to do it, much more robust and scalable over time. But if you want everything in the local timezone and don't want to bother about conversions because you don't plan to scale up to other timezones (or you don't have the time). I mean, what's the difference between using always `Instant` and converting it always to the same timezone, than configuring the proper timezone in the server and use always `LocalDateTime`. It could save you some coding time. Unfortunately "pragmatic" and "proper" don't usually get along with each other. – gmanjon Nov 22 '21 at 11:27
  • Either way you answer is clearly better than mine. Upvoted. – gmanjon Nov 22 '21 at 11:30
  • How can I get data a variable? – JaloliddinPage Nov 27 '21 at 05:01
  • Sorry @JaloliddinPage, I don't understand the question. Could you please rephrase it? Thanks. – gmanjon Nov 28 '21 at 02:29