0

I am trying to fill 2 date objects, one in Local time and the other in UTC.

I AM NOT TRYING TO PRINT THE DATE AS A STRING IN GMT/UTC, please do not suggest DateFormatting, and dont say its a duplicate until you read the full question.

Local, I have no problem:

Date dateLocal = new Date();

The problem is I cant get the utcDate to be UTC.

Using a Calendar like so:

TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.setTimeZone(TimeZone.getTimeZone(utcTimeZone.getID()));
Date utcDate = c.getTime();

When debugged or submitted to the webservice, utcDate shows in my local timezone, instead of UTC.

Using Joda:

DateTime utcDateTime = DateTime.now(DateTimeZone.UTC);
Date utcDate = utcDateTime.toDate();

Same issue, utcDate when debugged/submitted to webservice is showing in local time.

Here is how the object looks when debugged:

enter image description here

This is an issue because this causes the webservice (which i have no access to) to think this time is UTC, so when it does its work and conversions, the time is always off by 4 hours, since for me the UTC to Local conversion is GMT -4.

The ONLY way i have been able to get this to submit the date in UTC time is by adding:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"));

BUT this also changes the LocalTime object, even though this object was defined and set before the default TimeZone was changed.

So i get it, the Date() object uses the JVM locale, so any time a Date is created, its created in the default timezone, and apparently whenever the default timezone is changed, all of the Date objects (even if they are already created) change to the new default timezone... I know Date objects are just the millis between now and 1970 whatever, but the TimeZone is obviously being taken into account in the Webservice and this is messing up my results...how can i get the dates the way i want?

Community
  • 1
  • 1
RH201
  • 312
  • 3
  • 16
  • 1
    Java `Date` objects don't even have a timezone, they are just an abstract point in time. You would either need to use a `Calendar`, or tap into the newer Java 8 date API to do what you want here (I think). – Tim Biegeleisen Sep 04 '18 at 14:25
  • 1
    To piggyback on what @TimBiegeleisen said, you can expect that `Date` objects store only the UTC time, internally, and carry no time zone information. The reason you see an EDT time in your debugger is that the `Date.toString()` function has added information from your device's time-zone settings as it was creating the string. – greeble31 Sep 04 '18 at 15:27
  • Possible duplicate of [Java: Calculate month end date based on timezone](https://stackoverflow.com/questions/51644208/java-calculate-month-end-date-based-on-timezone) – Ole V.V. Sep 05 '18 at 09:24
  • And indeed this has been asked and answered before. What you really need is a (or rather two) `ZonedDateTime` from [java.time. the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). You may use java.time on a not brand new Android through the backport, [the ThreeTenABP library](https://github.com/JakeWharton/ThreeTenABP). – Ole V.V. Sep 05 '18 at 09:24
  • If you are not formatting your `Date` into a string, how are you submitting it to the web service? Of course there is a way to give the web service the correct time, you just haven’t given us enough information for us to tell you how. BTW the `Date` class is long outdated and poorly designed, same for `Calendar` and `TimeZone`. Consider java.time instead. – Ole V.V. Sep 05 '18 at 10:45

0 Answers0