[英]new Date(year,month,day) is deprecated
I have a class A_Class
which one of it's constructor parameters is of type Date
and when I try to initialize an object of the class in my main for example:我有一个
A_Class
类,它的构造函数参数之一是Date
类型,当我尝试在 main 中初始化该类的对象时,例如:
A_Class aClass = new A_Class( param1, param2, new Date(1995,01,04) );
The IDE tells me that this format of Date
is deprecated. IDE 告诉我这种
Date
格式已被弃用。
Isn't there another way of directly passing a new date to the constructor or do I always have to do it like this:有没有另一种直接将新日期传递给构造函数的方法,或者我总是必须这样做:
final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",Locale.US);
String dateInString1 = "1995-Jan-4";
Date dateInDate1 = formatter.parse(dateInString1);
A_Class aClass = new A_Class( param1, param2, dateInDate1 );
You are using a terrible date-time class that is now legacy.您正在使用一个糟糕的日期时间类,该类现在已成为遗留。 Supplanted years ago by the modern java.time classes.
多年前由现代java.time类补充。
LocalDate
For a date-only value without time-of-day and without time zone, use LocalDate
.对于没有时间和时区的仅日期值,请使用
LocalDate
。
LocalDate localDate = LocalDate.of( 1995 , 1 , 5 ) ;
Generate text in standard ISO 8601 format YYYY-MM-DD.以标准 ISO 8601 格式 YYYY-MM-DD 生成文本。
String output = localDate.toString() ;
Design your class to hold a LocalDate
rather than a Date
.设计你的类来保存
LocalDate
而不是Date
。
public class Employee
{
private String givenName, surName ;
LocalDate whenHired ;
// Constructor
public Employee( String givenName , String surName , LocalDate whenHired )
{
…
}
}
Example usage.示例用法。
Employee alice = new Employee( "Alice" , "Anderson" , LocalDate.of( 1995 , Month.JANUARY , 4 ) ) ;
new Date(1995,01,04)
新日期(1995,01,04)
Do not start a literal integer with a zero unless you mean octal (base 8) rather than decimal (base 10).不要以零开头文字整数,除非您指的是八进制(基数 8)而不是十进制(基数 10)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.