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
Question
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
How do I format a Microsoft JSON date?
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!
Solution
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
OTHER TIPS
The easiest way for you to get the number is to use
Long dateInMiliSeconds = new Scanner(jsonDate).nextLong();
Date date = new Date(dateInMiliSeconds);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
Also the reason for your compilation error is that the method to get a sub string in Java String
class is called subString
not substr
.
Although using the scanner is cleaner if you don't know much regex Evgeniy's answer may be better performance wise. I have no idea about the two approaches performance differences.