0

I searched for solutions but still not working ,I'm trying to convert the date object's local timezone to UTC +0 , but when i format the date object to UTC it's working . but when I want to convert the converted string to date again, the format changes and UTC goes back to GMT+8 before i store it in the fire store. what is the problem in the code?

this is the current date object that i get

Calendar time = Calendar.getInstance();
time.setTimeZone(TimeZone.getTimeZone("UTC"));
Date current_time = time.getTime();

if printed

Thu Aug 22 10:09:55 GMT+08:00 2019

then i convert it to UTC

String dismissal_time_firestore;
Log.i(TAG, "Current time when swiped from phone time.getTime()  "+current_time);
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
dismissal_time_firestore = dateFormat.format(current_time);

got this

Thu, 22 Aug 2019 02:09:55 +0000

but then when i convert this string to a date object

try {
     current_time = dateFormat.parse(dismissal_time_firestore);
    } catch (ParseException e) {
      e.printStackTrace();
    }

i got this

 Thu Aug 22 10:09:55 GMT+08:00 2019
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
tasif99
  • 65
  • 1
  • 8
  • You can't. An old-fashioned `Date` object cannot have a time zone or offset. Do consider using the modern java.time through ThreeTenABP instead. – Ole V.V. Aug 22 '19 at 04:27

1 Answers1

2

You are using terrible date-time classes that were supplanted years ago by the modern java.time classes defined in JSR 310.

Your main problem is not understanding that the Date::toString method lies to you. It dynamically applies the JVM’s current time zone to the moment in UTC while generating the text. One of many reasons to never use this class.

Get current moment in UTC.

Instant instant = Instant.now() ;

View that moment as seen in the wall-clock time used by the people of a particular region (a time zone).

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

Generate text for display to user.

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.FULL ).withLocale( Locale.CANADA_FRENCH ) ;
String output = zdt.format( f ) ;

You talk about parsing formatted strings. Bad idea. Think of textual representations of date-time values only as outputs, not inputs. Collecting date-time inputs should be done by using date-time widgets, not typed text.

When storing or exchanging date-time values as text, always use ISO 8601 standard formats. The java.time classes use ISO 8601 formats by default when parsing/generating strings. So no need to specify any formatting pattern. Just call parse/toString. Example: Instant.now().toString() and Instant.parse( "2020-01-23T12:34:56.123456Z" ).

I cannot help further as you did not really say what you were trying to accomplish.

All of this has been covered many many times on Stack Overflow. So search to learn more. And search before posting.

Table of date-time types in Java (both legacy and modern) and in standard SQL

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you for you suggestion , i want the formatted string to convert to date object so that i can store it in the firestore. i found others suggesting to store it as a string but i need to make calculations and queries in other part of the app. may i know your suggestion for this? – tasif99 Aug 22 '19 at 03:16
  • @tasif99 When storing or exchanging date-time values as text, always use [ISO 8601](https://en.m.wikipedia.org/wiki/ISO_8601) standard formats. The *java.time* classes use ISO 8601 formats by default when parsing/generating strings. So no need to specify any formatting pattern. Just call `parse`/`toString`. Example: `Instant.now().toString()` – Basil Bourque Aug 22 '19 at 04:42