英文:
Initialize static final Date using custom String
问题
我正在使用Java,并遇到一个随机的问题。
这里我分享了我的问题的示例代码。
我想要使用自定义字符串格式初始化一些静态最终日期字段。
public class Sample {
protected static final Date MAX_DATE ;
static {
try {
MAX_DATE = new SimpleDateFormat("yyyy-MM-dd").parse("2099-12-31");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
直接放置下面这行代码时,它会要求使用try和catch。
protected static final Date MAX_DATE= new SimpleDateFormat("yyyy-MM-dd").parse("2099-12-31");
当我像上面的代码中所提到的那样添加了try和catch时,它会抛出一个错误
变量 'MAX_DATE' 可能尚未初始化
当使用以下代码进行初始化时,在第5行开始抛出“无法为最终变量 'MAX_DATE' 分配值”的错误。
protected static final Date MAX_DATE=null;
有人可以帮助我解决这个问题吗?
英文:
I am working with Java and come through one random problem.
Here I had shared sample code of my problem.
I want to initialize some of static final date field with my custom string format.
public class Sample {
protected static final Date MAX_DATE ;
static {
try {
MAX_DATE = new SimpleDateFormat("yyyy-MM-dd").parse("2099-12-31");
} catch (ParseException e) {
e.printStackTrace();
}
}
}
While directly putting below line, it's asking for try and catch.
protected static final Date MAX_DATE= new SimpleDateFormat("yyyy-MM-dd").parse("2099-12-31");
When I had added try and catch as mentioned in above code, it's throwing an error
> Variable 'MAX_DATE' might not have been initialized
While initialize with below code, it started throwing an error of Cannot assign a value to final variable 'MAX_DATE'
on line number 5.
protected static final Date MAX_DATE=null;
Can somebody help me in this issue?
答案1
得分: 3
如果您只需要一个简单的日期,应该使用 LocalDate
而不是 Date
:
protected static final LocalDate MAX_DATE = LocalDate.of(2099, 12, 31);
如果(出于任何原因)必须从字符串中获取日期,也可以按以下方式使用:
protected static final LocalDate MAX_DATE = LocalDate.parse("2099-12-31");
如果确实有以下严格要求:
- 必须从具有任意模式的字符串中解析日期,并且
- 必须使用经典的
java.util.Date
可以使用类似以下方式实现:
protected static final Date MAX_DATE = Date.from(LocalDate.parse("2088||12||31", DateTimeFormatter.ofPattern("yyyy||MM||dd")).atStartOfDay(ZoneId.systemDefault()).toInstant());
英文:
If you just need a plain date, you should use LocalDate
instead of Date
:
protected static final LocalDate MAX_DATE = LocalDate.of(2099, 12, 31);
If (for whatever reason) the date has to be taken from a String, you can also use it as follows:
protected static final LocalDate MAX_DATE = LocalDate.parse("2099-12-31");
In case it is really a hard requirement to
- have the date parsed from String of arbitrary pattern and
- use good ol'
java.util.Date
something like that should do the trick:
protected static final Date MAX_DATE = Date.from(LocalDate.parse("2088||12||31", DateTimeFormatter.ofPattern("yyyy||MM||dd")).atStartOfDay(ZoneId.systemDefault()).toInstant());
答案2
得分: 1
-
将
protected static final Date MAX_DATE;
更改为protected static final Date MAX_DATE = null;
并保留 try-catch 块。 -
要消除 try-catch 块 - 在
Sample
和{
之间添加throws ParseException
。
英文:
You can:
-
Change
protected static final Date MAX_DATE;
toprotected static final Date MAX_DATE = null;
and keep try-catch block -
To get rid of try-catch block - add
throws ParseException
betweenSample
and{
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论