/** * Gets the anniversary date not yet passed (with delta of 1 day) * @param date Date * @return Next anniversary in the year */ public static Date getNextAnniversary(Date date) { DateTime dateTimeNow = DateTime.now(); MonthDay monthDayNow = MonthDay.now(); MonthDay monthDayOfNextDate = MonthDay.fromDateFields(date); if(monthDayNow.isEqual(monthDayOfNextDate)) { DateTime inputDate = new DateTime(date); return dateTimeNow .withHourOfDay(inputDate.getHourOfDay()) .withMinuteOfHour(inputDate.getMinuteOfHour()) .withSecondOfMinute(inputDate.getSecondOfMinute()) .withMillisOfSecond(inputDate.getMillisOfSecond()).toDate(); } if(monthDayNow.isBefore(monthDayOfNextDate)) return new DateTime(date).withYear(dateTimeNow.getYear()).toDate(); else { DateTime dateTimeOfNextDate = new DateTime(date).withYear(dateTimeNow.getYear()).plusYears(1); return dateTimeOfNextDate.toDate(); } }
public MonthDay extractMonthDayNamed(final String parameterName, final JsonElement element) { MonthDay value = null; if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); final String monthDayFormat = extractMonthDayFormatParameter(object); final Locale clientApplicationLocale = extractLocaleParameter(object); value = extractMonthDayNamed(parameterName, object, monthDayFormat, clientApplicationLocale); } return value; }
public MonthDay extractMonthDayNamed(final String parameterName, final JsonObject element, final String dateFormat, final Locale clientApplicationLocale) { MonthDay value = null; if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); final String valueAsString = primitive.getAsString(); if (StringUtils.isNotBlank(valueAsString)) { try { final DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat).withLocale(clientApplicationLocale); value = MonthDay.parse(valueAsString.toLowerCase(clientApplicationLocale), formatter); } catch (final IllegalArgumentException e) { final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); final ApiParameterError error = ApiParameterError.parameterError("validation.msg.invalid.month.day", "The parameter " + parameterName + " is invalid based on the monthDayFormat: '" + dateFormat + "' and locale: '" + clientApplicationLocale + "' provided:", parameterName, valueAsString, dateFormat); dataValidationErrors.add(error); throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", "Validation errors exist.", dataValidationErrors); } } } } return value; }
/** * Return number of days between today and the next date in a year * @param date date for calculate delta * @return Number of days always >= 0 */ public static int daysBetweenTodayAnd(Date date) { MonthDay monthDayNow = MonthDay.now(); MonthDay monthDayOfNextDate = MonthDay.fromDateFields(date); if(monthDayNow.isEqual(monthDayOfNextDate)) return 0; if(monthDayNow.isBefore(monthDayOfNextDate)) return Days.daysBetween(monthDayNow, monthDayOfNextDate).getDays(); else { DateTime dateTimeNow = DateTime.now(); DateTime dateTimeOfNextDate = new DateTime(date).withYear(dateTimeNow.getYear()).plusYears(1); return Days.daysBetween(DateTime.now(), dateTimeOfNextDate).getDays(); } }
private Birthday buildBirthday(ContentResolver contentResolver, Cursor c) { String birthDate = c.getString(1); if (birthDate == null) return null; // Analyze birthday string try { Matcher regexMatcher = regexDate.matcher(birthDate); if (regexMatcher.find()) { Birthday birthday = new Birthday(contentResolver, c.getLong(0)); // Birthday *must* have a display name if (birthday.displayName == null) return null; birthday.birthdayDate = new MonthDay( Integer.parseInt(regexMatcher.group(2)), Integer.parseInt(regexMatcher.group(3)) ); if (!"-".equals(regexMatcher.group(1))) { birthday.year = Integer.parseInt(regexMatcher.group(1)); birthday.unknownYear = false; } return birthday; } } catch (Exception e) { Log.e(TAG, "Error while analyzing birthday", e); return null; } return null; }
@Override public MonthDay read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return MonthDay.parse(in.nextString()); }
@Test public void creates_instance_of_MonthDay() { MonthDay monthDay = fixture.create(MonthDay.class); assertThat(monthDay, notNullValue()); assertThat(monthDay.getMonthOfYear(), is(1)); assertThat(monthDay.getDayOfMonth(), is(1)); }
@Override public MonthDay deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JsonProcessingException { String str = p.getText().trim(); if (str.isEmpty()) { return null; } return MonthDay.parse(str, formatter); }
public MonthDay extractMonthDayNamed(final String parameterName, final JsonElement element) { return this.helperDelegator.extractMonthDayNamed(parameterName, element); }
public MonthDay extractMonthDayNamed(final String parameterName, final JsonObject object, final String dateFormat, final Locale clientApplicationLocale) { return this.helperDelegator.extractMonthDayNamed(parameterName, object, dateFormat, clientApplicationLocale); }
@Override public MonthDay parse(String text, Locale locale) throws ParseException { return MonthDay.parse(text); }
@Override public String print(MonthDay object, Locale locale) { return object.toString(); }
public static void registerAdditionalFormatters(FormatterRegistry registry) { registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter()); registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter()); }
public MonthDay getMonthDay() { return monthDay; }
public void setMonthDay(MonthDay monthDay) { this.monthDay = monthDay; }
@Test public void testSerialization() { MonthDay monthDay = new MonthDay(3,30); String json = gson.toJson(monthDay); assertEquals("\"--03-30\"", json); }
@Test public void testDeserialization() { MonthDay monthDay = gson.fromJson("\"--03-30\"", MonthDay.class); assertEquals(3, monthDay.getMonthOfYear()); assertEquals(30, monthDay.getDayOfMonth()); }
public static boolean isAprilFools() { MonthDay now = MonthDay.now(); return (now.getDayOfMonth() == 1 || now.getDayOfMonth() == 2) && now.getMonthOfYear() == 4; }
@Override public MonthDay fromNonNullValue(String s) { return MonthDay.parse(s); }
@Override public String toNonNullValue(MonthDay value) { return value.toString(); }
public ScheduleItem(MonthDay from, MonthDay to, ScheduleBasic schedule) { this.from = from; this.to = to; this.schedule = schedule; }
public MonthDay getFrom() { return from; }
public MonthDay getTo() { return to; }
@Override public void serialize(MonthDay value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(formatter.print(value)); }