google / google/gson

ISO8601Utils assumes local timezone while parsing "yyyy-MM-dd" dates

Open
#1,511 2 comments 1 reaction 0 assignees View on GitHub
bug
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

Parsing a "yyyy-MM-dd" date without a timezone using

```
ISO8601Utils.parse(dateString, ParsePosition(0)).toInstant()
```

applies a timezone from a default locale which leads to an incorrect result.

Input: `2019-03-20`
Actual Output: `2019-03-19T23:00:00Z` when a system default locale is UTC+01
Expected Output: `2019-03-20T00:00:00Z` regardless of the system locale

I think the problem is that `GregorianCalendar` must be preconfigured with the UTC timezone, and it's not done in `ISO8601Utils`:
```
// if the value has no time component (and no time zone), we are done
boolean hasT = checkOffset(date, offset, 'T');

if (!hasT && (date.length() <= offset)) {
Calendar calendar = new GregorianCalendar(year, month - 1, day);

pos.setIndex(offset);
return calendar.getTime();
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.