INTER.DTS
A Date or Time variable should not be followed by 'toString()' in an internationalized environment
Description
This rule flags date or time variables that are followed by `toString()'.
For code to be able to run in an internationalized environment, a date variable cannot be followed by '.toString()' because date and time formats differ with region and language.
Example
package INTER;
import java.util.*;
import java.awt.*;
public class DTS{
public void displaydate(){
Date today = new Date();
String dateOut = today.toString(); //violation
System.out.println(dateOut);
}
}
Repair
If you use the date-formatting classes, your application can display dates and times correctly around the world. The "DateFormat" class provides predefined formatting styles that are locale-specific and easy to use. The following code is an example of how to use the "DateFormat" class.
Date today;
String dateOut;
DateFormat dateFormatter;
dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT,cur-
rentLocale);
today = new Date();
dateOut = dateFormatter.format(today);
System.out.println(dateOut + " " + currentLocale.toString());
Reference
http://java.sun.com/docs/books/tutorial/i18n/intro/checklist.html
http://java.sun.com/docs/books/tutorial/i18n/format/dateFormat.html
|