英文:
java.lang.NullPointerException: null while assigning current date to variable
问题
在我的ISData类中,我创建了以下方法:
public static final DateFormat IN_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
现在在我的TSTIMP类中,我正在尝试将当前系统日期的值分配给变量import_date,该变量是Date数据类型,并采用IN_DATE_FORMAT的格式。但是不知道如何做。我想将此日期解析为字符串,因为我之后需要将其存储在数据库表中。目前我得到的错误是java.lang.NullPointerException: null
data.import_date = InstrumentMasterData.IN_DATE_FORMAT.parse(<需要提供系统日期>作为字符串)
以下是我针对变量effective_date尝试过的内容,它也是Date数据类型,它可以正常工作:
data.effective_date = allocDate != null ? InstrumentMasterData.IN_DATE_FORMAT.parse(allocDate作为字符串) : null
此外,我还有一个名为data.fund_quote_date的变量,它也是Date数据类型,应始终为null。如何将null值分配给这个变量?
英文:
In my ISData class i have create below method:
public static final DateFormat IN_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
and now in my TSTIMP class i am trying to assign the current sys date value in the IN_DATE_FORMAT format to variable import_date which is Date datatype. But dont know how to do that. I want to parse this date into String as i need to later on store this in database table. Currently i am getting error as java.lang.NullPointerException: null
data.import_date = InstrumentMasterData.IN_DATE_FORMAT.parse(<need to provide sysdate> as String)
Below i tried for variable effective_date which is also Date datatype and it works fine:
data.effective_date = allocDate != null ? InstrumentMasterData.IN_DATE_FORMAT.parse(allocDate as String) : null
Also i have data.fund_quote_date variable which is also Date datatype and it should always be null. How can i assign null value to this varaible ?
答案1
得分: 0
> 我正在尝试将当前的系统日期值按照 IN_DATE_FORMAT 格式赋值给变量 import_date,该变量是 Date 类型。但不知道如何实现。
您可以使用 DateFormat#format 来获取以 IN_DATE_FORMAT 格式表示的系统日期字符串,例如:
data.import_date = InstrumentMasterData.IN_DATE_FORMAT.parse(InstrumentMasterData.IN_DATE_FORMAT.format(new Date()) as String)
然而,这样做实际上是对一个 Date 对象进行了不必要的 IN_DATE_FORMAT 格式化,然后将相同格式的字符串解析回一个 Date 对象。与此相反,您可以简单地这样做:
data.import_date = new Date();
> 另外,我有一个名为 data.fund_quote_date 的变量,它也是 Date 类型,且应始终为 null。如何将 null 值赋予这个变量?
只需将其赋值为 null:
data.fund_quote_date = null;
注意: 如果可能的话,请停止使用过时且容易出错的 java.util 日期时间 API 和 SimpleDateFormat。请转而使用现代的 java.time 日期时间 API 以及相应的格式化 API (java.time.format)。您可以从**教程:日期时间**中了解有关现代日期时间 API 的更多信息。
英文:
> i am trying to assign the current sys date value in the IN_DATE_FORMAT
> format to variable import_date which is Date datatype. But dont know
> how to do that.
You can use DateFormat#format to get the system date string in the IN_DATE_FORMAT format e.g.
data.import_date = InstrumentMasterData.IN_DATE_FORMAT.parse(InstrumentMasterData.IN_DATE_FORMAT.format(new Date()) as String)
However, this is unnecessarily formatting a Date object using IN_DATE_FORMAT and then parsing the same formatted string back to a Date object. Instead of this, you can simply do:
data.import_date = new Date();
> Also i have data.fund_quote_date variable which is also Date datatype
> and it should always be null. How can i assign null value to this
> varaible ?
Simply, assign null to it:
data.fund_quote_date = null;
Note: If it is possible for you, stop using the outdated and error-prone java.util date-time API and SimpleDateFormat. Switch to the modern java.time date-time API and the corresponding formatting API (java.time.format). Learn more about the modern date-time API from Trail: Date Time.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论