简体   繁体   English

声纳-将DATE_FORMAT设置为实例变量

[英]Sonar - Make DATE_FORMAT as instance variable

I have a rest web service, and below is how i have declared DateFormat as this is the date format i am going to use application wide. 我有一个休息的Web服务,下面是我如何声明DateFormat的信息,因为这是我将在应用程序范围内使用的日期格式。

When i did code analysis using SonarLint eclipse plug-in, i got major warning saying "Make DATE_FORMAT as instance variable." 当我使用SonarLint eclipse插件进行代码分析时,我得到重大警告,提示“将DATE_FORMAT设置为实例变量”。

public class Constants {

    private Constants() {

    }

    public static final DateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS");

}

Can anyone tell me what issue i might face if i use it this way in my rest API ? 任何人都可以告诉我,如果我在其余的API中使用这种方式会遇到什么问题?
If i use it as instance variable i will end up declaring it in multiple classes ? 如果我将其用作实例变量,我最终将在多个类中对其进行声明?

Static variable are mostly used for Constants. 静态变量主要用于常量。
Here you have declared static and assigning it instance of SimpleDateFormat . 在这里,您已经声明了static并为其分配了SimpleDateFormat实例。
Either make DATE_TIME_FORMAT non-static or assign a constant to this variable. 使DATE_TIME_FORMAT为非静态,或为该变量分配一个常量。

Better change it to instance variable and use a Sting to do that. 最好将其更改为实例变量,然后使用Sting做到这一点。
eg public final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss:SSS"; 例如, public final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss:SSS";

The triggered rule is S2885: 触发规则为S2885:

Non-thread-safe fields should not be static 非线程安全字段不应为静态

squid:S2885 鱿鱼:S2885

Not all classes in the standard Java library were written to be thread-safe. 并非标准Java库中的所有类都被编写为线程安全的。 Using them in a multi-threaded manner is highly likely to cause data problems or exceptions at runtime. 以多线程方式使用它们很可能在运行时导致数据问题或异常。 This rule raises an issue when an instance of Calendar, DateFormat, javax.xml.xpath.XPath, or javax.xml.validation.SchemaFactory is marked static. 当Calendar,DateFormat,javax.xml.xpath.XPath或javax.xml.validation.SchemaFactory的实例标记为静态时,此规则会引起问题。

Since SimpleDateFormat is not thread safe, it doesn't work well with being shared between threads. 由于SimpleDateFormat并不是线程安全的,因此不能在线程之间共享时很好地工作。 You might well end up with wrong formatting of dates. 您可能会以错误的日期格式结尾。

If you are using Java 8 or above you should use DateTimeFormatter , as in this answer . 如果您使用的是Java 8或更高版本,则应使用DateTimeFormatter ,如本答案所示 Otherwise, using Joda Time makes sence, as per this answer . 否则,根据此答案 ,请使用Joda Time进行提示。


As a side note, having a class named Constants end let it contain all sorts of static final variables rarely makes sense. 附带说明一下,让名为Constants end的类让它包含各种静态最终变量几乎没有道理。 Typically you should put each constant where it belongs. 通常,您应该将每个常数放在其所属的位置。

Use joda-time or simply replace variable with method: 使用joda-time或简单地用方法替换变量:

public static final DateFormat getDateTimeFormat() {
    return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSS");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM