Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Missing timezone of SimpleDateFormat Medium

Using a SimpleDateFormat object without setting its timezone can result in unexpected date and time. Use letter z, Z or X in the pattern, or call setTimeZone() on the created SimpleDateFormat object to avoid the issue.

Detector ID
java/simple-date-format-time-zone@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1void setTimezoneNoncompliant() {
2    // Noncompliant: does not set the timezone while using a 'SimpleDateFormat' object.
3    doSomething(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
4}

Compliant example

1void setTimezoneCompliant() {
2    // Compliant: sets the timezone while using a 'SimpleDateFormat' object.
3    doSomething(new SimpleDateFormat("yyyy-MM-dd'Z'").format(new Date()));
4}