Hi I have a table from which I get a date like "2017-03-24 06:01:33" which is in UTC. In my java program I have to receive it as a string. How to convert it to a local date string using client's offset hour and minute?
-
1http://stackoverflow.com/questions/31395441/convert-utc-date-to-local-date – soorapadman Mar 27 '17 at 12:33
2 Answers
I would do:
OffsetDateTime odt = LocalDateTime.parse(dateFromDb, DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss"))
.atOffset(ZoneOffset.UTC);
String localDateString = odt.atZoneSameInstant(ZoneOffset.ofHoursMinutes(5, 30))
.toLocalDateTime()
.toString();
I am assuming you have got the client’s time zone as an offset in hours and minutes. Then you can pass those into from ZoneOffset.ofHoursMinutes(). Beware that if the client time zone uses summer time (daylight savings time) some of the year (as is the case in greater parts of the USA), you need to be sure you get the offset for the time of year where the date falls. Other ways of specifying the client time zone would be ZoneId.systemDefault() or ZoneId.of("Asia/Kolkata") — such would know the summer time rules and hence may be safer.
The code prints:
2017-03-24T11:31:33
You notice it’s five and a half hours ahead of the input string as expected.
If you only require the date, not the time, use toLocalDate() instead of toLocalDateTime().
All of the above requires Java 8. Or the backport of the Java 8 date and time classes to Java 6 and 7, see ThreeTen Backport. If you cannot use Java 8 and do not want to depend on the backport, find your inspiration in this question: Not able to parse UTC date time to EST local time.
-
V.V Where am I giving client's offset data here? I hope ZoneOffset.UTC, takes server's offset? – Virat Mar 27 '17 at 12:25
-
Does my edit explain, @Sandesha? Are you getting a time zone string from the client, or how? – Ole V.V. Mar 27 '17 at 12:30
-
I am getting's local offset minute and offset hour...If I have to display "2017-03-24 06:01:33" UTC as local in India, I will get India's offset hour as 5 and offset minute as 30 or If I have to display the same UTC date as local date in USA, I will get USA's offset hour and minute – Virat Mar 27 '17 at 12:36
-
I have edited again. Hope it helps. Please revert with more questions. @Sandesha – Ole V.V. Mar 27 '17 at 12:47
-
Thank you @Ole....It worked.. In the same way how to do reverse action...i.e converting from local date string utc? – Virat Mar 27 '17 at 17:12
-
Depends on your exact requirements. Local date only or date and time? What string format? Should your UTC time be at midnight in the client time zone, midnight in UTC or something else? Look into `LocalDateTime.atZone()` and `ZonedDateTime.format()`, for example. But the discussion will be too long for this format, better try something and then ask a new question. – Ole V.V. Mar 27 '17 at 17:26
-
1I suggest you use a `DateTimeFormatter` to generate the string. Converting to `LocalDateTime` creates confusion for learners reading this post, as the idea that “Local” means losing zone & offset data is not intuitive. Throwing away important data is not a good habit to teach. So for generating a string, use a formatter – that’s its job. We already have a formatter predefined fit for our purpose here: [`DateTimeFormatter.ISO_LOCAL_DATE_TIME`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_LOCAL_DATE_TIME). – Basil Bourque Mar 27 '17 at 19:59
Use this
simpleDateFormat.setTimeZone("Your-Client's-Timezone");
- 2,769
- 2
- 18
- 34
-
How to use that? Where should I pass my UTC String "2017-03-24 06:01:33" and how to get the local string in the format "yyyy-mm-dd"? @Sandeep – Virat Mar 27 '17 at 11:45
-
1In my humble opinion you shouldn’t suggest the old/outdated classes like `SimpleDateFormat` when the asker can use the more programmer-friendly date and time classes in Java 8. – Ole V.V. Mar 27 '17 at 20:01