java.date 废弃,Java中的Date(String)的不被废弃的完全等价物?

I have old code that uses new Date(dateString) to parse a date string. Compiling the code produces the deprecation warning Date(java.lang.String) in java.util.Date has been deprecated.

The javadoc unhelpfully advises me to use DateFormat.parse(), even though the DateFormat class does not have a static parse method.

Now, I know how to use SimpleDateFormat, but I want to make sure I'm getting the exact same behaviour of the deperecated Date constructor.

解决方案

Here's my guess (I posted as community wiki so you can vote up if I'm right):

Date parsed = new Date();

try {

SimpleDateFormat format =

new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

parsed = format.parse(dateString);

}

catch(ParseException pe) {

throw new IllegalArgumentException();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在MyBatis,当你在查询条件将`java.util.Date`类型的属性与`java.lang.String`类型的属性进行比较时,可能会出现报错信息"invalid comparison: java.util.Date and java.lang.String"。这是因为在比较过程,MyBatis无法将`java.util.Date`类型的属性与`java.lang.String`类型的属性进行正确的比较。 为了解决这个问题,你可以使用MyBatis提供的类型处理器来处理`java.util.Date`类型的属性。类型处理器可以将`java.util.Date`类型的属性转换为数据库的日期类型,以便正确比较。 以下是一个示例,展示了如何在MyBatis使用类型处理器来解决`java.util.Date`和`java.lang.String`比较的问题: 1. 首先,在你的MyBatis配置文件,添加类型处理器的配置: ```xml <typeHandlers> <typeHandler handler="org.apache.ibatis.type.DateTypeHandler" /> </typeHandlers> ``` 2. 然后,在你的Mapper接口,将`java.util.Date`类型的属性与`java.lang.String`类型的属性进行比较: ```xml <select id="selectByDateAndString" parameterType="map" resultType="YourResultType"> SELECT * FROM your_table WHERE date_column = #{dateProperty, jdbcType=DATE} AND string_column = #{stringProperty, jdbcType=VARCHAR} </select> ``` 在上面的示例,`date_column`是数据库表的日期类型列,`string_column`是数据库表的字符串类型列。`dateProperty`和`stringProperty`是你传入的参数,分别对应`java.util.Date`类型的属性和`java.lang.String`类型的属性。 通过使用类型处理器和正确设置jdbcType,你可以避免在MyBatis比较`java.util.Date`和`java.lang.String`类型时出现报错。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值