|
29 | 29 |
|
30 | 30 | import com.aspose.tasks.cloud.model.*; |
31 | 31 | import com.aspose.tasks.cloud.model.responses.*; |
| 32 | +import org.threeten.bp.LocalDateTime; |
| 33 | +import org.threeten.bp.format.DateTimeParseException; |
32 | 34 | import com.google.gson.*; |
33 | 35 | import com.google.gson.internal.bind.util.ISO8601Utils; |
34 | 36 | import com.google.gson.stream.JsonReader; |
@@ -312,33 +314,80 @@ public OffsetDateTime read(JsonReader in) throws IOException { |
312 | 314 | return null; |
313 | 315 | default: |
314 | 316 | String date = in.nextString(); |
315 | | - if (date.endsWith("Z")) { |
316 | | - date = date.substring(0, date.length() - 1); |
317 | | - } |
318 | | - if (date.indexOf("+") >= 0) { |
319 | | - date = date.substring(0, date.indexOf("+")); |
320 | | - } |
321 | | - Integer index = date.indexOf("T"); |
322 | | - String dateString = date.substring(0, index); |
323 | | - String[] timeString = date.substring(index + 1).split(":"); |
324 | | - String[] val = dateString.split("-"); |
325 | | - String second = timeString[2]; |
326 | | - Integer nanoOfSecond = 0; |
327 | | - if(second.contains(".")) { |
328 | | - String[] separatedValues = second.split("\\."); |
329 | | - second = separatedValues[0]; |
330 | | - nanoOfSecond = Integer.valueOf(separatedValues[1]); |
| 317 | + try { |
| 318 | + return OffsetDateTime.parse(date, formatter); |
| 319 | + } catch (DateTimeParseException e1) { |
| 320 | + try { |
| 321 | + return OffsetDateTime.parse(date); |
| 322 | + } catch (DateTimeParseException e2) { |
| 323 | + try { |
| 324 | + LocalDateTime ldt = LocalDateTime.parse(date, DateTimeFormatter.ISO_LOCAL_DATE_TIME); |
| 325 | + return OffsetDateTime.of(ldt, ZoneOffset.UTC); |
| 326 | + } catch (DateTimeParseException e3) { |
| 327 | + String normalized = normalizeOffsetDateTimeString(date); |
| 328 | + try { |
| 329 | + return OffsetDateTime.parse(normalized, formatter); |
| 330 | + } catch (DateTimeParseException e4) { |
| 331 | + try { |
| 332 | + return OffsetDateTime.parse(normalized); |
| 333 | + } catch (DateTimeParseException e5) { |
| 334 | + LocalDateTime ldt2 = LocalDateTime.parse(normalized, DateTimeFormatter.ISO_LOCAL_DATE_TIME); |
| 335 | + return OffsetDateTime.of(ldt2, ZoneOffset.UTC); |
| 336 | + } |
| 337 | + } |
| 338 | + } |
| 339 | + } |
331 | 340 | } |
332 | | - return OffsetDateTime.of(Integer.valueOf(val[0]), |
333 | | - Integer.valueOf(val[1]), |
334 | | - Integer.valueOf(val[2]), |
335 | | - Integer.valueOf(timeString[0]), |
336 | | - Integer.valueOf(timeString[1]), |
337 | | - Integer.valueOf(second), |
338 | | - nanoOfSecond, |
339 | | - ZoneOffset.UTC); |
340 | 341 | } |
341 | 342 | } |
| 343 | + |
| 344 | + private static String normalizeOffsetDateTimeString(String input) { |
| 345 | + int tIndex = input.indexOf('T'); |
| 346 | + if (tIndex < 0) return input; |
| 347 | + |
| 348 | + int plus = input.indexOf('+', tIndex); |
| 349 | + int minus = input.indexOf('-', tIndex); |
| 350 | + int tzPos; |
| 351 | + if (plus == -1 && minus == -1) { |
| 352 | + tzPos = -1; |
| 353 | + } else if (plus == -1) { |
| 354 | + tzPos = minus; |
| 355 | + } else if (minus == -1) { |
| 356 | + tzPos = plus; |
| 357 | + } else { |
| 358 | + tzPos = Math.min(plus, minus); |
| 359 | + } |
| 360 | + |
| 361 | + String main; |
| 362 | + String offset = ""; |
| 363 | + if (tzPos >= 0) { |
| 364 | + main = input.substring(0, tzPos); |
| 365 | + offset = input.substring(tzPos); |
| 366 | + } else { |
| 367 | + main = input; |
| 368 | + } |
| 369 | + |
| 370 | + if (main.contains(".")) { |
| 371 | + int dot = main.indexOf('.'); |
| 372 | + String before = main.substring(0, dot); |
| 373 | + String fraction = main.substring(dot + 1); |
| 374 | + fraction = fraction.replaceAll("[^0-9]", ""); |
| 375 | + if (fraction.length() > 9) fraction = fraction.substring(0, 9); |
| 376 | + while (fraction.length() < 9) fraction = fraction + "0"; |
| 377 | + main = before + "." + fraction; |
| 378 | + } |
| 379 | + |
| 380 | + if (!offset.isEmpty()) { |
| 381 | + if (offset.matches("^[+-]\\d{2}$")) { |
| 382 | + offset = offset + ":00"; |
| 383 | + } |
| 384 | + else if (offset.matches("^[+-]\\d{4}$")) { |
| 385 | + offset = offset.substring(0, 3) + ":" + offset.substring(3); |
| 386 | + } |
| 387 | + } |
| 388 | + |
| 389 | + return main + offset; |
| 390 | + } |
342 | 391 | } |
343 | 392 |
|
344 | 393 | /** |
|
0 commit comments