0

I am trying to convert my datetime that is in local timezone into UTC date time.

    Date localDate; // this is local date 

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ") ;
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    String dateStr = simpleDateFormat.format(localDate);

i am getting proper converted UTC time in dateStr now i want to convert it into Date object with UTC timezone only but the moment i do that i am again getting the localDate.

    //converting string to date object

    simpleDateFormat.parse(dateStr)

does anyone know how can i convert local date object to UTC date object

here is the value i am getting while debugging

enter image description here

here dateStr is showing proper date in UTC but utcDate object is showing the local time

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Hunt
  • 8,215
  • 28
  • 116
  • 256
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 10 '19 at 10:45
  • 2
    You cannot. A `Date` doesn’t have a time zone. (And no, your screen shot doesn’t show the value of the `Date`, it shows the result of calling its `toString` method. Confusingly `Date` uses your JVM’s time zone for generating the string returned from `toString`.) – Ole V.V. Jan 10 '19 at 10:47

1 Answers1

0

Date class is intended to reflect coordinated universal time (UTC) time. It can be formatted into ANY form you want, e.g. you can format it into your local time zone or UTC time zone.

See from javadoc: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html

Although the Date class is intended to reflect coordinated universal time (UTC), it may not do so exactly, depending on the host environment of the Java Virtual Machine. Nearly all modern operating systems assume that 1 day = 24 × 60 × 60 = 86400 seconds in all cases. In UTC, however, about once every year or two there is an extra second, called a "leap second." The leap second is always added as the last second of the day, and always on December 31 or June 30. For example, the last minute of the year 1995 was 61 seconds long, thanks to an added leap second. Most computer clocks are not accurate enough to be able to reflect the leap-second distinction.

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • Well how exactly this is related to my problem as when I store date it turns back into local timestamp and pass that time to server , rather I want to pass UTC – Hunt Jan 10 '19 at 09:14
  • It is just a formatting about what time zone. If you want to get the UTC absolute milliseconds, you can just call `date.getTime()`, which is UTC value already. – shizhen Jan 10 '19 at 09:17
  • for client and server transmission, it will be better to pass **milliseconds** rather than the **String** format. This is because, millisecond is an absolute value and can be converted into any format per your requirement, String format usually need an additional parameter saying which time zone it is, so that the server side can interpret properly. – shizhen Jan 10 '19 at 09:20
  • 1
    in third party server we need to pass date in `yyyy-MM-dd'T'HH:mm:ssZZZZZ` format and the data type that holds this time is of type `Date` – Hunt Jan 10 '19 at 09:50
  • see my edits , it shows the data i am getting while debugging – Hunt Jan 10 '19 at 10:09
  • @Hunt You said in the question that you can already produce a string in that format in UTC. So what is the problem? Just pass that string. A `Date` cannot have a format. A format is always in a string. – Ole V.V. Jan 10 '19 at 10:51