如何检查Date参数在Java中是否具有某些值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9775199/
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 check if Date parameter has some value in Java
提问by Dragan
I'm moving from C# to java and I'm having troubles with the basics. I'm expecting a variable in my method of type Date and I need to check if the Date has been set or not. How do I do that in Java? using ,equals or == ??
我正在从 C# 转向 Java,但在基础知识方面遇到了麻烦。我期待我的 Date 类型方法中有一个变量,我需要检查 Date 是否已设置。我如何在 Java 中做到这一点?使用 ,equals 或 == ??
Thanks a lot!
非常感谢!
回答by Edwin Dalorzo
if ( myDate != null){
//do your stuff here
}
回答by Jon Skeet
I suspect you want:
我怀疑你想要:
public void foo(Date date) {
if (date != null) {
// Use date here
}
}
Note that unlike DateTime
in .NET, java.util.Date
is a class (and therefore a reference type)... Java doesn't have "custom" value types.
请注意,与DateTime
.NET不同的是,它java.util.Date
是一个类(因此是一个引用类型)……Java 没有“自定义”值类型。
Also note that that's not quite the same as whether or not the variable has been "set". For example:
另请注意,这与变量是否已“设置”不同。例如:
Date date = new Date();
date = null;
Would you consider that variable to be "set" or not? It's set to have a null reference as its value. I suspectyou want to know whether the variable has a value referring to an object, but that's not quite the same thing.
您会认为该变量是“设置”还是不“设置”?它被设置为有一个空引用作为它的值。我怀疑您想知道变量是否具有引用对象的值,但这并不完全相同。