how to round off a date which is in UTC?

187 views Asked by At

I am trying to round off a date object to the nearest date.

for eg: let date = 12th march 4:00 UTC (offset = 4:00).

I want to round the date to the nearest date after converting it into local time with the Timezone(TZ) information provided.

So now, if I convert this date with TZ as America/Los_Angeles, the date becomes: 11th March 9 PM. Now, I want to round this to nearest date. So it should become 12th March 00:00:00.

How do I do this?

The date object does not store TZ information. It only stores number of milli seconds after epoch. So I cannot use Apache DateUtils to convert because I want to convert only after I have converted the date object to local time.

The only way I can get the local time off of a date object is by using DateFormatter and passing TZ information. This will return a string to me according to the format passed to the date formatter. How do i round this string to the nearest date? I know how to parse it manually and then do it. However, I am looking for a cleaner, simpler and easier way to do it.

Is there any such way?

Thanks!

0

There are 0 answers