skip to Main Content

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

Answers


  1. 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"))
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search