2

I cant quite figure out what the format should be to parse this date. Its a millisecond value followed by timezone. thx.

// so far tried:   "S Z"
//                "SSSSSSSSSSS-ZZZZ",
//                "SSSSSSSSSSS-Z",
// etc.

Format formatter = new SimpleDateFormat("SSSSSSSSSSSS Z", Locale.CANADA);

// source string looks like this /Date(928164000000-0400)/
String temp = jsonUserObj.getString("DateOfBirth").substring(6, 6+17);
System.err.println("got date="+temp);
Date date = (Date) formatter.parseObject(temp);
Saideira
  • 2,374
  • 6
  • 38
  • 49

2 Answers2

1

You can do it without parser.

String[] parts = new String[]{temp.substring(0, temp.indexOf('-')), temp.substring(temp.indexOf('-') + 1)};
long millis = Long.parseLong(parts[0]);
parts[1] = parts[1].replaceAll("^0*(\\-?[0-9]*)$", "$1");
int timeZone = Integer.parseInt(parts[1]);

int rawOffset = (timeZone / 100) * 3600000 + (timeZone % 100);

GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(millis);
cal.setTimeZone(new SimpleTimeZone(rawOffset, "GMT"));
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
0

SimpleDateFormat expects a milliseconds value < 1000, as it expects you would increment seconds, then minutes, etc, for larger values.

You'll need to convert the value first; this post might help: Unix epoch time to Java Date object

Community
  • 1
  • 1
Rodney Gitzel
  • 2,652
  • 16
  • 23