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