-3

I have a json request with one date field as follows:
"PolicyEffectiveDate": "\/Date(1389715867000)/\"

I want to parse it into java object of type java.util.Date I got how to do it in javascript date in javascript here and in ionic here but I need to do this in java. Please help.

Jabongg
  • 2,099
  • 2
  • 15
  • 32
  • Is there a possibility to send date as timestamps (long) instead of `/Date(1389715867000)/` send just `1389715867000` ?? – Maraboc Sep 19 '17 at 10:46
  • No, actually this request is coming from a third party as it is. and I need to convert and save to db as valid java.util or sql date. – Jabongg Sep 19 '17 at 10:47
  • 2
    Have you tried to extract the long value from that string and to create a `new Date(value)` from it? – Oleg Estekhin Sep 19 '17 at 10:50
  • So you can do it like this : `new Date(Long.valueOf(PolicyEffectiveDate.replaceAll("[\\D]", "")));` with `PolicyEffectiveDate` the value of the json withe the key `PolicyEffectiveDate` – Maraboc Sep 19 '17 at 10:57
  • 4
    "I need this date to be parsed in format dd/MM/yyy in java.util.date" - what do you mean by that? It's not in dd/MM/yyyy format, and java.util.Date doesn't *have* a format, so where does dd/MM/yyyy come in? – Jon Skeet Sep 19 '17 at 11:14
  • the long value inside parenthesis is long value... but when i tried to convert to new java.util.Date(); i get Exception in thread "main" java.lang.IllegalArgumentException at java.util.Date.parse(Date.java:617) It's not valid argument. What I need is something like 19/09/2017 format. – Jabongg Sep 19 '17 at 11:17
  • 2
    No, that's not what you need. What you need is to extract the numeric part of your input string as a long, then construct a Date (or, preferrably, a Java 8 Instant) from that long value representing the number of millis since the epoch. Then, once you have that Date or Instant object, you can convert it to another String using the format (and timezone) you want. A Date/Instant doesn't have any format. Just like a double, even if you want to display it as 2,345.67, doesn't have any format. – JB Nizet Sep 19 '17 at 11:28
  • 1
    @JBNizet I have modified my question. Thanks for your valuable comments. – Jabongg Sep 19 '17 at 11:40
  • And what exactly don't you understand in all the answers and comments you got already? – JB Nizet Sep 19 '17 at 11:43
  • 1
    As an aside, is there any reason why you are still using the long outdated `Date` class? Today we have so much better in [the modern java date and time API known as `java.time` or JSR-310](https://docs.oracle.com/javase/tutorial/datetime/). JB Nizet already said in [a comment](https://stackoverflow.com/questions/46298350/json-date-date1389715867000-to-convert-to-java-util-date-in-java#comment79560683_46298350) that you should prefer an `Instant`. I agree 200 %. – Ole V.V. Sep 19 '17 at 13:14

3 Answers3

1
String policyEffective = ...

long time = Long.parseLong(policyEffective.replaceFirst("^.*Date\\((\\d+)\\).*$", "$1"));
Date date = new Date(time);

The number is a long, ms since 1970 or so, and goes as such into the Date constructor.

Result:

Tue Jan 14 17:11:07 CET 2014

Since you got an illegal argument; maybe you took an int or something else.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1
long time = Long.parseLong(policyEffective.subString(
policyEffective.indexOf("(")+1,policyEffective.lastIndexOf(")"))));
Date date = new Date(time);

this way will trim the string from the index of "(" to ")" so it will extract only the number

Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
1
import java.util.Date;
public class JsonDate {

    public static void main(String[] args) {
        String jsonDate = "\\" + "/Date(1389715867000)" + "/" + "\\";
        jsonDateToUtilDateConverter(jsonDate);
        System.out.println(jsonDate);
    }

    private static void jsonDateToUtilDateConverter(String jsonDate) {
        String epochDate = jsonDate.replaceAll("[^0-9]", "");

        Date date = new Date(Long.parseLong(epochDate));
        System.out.println("date: "+date);
        System.out.println(epochDate);

    }
}

And output I've got is:

date: Tue Jan 14 21:41:07 IST 2014
1389715867000
/Date(1389715867000)/\

Jabongg
  • 2,099
  • 2
  • 15
  • 32
  • 1
    based on suggestion provided by Jon skeet and jbNizet. – Jabongg Sep 19 '17 at 11:46
  • 1
    using java.time api... String epochDate = jsonDate.replaceFirst("^.*Date\\((\\d+)\\).*$", "$1"); LocalDate epochFromLocalDate = Instant.ofEpochMilli(Long.parseLong(epochDate)).atZone(ZoneId.systemDefault()).toLocalDate(); System.out.println(epochFromLocalDate); – Jabongg Sep 21 '17 at 04:32