-1

How do I parse this date format:

"/Date(1514728800000+0300)/"

I don't know what the meaning of this number is, or that of the + sign.

Ibrahim Disouki
  • 2,642
  • 4
  • 21
  • 52
  • 2
    where you get these values? – Nouman Ch Oct 18 '17 at 13:18
  • From a web service which I don't have access to and can not change – Ibrahim Disouki Oct 18 '17 at 13:24
  • no idea about +0300 may be timezone or something. – Nouman Ch Oct 18 '17 at 13:28
  • 1
    The first part (1514728800000) is a [unix timestamp](https://www.unixtimestamp.com) - the number of milliseconds since unix epoch (`1970-01-01T00:00Z`). The second part (`+0300`) is an [UTC offset](https://en.wikipedia.org/wiki/UTC_offset), it represents the difference from UTC (in this case, 3 hours ahead UTC). But what should be the result of parsing? A `java.util.Date`? Any other object? –  Oct 18 '17 at 13:29
  • 1
    @Hugo It seems weird to me that one would add a UTC offset to a Unix timestamp. I thought one of the purposes of Unix timestamps was to avoid the hassle with time zones. (Not saying you're wrong, in fact you're probably right. It's just seems a weird decision by the web service in question). – S.L. Barth is on codidact.com Oct 18 '17 at 13:33
  • 1
    @S.L.Barth I also think it's strange, but my guess is that you can convert the timestamp to the specified offset, so you'll get a specific date and time (as the same timestamp corresponds to a different date and time in each timezone). But I still think it's not the smartest format... –  Oct 18 '17 at 13:35

3 Answers3

1

Partial answer: the number is the epoch time.

This is the amount of seconds since Jan 1, 1970, UTC.
You can pass this value to the constructor of java.util.Date, which will get you a Date object with the right value.

The +0300 is unclear, perhaps a reference to a different timezone.

1

Im not sure about the +0300, but you can convert a epoch time to Date with the following function:

 Date date = new Date(Long.parseLong(myDateToParse.replaceAll("[^\\d-]", "")));
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.DEFAULT);
  • 1
    This will create a `long` value equals to `15147288000000300`, but the last digits (`0300`) are not part of the millis value and it shouldn't be passed to `Date` constructor (your code creates a date in year **481968**!) - you should pass just `1514728800000`. And `Locale.DEFAULT` doesn't exist, perhaps you meant `Locale.getDefault()`? –  Oct 18 '17 at 14:47
  • @Hugo True, i meant Locale.getDefault(), thanks for the input. As for the +0300, i also have to say taht you are right. What i tried to do in my example, was to parse a simple epoch time (without the +0300). You could just simplly remove the +0300 and add the 3 Hours (with the required logic) to the new Date – Andreas Imsand Oct 18 '17 at 15:02
  • Well, you can always [edit] your answers to fix/improve them, if you want. –  Oct 18 '17 at 15:12
1

Here is the code that parses this assuming you have unixtimestamp_zoneoffset

String inputStr = "1514728800000+0300";
String[] splitStr = inputStr.split("[+-]");
String offsetSign = inputStr.indexOf("+")>0 ? "+" : "-";
ZonedDateTime captureTime = Instant.ofEpochMilli(Long.valueOf(splitStr[0])).atZone(ZoneOffset.of(offsetSign+splitStr[1]));

The value returned is 2017-12-31T17:00+03:00

Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43