0

I am receiving an XML, in which some fields have date values, but they are 'encrypted', and I do not know how to convert.

examples:

Input:

<effectiveEndDate>/Date(253402214400000)/</effectiveEndDate>
<code>002956</code>
<lastModifiedDateTime>/Date(1574082723000+0000)/</lastModifiedDateTime>

Output Expected:

<effectiveEndDate>9999-12-31T00:00:00.000GMT</effectiveEndDate>
<code>002956</code>
<lastModifiedDateTime>2019-11-12T17:18:39.000GMT</lastModifiedDateTime>

Note that its an example, of how I want the date field, it's not the real converted values.

Can anyone illuminate the way?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
AJ Siegel
  • 74
  • 6
  • You need to know the value of the input to properly convert it into a desired format. If it's encrypted, you need to decrypt it first. – ahoxha Nov 18 '19 at 19:00
  • 1
    That's apparently a SAP JSON DateTime. See https://developer.jboss.org/message/985070 – Robert Harvey Nov 18 '19 at 19:04
  • I used the word 'encrypted', but it's not really encrypted. It's like in excel, that internally they have a value 43780 but they convert to date like 2019-11-11. But I do not know how to do this on Java. The excel example is just a comparison, the XML there nothing to do with excel. – AJ Siegel Nov 18 '19 at 19:06
  • 1
    For your output, it resembles [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601), so I recommend you take the full step. It just means using `Z` (for UTC or “Zulu time zone”) instead of `GMT`. – Ole V.V. Nov 19 '19 at 05:56

3 Answers3

2

Thanks to Robert Harvey, I was able to found what exactly is this date... I found this SAP Blog, where he explains that this field has

“/Date()/” = number of milliseconds since midnight Jan 1, 1970 Blockquote

So, I found this tool, where I was able to see how it works.

And finally my solution is:

System.out.println(Instant.ofEpochMilli(Long.parseLong("253402214400000")));

That I was able to find because of this!.

So Thanks!! And I hope this finds someone that was totally lost as I was :D

AJ Siegel
  • 74
  • 6
1

You can parse the date values as follows:

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

    public class Main {
        public static void main(String[] args) {
            ZonedDateTime utc = Instant.ofEpochMilli(253402214400000L).atZone(ZoneOffset.UTC);
            System.out.println( DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSO").format(utc));
        }
    }

Output:

9999-12-31T00:00:00.000GMT
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Doesn't the `O` pattern print the offset? Or is it omitted in this case since it's zero? – erickson Nov 18 '19 at 20:10
  • @erickson - Yes, `O` prints [localized zone-offset](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html) as required in the question. Yes, it's omitted in this case since it's zero (**Note for other readers**: `UTC` has an offset of `+00:00` hours). If a `ZoneId`, which has a non-zero offset, is used, the offset will also be printed e.g. for `.atZone(ZoneId.of("Asia/Calcutta"))`, it will print `9999-12-31T05:30:00.000GMT+5:30`. – Arvind Kumar Avinash Dec 26 '20 at 00:13
  • I don’t see a requirement for the offset in the question, and the accepted answer uses the ISO “instant” format in UTC. – erickson Dec 26 '20 at 20:17
  • 1
    @erickson - The requirement states: `Output Expected: 9999-12-31T00:00:00.000GMT` and therefore IMHO, this solution meets the requirement unless I am missing something. I am not sure which answer you are referring to as the accepted answer - is it the one which is posted by the OP himself? – Arvind Kumar Avinash Dec 26 '20 at 20:27
  • Yes, this answer does produce the desired output. I meant the OP’s answer. – erickson Dec 26 '20 at 21:18
0

You can also do

ZonedDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(sapDate.replaceAll("\\D+",""))),
  ZoneId.systemDefault());