Sonar - 实例化 SimpleDateFormat 对象时,指定 Locale
Sonar - When instantiating a SimpleDateFormat object, specify a Locale
下面是我以 yyyy-MM-dd 格式设置日期的方法,代码按预期工作,但我收到声纳错误 - 在实例化 SimpleDateFormat 对象时,指定区域设置。任何建议专家如何解决这个问题?
public String getEndDate() throws ParseException {
Calendar date = Calendar.getInstance();
date.setTime(new Date());
Format f = new SimpleDateFormat("yyyy-MM-dd");
date.add(Calendar.YEAR,1);
return f.format(date.getTime());
}
您可以按照以下示例指定语言环境。
public static String getEndDate() {
Calendar date = Calendar.getInstance();
date.setTime(new Date());
Format f = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); // Locale
date.add(Calendar.YEAR,1);
return f.format(date.getTime());
}
值得注意的是,如果您省略语言环境,它将使用基于主机 JVM 的 Locale.getDefault()
。这可能是一个理想的功能。
下面是我以 yyyy-MM-dd 格式设置日期的方法,代码按预期工作,但我收到声纳错误 - 在实例化 SimpleDateFormat 对象时,指定区域设置。任何建议专家如何解决这个问题?
public String getEndDate() throws ParseException {
Calendar date = Calendar.getInstance();
date.setTime(new Date());
Format f = new SimpleDateFormat("yyyy-MM-dd");
date.add(Calendar.YEAR,1);
return f.format(date.getTime());
}
您可以按照以下示例指定语言环境。
public static String getEndDate() {
Calendar date = Calendar.getInstance();
date.setTime(new Date());
Format f = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); // Locale
date.add(Calendar.YEAR,1);
return f.format(date.getTime());
}
值得注意的是,如果您省略语言环境,它将使用基于主机 JVM 的 Locale.getDefault()
。这可能是一个理想的功能。