Example usage for org.joda.time MonthDay dayOfMonth

List of usage examples for org.joda.time MonthDay dayOfMonth

Introduction

In this page you can find the example usage for org.joda.time MonthDay dayOfMonth.

Prototype

public Property dayOfMonth() 

Source Link

Document

Get the day of month field property which provides access to advanced functionality.

Usage

From source file:br.com.fidias.chance4j.Chance.java

License:Open Source License

/**
 * Generate a random datetime, limited to a year.
 *
 * @param year Year of the date//from   w  ww  . j  a v  a  2s. c om
 * @param hour Hour of the date
 * @param minute Minute of the date
 * @return A random date with time
 * @throws ChanceException
 */
private DateTime dateTime(int year, int hour, int minute, int second) throws ChanceException {
    int month = month();
    // https://github.com/JodaOrg/joda-time/issues/22
    int maximumValue;
    if (month == DateTimeConstants.FEBRUARY) {
        maximumValue = 28;
    } else {
        MonthDay monthDay = new MonthDay(month, 1);
        maximumValue = monthDay.dayOfMonth().getMaximumValue();
    }

    int day = natural(1, maximumValue);
    return new DateTime(year, month, day, hour, minute, second);
}

From source file:fr.nicopico.dashclock.birthday.BirthdayService.java

License:Apache License

@Override
protected void onUpdateData(int reason) {
    if (reason == UPDATE_REASON_SETTINGS_CHANGED) {
        updatePreferences();/*from  www.  ja  v a 2 s.c o m*/
    }

    final Resources res = getResources();
    final List<Birthday> birthdays = birthdayRetriever.getContactWithBirthdays(getApplicationContext(),
            contactGroupId);

    Configuration config = new Configuration();
    config.setToDefaults();

    // Disable/enable Android localization
    if (needToRefreshLocalization
            || (disableLocalization && !DEFAULT_LANG.equals(Locale.getDefault().getLanguage()))) {
        if (disableLocalization) {
            config.locale = new Locale(DEFAULT_LANG);
        } else {
            // Restore Android localization
            //noinspection ConstantConditions
            config.locale = Resources.getSystem().getConfiguration().locale;
        }

        Locale.setDefault(config.locale);
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
    }

    DateTime today = new DateTime();

    int upcomingBirthdays = 0;
    String collapsedTitle = null;
    String expandedTitle = null;
    StringBuilder body = new StringBuilder();

    for (Birthday birthday : birthdays) {
        DateTime birthdayEvent;
        MonthDay birthdayDate = birthday.birthdayDate;
        try {
            birthdayEvent = birthdayDate.toDateTime(today);
        } catch (IllegalFieldValueException e) {
            if (birthdayDate.getDayOfMonth() == 29 && birthdayDate.getMonthOfYear() == 2) {
                // Birthday on February 29th (leap year) -> March 1st
                birthdayEvent = birthdayDate.dayOfMonth().addToCopy(1).toDateTime(today);
            } else {
                Log.e(TAG, "Invalid date", e);
                continue;
            }
        }

        // How many days before the birthday ?
        int days;
        if (birthdayEvent.isAfter(today) || birthdayEvent.isEqual(today)) {
            days = Days.daysBetween(today, birthdayEvent).getDays();
        } else {
            // Next birthday event is next year
            days = Days.daysBetween(today, birthdayEvent.plusYears(1)).getDays();
        }

        // Should the birthday be displayed ?
        if (days <= daysLimit) {
            upcomingBirthdays++;

            if (upcomingBirthdays == 1) {
                // A single birthday will be displayed
                collapsedTitle = birthday.displayName;
                expandedTitle = res.getString(R.string.single_birthday_title_format, birthday.displayName);
            }

            // More than 1 upcoming birthday: display contact name
            if (upcomingBirthdays > 1) {
                body.append("\n").append(birthday.displayName).append(", ");
            }

            // Age
            if (!birthday.unknownYear) {
                int age = today.get(DateTimeFieldType.year()) - birthday.year;
                body.append(res.getQuantityString(R.plurals.age_format, age, age));
                body.append(' ');
            }

            // When
            int daysFormatResId;
            switch (days) {
            case 0:
                daysFormatResId = R.string.when_today_format;
                break;
            case 1:
                daysFormatResId = R.string.when_tomorrow_format;
                break;
            default:
                daysFormatResId = R.string.when_days_format;
            }

            body.append(res.getString(daysFormatResId, days));
        } else {
            // All visible birthdays have been processed
            break;
        }
    }

    if (upcomingBirthdays > 0) {
        Intent clickIntent = buildClickIntent(birthdays.subList(0, upcomingBirthdays));

        if (upcomingBirthdays > 1) {
            collapsedTitle += " + " + (upcomingBirthdays - 1);
        }

        // Display message
        publishUpdate(
                new ExtensionData().visible(true).icon(R.drawable.ic_extension_white).status(collapsedTitle)
                        .expandedTitle(expandedTitle).expandedBody(body.toString()).clickIntent(clickIntent));
    } else {
        // Nothing to show
        publishUpdate(new ExtensionData().visible(false));
    }
}