如何将 java.util.date 初始化为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30168556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize java.util.date to empty
提问by 99maas
I need your help in initializing a java.util.Date variable to empty. As I am running the page and it is showing nullpointerexception if I didn't select any date.
我需要您帮助将 java.util.Date 变量初始化为空。当我运行页面时,如果我没有选择任何日期,它会显示 nullpointerexception。
The code is:
代码是:
private java.util.Date date2;
I tried to make that variable empty, but it didn't work>
我试图将该变量设为空,但没有用>
private java.util.Date date2;
if (date2==null || date2.equals(""))
date2="";
However, with the initialization:
但是,通过初始化:
private java.util.Date date2= new java.util.Date(0,0,0);
The above code will give me a default value which I don't want it.
上面的代码会给我一个我不想要的默认值。
采纳答案by Blip
Instance of java.util.Datestores a date. So how can you store nothing in it or have it empty? It can only store references to instances of java.util.Date
. If you make it null
means that it is not referring any instance of java.util.Date
.
java.util.Date 的实例存储日期。那么你怎么能在里面什么都不存,或者让它空着呢?它只能存储对 的实例的引用java.util.Date
。如果你让它null
意味着它没有引用java.util.Date
.
You have tried date2="";
what you mean to do by this statement you want to reference the instance of String
to a variable that is suppose to store java.util.Date
. This is not possible as Java is Strongly Typed Language.
您已尝试date2="";
通过此语句执行您想要将 的实例引用String
到假设要存储的变量的意思java.util.Date
。这是不可能的,因为 Java 是强类型语言。
Edit
编辑
After seeing the comment posted to the answer of LastFreeNickname
看到张贴到LastFreeNickname答案的评论后
I am having a form that the date textbox should be by default blank in the textbox, however while submitting the data if the user didn't enter anything, it should accept it
我有一个表单,文本框中的日期文本框默认为空白,但是如果用户没有输入任何内容,则在提交数据时,它应该接受它
I would suggest you could check if the textbox is empty. And if it is empty, then you could store default date in your variable or current date or may be assign it null
as shown below:
我建议您可以检查文本框是否为空。如果它是空的,那么您可以将默认日期存储在您的变量或当前日期中,或者可以null
如下所示分配它:
if(textBox.getText() == null || textBox.getText().equals(""){
date2 = null; // For Null;
// date2 = new Date(); For Current Date
// date2 = new Date(0); For Default Date
}
Also I can assume since you are asking user to enter a date in a TextBox
, you are using a DateFormat
to parse the text that is entered in the TextBox
. If this is the case you could simply call the dateFormat.parse()
which throws a ParseException
if the format in which the date was written is incorrect or is empty string. Here in the catch
block you could put the above statements as show below:
另外我可以假设,由于您要求用户在 a 中输入日期TextBox
,因此您正在使用 aDateFormat
来解析在TextBox
. 如果是这种情况,您可以简单地调用dateFormat.parse()
whichParseException
如果写入日期的格式不正确或为空字符串,则抛出 a 。在catch
块中,您可以将上述语句如下所示:
try{
date2 = dateFormat.parse(textBox.getText());
}catch(ParseException e){
date2 = null; // For Null;
// date2 = new Date(); For Current Date
// date2 = new Date(0); For Default Date
}
回答by Kaushik
Try initializing with null value.
尝试用空值初始化。
private java.util.Date date2 = null;
Also private java.util.Date date2 = "";
will not work as "" is a string.
也private java.util.Date date2 = "";
不会工作,因为 "" 是一个字符串。
回答by LastFreeNickname
It's not clear how you want your Date logic to behave? Usually a good way to deal with default behaviour is the Null Object pattern.
不清楚您希望 Date 逻辑如何表现?通常处理默认行为的好方法是空对象模式。
回答by akhil_mittal
IMO, you cannot create an empty Date(java.util)
. You can create a Date
object with null
value and can put a null check.
IMO,您不能创建一个空的Date(java.util)
. 您可以创建一个Date
具有null
值的对象,并可以进行空检查。
Date date = new Date(); // Today's date and current time
Date date2 = new Date(0); // Default date and time
Date date3 = null; //Date object with null as value.
if(null != date3) {
// do your work.
}
回答by Michiel Scheepmaker
An instance of Date always represents a date and cannot be empty. You can use a null value of date2 to represent "there is no date". You will have to check for null whenever you use date2 to avoid a NullPointerException, like when rendering your page:
Date 的实例总是代表一个日期并且不能为空。您可以使用 date2 的空值来表示“没有日期”。每当您使用 date2 以避免 NullPointerException 时,您都必须检查 null,例如在呈现页面时:
if (date2 != null)
// print date2
else
// print nothing, or a default value