@Test public void convertLocalToDTSafely() { DateTimeZone.setDefault(DateTimeZone.forID("America/Los_Angeles")); Assert.assertEquals(LocalDateTime.parse("2018-03-11T03:04:00").toDateTime(DateTimeZone.getDefault()), Utils.convertLocalToDTSafely(LocalDateTime.parse("2018-03-11T02:04:00"))); Assert.assertEquals(LocalDateTime.parse("2018-03-11T03:04:00").toDateTime(DateTimeZone.getDefault()), Utils.convertLocalToDTSafely(LocalDateTime.parse("2018-03-11T02:04:00"))); Assert.assertEquals(LocalDateTime.parse("2018-03-11T03:04:00").toDateTime(DateTimeZone.getDefault()), Utils.convertLocalToDTSafely(LocalDateTime.parse("2018-03-11T02:34:00"))); }
public static DateTime convertLocalToDTSafely(LocalDateTime localDateTime) { DateTime convertedDT = null; try { convertedDT = localDateTime.toDateTime(); } catch (IllegalInstantException e) { boolean failed = true; int iteration = 1; while (failed) { try { convertedDT = localDateTime.plusMinutes(30 * iteration).toDateTime(); failed = false; } catch (IllegalInstantException e2) { iteration++; } } Timber.i("Converted date from "+ localDateTime + " to " + convertedDT + " because of DST gap"); } return convertedDT; }
Utils { public static DateTime convertLocalToDTSafely(LocalDateTime localDateTime) { DateTime convertedDT = null; try { convertedDT = localDateTime.toDateTime(); } catch (IllegalInstantException e) { boolean failed = true; int iteration = 1; while (failed) { try { convertedDT = localDateTime.plusMinutes(30 * iteration).toDateTime(); failed = false; } catch (IllegalInstantException e2) { iteration++; } } Timber.i("Converted date from "+ localDateTime + " to " + convertedDT + " because of DST gap"); } return convertedDT; } }
Utils { public static DateTime convertLocalToDTSafely(LocalDateTime localDateTime) { DateTime convertedDT = null; try { convertedDT = localDateTime.toDateTime(); } catch (IllegalInstantException e) { boolean failed = true; int iteration = 1; while (failed) { try { convertedDT = localDateTime.plusMinutes(30 * iteration).toDateTime(); failed = false; } catch (IllegalInstantException e2) { iteration++; } } Timber.i("Converted date from "+ localDateTime + " to " + convertedDT + " because of DST gap"); } return convertedDT; }  }
Utils { public static DateTime convertLocalToDTSafely(LocalDateTime localDateTime) { DateTime convertedDT = null; try { convertedDT = localDateTime.toDateTime(); } catch (IllegalInstantException e) { boolean failed = true; int iteration = 1; while (failed) { try { convertedDT = localDateTime.plusMinutes(30 * iteration).toDateTime(); failed = false; } catch (IllegalInstantException e2) { iteration++; } } Timber.i("Converted date from "+ localDateTime + " to " + convertedDT + " because of DST gap"); } return convertedDT; }  static LocalDateTime getNow(); static int getAppSmallIcon(); static DatePickerDialog getDatePickerDialog(Context context, DatePickerDialog.OnDateSetListener onDateSetListener, int year, int month, int day); static TimePickerDialog getTimePickerDialog(Context context,
                                                       TimePickerDialog.OnTimeSetListener onTimeSetListener,
                                                       int hour, int minute, boolean is24HourFormat); static void toastTo(final String toastMessage); static PendingIntent getPIInNewStack(Intent intent, int requestCode); static PendingIntent getPIInNewStack(Intent intent, int requestCode, int flags); static int calculateDaysUntil(LocalDateTime until); static String getFormattedMessageForDate(LocalDateTime localDateTime,
                                                    @StringRes int stringRed); static int getSharedUniqueId(); static LocalDateTime saneNowPlusDurationForAlarms(Duration duration); static DateTime convertLocalToDTSafely(LocalDateTime localDateTime); }
Utils { public static DateTime convertLocalToDTSafely(LocalDateTime localDateTime) { DateTime convertedDT = null; try { convertedDT = localDateTime.toDateTime(); } catch (IllegalInstantException e) { boolean failed = true; int iteration = 1; while (failed) { try { convertedDT = localDateTime.plusMinutes(30 * iteration).toDateTime(); failed = false; } catch (IllegalInstantException e2) { iteration++; } } Timber.i("Converted date from "+ localDateTime + " to " + convertedDT + " because of DST gap"); } return convertedDT; }  static LocalDateTime getNow(); static int getAppSmallIcon(); static DatePickerDialog getDatePickerDialog(Context context, DatePickerDialog.OnDateSetListener onDateSetListener, int year, int month, int day); static TimePickerDialog getTimePickerDialog(Context context,
                                                       TimePickerDialog.OnTimeSetListener onTimeSetListener,
                                                       int hour, int minute, boolean is24HourFormat); static void toastTo(final String toastMessage); static PendingIntent getPIInNewStack(Intent intent, int requestCode); static PendingIntent getPIInNewStack(Intent intent, int requestCode, int flags); static int calculateDaysUntil(LocalDateTime until); static String getFormattedMessageForDate(LocalDateTime localDateTime,
                                                    @StringRes int stringRed); static int getSharedUniqueId(); static LocalDateTime saneNowPlusDurationForAlarms(Duration duration); static DateTime convertLocalToDTSafely(LocalDateTime localDateTime);  }