Example usage for org.joda.time DateMidnight withMonthOfYear

List of usage examples for org.joda.time DateMidnight withMonthOfYear

Introduction

In this page you can find the example usage for org.joda.time DateMidnight withMonthOfYear.

Prototype

public DateMidnight withMonthOfYear(int monthOfYear) 

Source Link

Document

Returns a copy of this date with the month of year field updated.

Usage

From source file:com.latlab.common.model.PeriodUtils.java

/**
 * Obtains a Map of {@link YearQuarterDate} for all the quarters of the
 * specified year. If <code>year</code> value is 0 or less, it is assumed
 * that the year is the current year.//  w w  w . jav a2  s  .  c  om
 *
 * @param year
 * @return
 */
public static Map<Period, DateRange> getQuarterDates(int year) {
    Map<Period, DateRange> quarterMap = new HashMap<>();

    DateMidnight refDate = new DateMidnight();
    if (year > 0) {
        refDate = refDate.withYear(year);
    }

    refDate = refDate.withMonthOfYear(1).withDayOfMonth(1);
    Date startDate1 = refDate.toDate();
    refDate = refDate.plusMonths(2);
    refDate = refDate.withDayOfMonth(refDate.dayOfMonth().getMaximumValue());
    Date endDate1 = new DateTime(refDate.toDateTime()).withHourOfDay(23).withMinuteOfHour(59)
            .withSecondOfMinute(59).toDate();

    DateRange quarterDate = new DateRange(Period.FIRST_QUARTER, year, startDate1, endDate1);
    quarterMap.put(quarterDate.getPeriod(), quarterDate);

    refDate = refDate.withMonthOfYear(4).withDayOfMonth(1);
    Date starteDate2 = refDate.toDate();
    refDate = refDate.plusMonths(2);
    refDate = refDate.withDayOfMonth(refDate.dayOfMonth().getMaximumValue());
    Date endDate2 = new DateTime(refDate.toDateTime()).withHourOfDay(23).withMinuteOfHour(59)
            .withSecondOfMinute(59).toDate();
    DateRange quarterDate2 = new DateRange(Period.SECOND_QUARTER, year, starteDate2, endDate2);
    quarterMap.put(quarterDate2.getPeriod(), quarterDate2);

    refDate = refDate.withMonthOfYear(7).withDayOfMonth(1);
    Date starteDate3 = refDate.toDate();
    refDate = refDate.plusMonths(2);
    refDate = refDate.withDayOfMonth(refDate.dayOfMonth().getMaximumValue());
    Date endDate3 = new DateTime(refDate.toDateTime()).withHourOfDay(23).withMinuteOfHour(59)
            .withSecondOfMinute(59).toDate();
    DateRange quarterDate3 = new DateRange(Period.THIRD_QUARTER, year, starteDate3, endDate3);
    quarterMap.put(quarterDate3.getPeriod(), quarterDate3);

    refDate = refDate.withMonthOfYear(10).withDayOfMonth(1);
    Date starteDate4 = refDate.toDate();
    refDate = refDate.plusMonths(2);
    refDate = refDate.withDayOfMonth(refDate.dayOfMonth().getMaximumValue());
    Date endDate4 = new DateTime(refDate.toDateTime()).withHourOfDay(23).withMinuteOfHour(59)
            .withSecondOfMinute(59).toDate();
    DateRange quarterDate4 = new DateRange(Period.LAST_QUARTER, refDate.getYear(), starteDate4, endDate4);
    quarterMap.put(quarterDate4.getPeriod(), quarterDate4);
    return quarterMap;
}

From source file:net.sourceforge.atunes.kernel.modules.tags.JAudiotaggerTagCreator.java

License:Open Source License

/**
 * @param iTag/*from   ww w . ja va2s. c o  m*/
 * @param tag
 */
private void setDateFromID3v23Tag(final ITag iTag, final org.jaudiotagger.tag.Tag tag) {
    // Set date from fields tag TYER and date/month tag TDAT
    DateMidnight c = null;
    String yearPart = getFirstTagValue(tag, "TYER");
    if (!StringUtils.isEmpty(yearPart)) {
        try {
            c = new DateMidnight().withYear(Integer.parseInt(yearPart)).withMonthOfYear(1).withDayOfMonth(1);
            String dateMonthPart = getFirstTagValue(tag, "TDAT");
            if (!StringUtils.isEmpty(dateMonthPart) && dateMonthPart.length() >= 4) {
                c = c.withMonthOfYear(Integer.parseInt(dateMonthPart.substring(2, 4)))
                        .withDayOfMonth(Integer.parseInt(dateMonthPart.substring(0, 2)));
            }
        } catch (NumberFormatException e) {
            // Skip this date
        } catch (IllegalArgumentException e) {
            // Skip this date
        }
    }

    if (c != null) {
        iTag.setDate(c);
    }
}