java json日期转换,将Json日期转换为java日历

博主在尝试将包含日期值的字符串(如:1234043600000)转换为Java Calendar类型时遇到困难。错误出现在使用`substr(6)`方法上,因为该方法在Java String类型中不存在。解决方案是使用正则表达式移除非数字部分,然后通过`Long.parseLong`解析为长整型,再创建Date对象。最后设置到Calendar实例中。
摘要由CSDN通过智能技术生成

I have the following value /Date(1234043600000)/ in string type and I need to

convert it to java calendar type without a success,I have tried to use the following post and create date and than do something like the post

Date date = new Date(parseInt(jsonDate.substr(6)));

and than do someting like

Calendar cal = Calendar.getInstance();

cal.setTime(date);

I got error in the first line since in the word date i have line in the middle and substr(6) have error (The method substr(int) is undefined for the type String) ,how should I continue .

Thanks!

解决方案

This should work in Java

Date date = new Date(Long.parseLong(jsonDate.replaceAll(".*?(\\d+).*", "$1")));

the problem with your example is that it's only good for javascript

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值