0

I have date like string 200901

On android device I convert it to date:

private static final SimpleDateFormat CARD_DATE_FORMAT = new SimpleDateFormat("yyMMdd", Locale.getDefault());
 public static Date toCardDateFormat(String date) {
        try {
            return CARD_DATE_FORMAT.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

and I get date: Tue Sep 01 00:00:00 GMT+07:00 2020

After that I pass this date to spring controller and in controllers method I have this date like: Mon Aug 31 20:00:00 GMT+03:00 2020

ip696
  • 6,574
  • 12
  • 65
  • 128
  • How do you pass the `date` back to the *Spring* controller? – tmarwen May 08 '18 at 10:39
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated along with `Date`, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). BTW a `Date` doesn’t have a time zone. Cannot have. – Ole V.V. May 08 '18 at 11:17
  • 1
    The solid solution is to use `LocalDate` from `java.time`. It’s date without time of day and without time zone. No room for confusion. :-) – Ole V.V. May 08 '18 at 11:19
  • For earlier Android, you should be using the *ThreeTenABP* project that adapts the *ThreeTen-Backport* project. Never use the terribly troublesome legacy date-time classes first bundled with the earliest versions of Java. – Basil Bourque May 08 '18 at 15:58

1 Answers1

0

You are parsing the date in the default timezone, which will depend on the timezone configured for the device, but it appears to be GMT+7

It looks like the server that's running Spring is in the GMT+3 timezone, and when it prints dates in its default timezone, that's what it uses.

The actual instant represented by 2020-09-01T00:00:00+07:00 and 2020-08-31T20:00:00+03:00 are the same.

This whole query is a result of two different computers printing the same instant as a date formatted in their local timezone.

ptomli
  • 11,730
  • 4
  • 40
  • 68