Search in sources :

Example 71 with WeekFields

use of java.time.temporal.WeekFields in project dhis2-core by dhis2.

the class BiWeeklyAbstractPeriodType method getIsoDate.

@Override
public String getIsoDate(DateTimeUnit dateTimeUnit, Calendar calendar) {
    int year;
    int week;
    if (calendar.isIso8601()) {
        LocalDate date = LocalDate.of(dateTimeUnit.getYear(), dateTimeUnit.getMonth(), dateTimeUnit.getDay());
        WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY, 4);
        year = date.get(weekFields.weekBasedYear());
        week = (date.get(weekFields.weekOfWeekBasedYear()) / 2) + 1;
    } else {
        DateTimeUnit date = adjustToStartOfBiWeek(dateTimeUnit, calendar);
        week = calendar.week(date);
        if (week == 1 && date.getMonth() == calendar.monthsInYear()) {
            date.setYear(date.getYear() + 1);
        }
        year = date.getYear();
    }
    return String.format("%d%s%d", year, weekPrefix, week);
}
Also used : DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate)

Example 72 with WeekFields

use of java.time.temporal.WeekFields in project dhis2-core by dhis2.

the class DateUnitPeriodTypeParser method getDateTimeFromWeek.

/**
 * returns a date based on a week number
 *
 * @param year The year of the date
 * @param week The week of the date
 * @param calendar The calendar used to calculate the daate
 * @param firstDayOfWeek The first day of the week
 * @param adjustedDate The first day of the year adjusted to the first day
 *        of the week it belongs to
 *
 * @return The Date of the week
 */
private DateTimeUnit getDateTimeFromWeek(int year, int week, Calendar calendar, DayOfWeek firstDayOfWeek, DateTimeUnit adjustedDate) {
    if (calendar.isIso8601()) {
        WeekFields weekFields = WeekFields.of(firstDayOfWeek, 4);
        LocalDate date = LocalDate.now().with(weekFields.weekBasedYear(), year).with(weekFields.weekOfWeekBasedYear(), week).with(weekFields.dayOfWeek(), 1);
        return new DateTimeUnit(date.getYear(), date.getMonthValue(), date.getDayOfMonth(), calendar.isIso8601());
    } else {
        DateTimeUnit date = new DateTimeUnit(year, adjustedDate.getMonth(), adjustedDate.getDay(), calendar.isIso8601());
        // years weeks, so we check and forward if needed
        if (calendar.isoWeek(date) == calendar.weeksInYear(year)) {
            date = calendar.plusWeeks(date, 1);
        }
        date = calendar.plusWeeks(date, week - 1);
        return date;
    }
}
Also used : WeekFields(java.time.temporal.WeekFields) LocalDate(java.time.LocalDate)

Example 73 with WeekFields

use of java.time.temporal.WeekFields in project java-swing-tips by aterai.

the class ContributionIcon method makeWeekCalendar.

private static Component makeWeekCalendar(JList<Contribution> weekList, Font font) {
    Locale l = Locale.getDefault();
    WeekFields weekFields = WeekFields.of(l);
    DefaultListModel<String> weekModel = new DefaultListModel<>();
    DayOfWeek firstDayOfWeek = weekFields.getFirstDayOfWeek();
    for (int i = 0; i < DayOfWeek.values().length; i++) {
        boolean isEven = i % 2 == 0;
        if (isEven) {
            weekModel.add(i, "");
        } else {
            weekModel.add(i, firstDayOfWeek.plus(i).getDisplayName(TextStyle.SHORT_STANDALONE, l));
        }
    }
    JList<String> rowHeader = new JList<>(weekModel);
    rowHeader.setEnabled(false);
    rowHeader.setFont(font);
    rowHeader.setLayoutOrientation(JList.VERTICAL_WRAP);
    rowHeader.setVisibleRowCount(DayOfWeek.values().length);
    rowHeader.setFixedCellHeight(CELL_SIZE.height);
    JPanel colHeader = new JPanel(new GridBagLayout());
    colHeader.setBackground(Color.WHITE);
    GridBagConstraints c = new GridBagConstraints();
    for (c.gridx = 0; c.gridx < CalendarViewListModel.WEEK_VIEW; c.gridx++) {
        // grid guides
        colHeader.add(Box.createHorizontalStrut(CELL_SIZE.width), c);
    }
    c.anchor = GridBagConstraints.LINE_START;
    c.gridy = 1;
    // use 3 columns to display the name of the month
    c.gridwidth = 3;
    for (c.gridx = 0; c.gridx < CalendarViewListModel.WEEK_VIEW - c.gridwidth + 1; c.gridx++) {
        LocalDate date = weekList.getModel().getElementAt(c.gridx * DayOfWeek.values().length).date;
        // int weekNumberOfMonth = date.get(weekFields.weekOfMonth());
        // System.out.println(weekNumberOfMonth);
        // ignore WeekFields#getMinimalDaysInFirstWeek()
        boolean isFirst = date.getMonth() != date.minusWeeks(1L).getMonth();
        if (isFirst) {
            colHeader.add(makeLabel(date.getMonth().getDisplayName(TextStyle.SHORT, l), font), c);
        }
    }
    JScrollPane scroll = new JScrollPane(weekList);
    scroll.setBorder(BorderFactory.createEmptyBorder());
    scroll.setColumnHeaderView(colHeader);
    scroll.setRowHeaderView(rowHeader);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setBackground(Color.WHITE);
    return scroll;
}
Also used : Locale(java.util.Locale) DayOfWeek(java.time.DayOfWeek) LocalDate(java.time.LocalDate) WeekFields(java.time.temporal.WeekFields)

Aggregations

WeekFields (java.time.temporal.WeekFields)73 AbstractTCKTest (tck.java.time.AbstractTCKTest)58 LocalDate (java.time.LocalDate)57 TemporalField (java.time.temporal.TemporalField)51 DateTimeFormatter (java.time.format.DateTimeFormatter)36 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)34 Test (org.junit.Test)33 Test (org.testng.annotations.Test)33 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)31 DayOfWeek (java.time.DayOfWeek)7 DateTimeException (java.time.DateTimeException)6 ValueRange (java.time.temporal.ValueRange)6 ParsePosition (java.text.ParsePosition)4 TemporalAccessor (java.time.temporal.TemporalAccessor)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 GregorianCalendar (java.util.GregorianCalendar)2