How to initialize java.util.date to empty

后端 未结 5 1796
粉色の甜心
粉色の甜心 2021-01-04 02:12

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 s

相关标签:
5条回答
  • 2021-01-04 02:33

    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:

    if (date2 != null)
      // print date2
    else
      // print nothing, or a default value
    
    0 讨论(0)
  • 2021-01-04 02:36

    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.

    0 讨论(0)
  • 2021-01-04 02:39

    Try initializing with null value.

    private java.util.Date date2 = null;
    

    Also private java.util.Date date2 = ""; will not work as "" is a string.

    0 讨论(0)
  • 2021-01-04 02:41

    Instance of java.util.Date stores 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.

    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.

    Edit

    After seeing the comment posted to the answer of 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:

    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:

    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
    }
    
    0 讨论(0)
  • 2021-01-04 02:51

    IMO, you cannot create an empty Date(java.util). You can create a Date object with null value and can put a null check.

     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.
     }
    
    0 讨论(0)
提交回复
热议问题