-2

Good day,

I am working on a project reporting.

and its my first time i have to deal with datetime.

I have database mongodb, as we know mongodb stores date time in UTC.

now i would like to show data from users provided date and time zone.

for example if i am login in my system i can set my timezone from dropdown. say i choose GMT+05:00 now if i choose date start and end as 2018-07-05 and 2018-07-06

how can i get the proper time with user specified time zone.

I guess if user has selected the time zone GMT+05:00 then date must be start from 2018-07-04 19:00:00 and 2018-07-05 19:00:00 minus 5 hours from given time.

how can i achieve this is java.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
cool cool
  • 67
  • 10
  • I think you can't get this information on backend. You need a client-side method to get the timezone. – Glim Apr 26 '18 at 21:37
  • i have stored user selected time zone in session – cool cool Apr 26 '18 at 21:38
  • Java 8 has a bunch of nice time classes for this. You can convert a UNIX epoch value to an Instant using `Instant.ofEpochSecond` or similar, then convert to a ZonedDateTime via the `atZone` method. Zones come from `TimeZone.getTimeZone(String).toZoneId`. Then format using DateTimeFormatter. – Hitobat Apr 26 '18 at 21:51
  • 1
    (a) Give examples of your inputs and outputs. (b) How have none of the thousands of existing date-time Questions & Answers not addressed your needs? – Basil Bourque Apr 26 '18 at 22:32

2 Answers2

1
    String userTimeZone = "Asia/Samarkand";
    String userDate = "2018-07-05";

    ZoneId zone = ZoneId.of(userTimeZone);
    Instant dbInstant = LocalDate.parse(userDate)
            .atStartOfDay(zone)
            .toInstant();

    System.out.println(dbInstant);

This prints what you had expected:

2018-07-04T19:00:00Z

I don’t know MongoDB’s JDBC driver, but I assume it would be happy to accept an Instant and store it in UTC in the database.

GMT+05:00 is not really a time zone, it’s a GMT offset. If your user is in a time zone that uses the same UTC offset always, it would work. But politicians tend to change their minds, so even if that time zone doesn’t use summer time (DST), it may do in a couple of years. And very many time zones already do. Therefore your user should pick a proper time zone like Asia/Tashkent, for example.

Edit: I understand from your comment that MongoDB expects a java.util.Date object. Funny and old-fashioned, but in that case the conversion is straightforward when you know how:

    Date dbDate = Date.from(dbInstant);
    System.out.println(dbDate);

On my computer in Europe/Copenhagen time zone this printed:

Wed Jul 04 21:00:00 CEST 2018

Don’t be fooled: this is the correct time. Date.toString (implicitly called through System.out.println) grabs my JVM’s time zone setting and uses it for generating the string. The Date itself doesn’t have a time zone in it and holds the same point in time as the Instant.

Link: Oracle tutorial: Date Time

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

If you already have the user selected time zone what you need to do is parse the date from DB to GMT:

Date dateFromDb = getDateFromDb(); // date from db
LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.of("GMT")); // parsing date to GMT but using LocalDateTime
Date correctDate = Date.from(localDateTime); // transforming into java.util.Date
Glim
  • 361
  • 1
  • 10
  • i am getting the date from user in string date – cool cool Apr 26 '18 at 22:05
  • **`LocalDateTime` is the wrong class** for this purpose. It lacks any concept of time zone or offset-from-UTC. So it does *not* represent a moment, a specific point on the timeline. – Basil Bourque Apr 26 '18 at 22:33