use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class I18nFormat method formatPeriod.
/**
* Formats a period. Returns null if value is null. Returns INVALID_DATE if
* formatting string is invalid.
*
* @param period the value to format.
*/
public String formatPeriod(Period period) {
if (period == null) {
return null;
}
PeriodType periodType = period.getPeriodType();
String typeName = periodType.getName();
if (// Use ISO dates
periodType instanceof WeeklyAbstractPeriodType) // due to
// potential week
// confusion
{
DateTime dateTime = new DateTime(period.getStartDate());
LocalDate date = Instant.ofEpochMilli(period.getStartDate().getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
WeekFields weekFields = WeekFields.of(PeriodType.MAP_WEEK_TYPE.get(periodType.getName()), 4);
String year = String.valueOf(date.get(weekFields.weekBasedYear()));
String week = String.valueOf(date.get(weekFields.weekOfWeekBasedYear()));
if (periodType instanceof WeeklyPeriodType) {
return String.format("W%s %s", week, year);
}
year += dateTime.dayOfWeek().getAsShortText() + " " + year;
return String.format("W%s %s", week, year);
} else if (periodType instanceof BiWeeklyAbstractPeriodType) {
int year;
int week;
Calendar calendar = PeriodType.getCalendar();
BiWeeklyAbstractPeriodType biWeeklyAbstractPeriodType = (BiWeeklyAbstractPeriodType) periodType;
DateTimeUnit dateTimeUnit = DateTimeUnit.fromJdkDate(period.getStartDate());
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 = biWeeklyAbstractPeriodType.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("BiW%s %s", week, year);
}
String keyStartDate = "format." + typeName + ".startDate";
String keyEndDate = "format." + typeName + ".endDate";
String startPattern = resourceBundle.getString(keyStartDate);
String endPattern = resourceBundle.getString(keyEndDate);
boolean dayPattern = startPattern.contains("dd") || endPattern.contains("dd");
Date periodStartDate = period.getStartDate();
Date periodEndDate = period.getEndDate();
DateTimeUnit start = PeriodType.getCalendar().fromIso(periodStartDate);
DateTimeUnit end = PeriodType.getCalendar().fromIso(periodEndDate);
String startDate;
String endDate;
if (!dayPattern) {
// Set day to first of month to not overflow when converting to JDK
// date
start.setDay(1);
end.setDay(1);
startDate = commonFormatting(new DateTimeUnit(start, true).toJdkDate(), startPattern);
endDate = commonFormatting(new DateTimeUnit(end, true).toJdkDate(), endPattern);
} else {
startDate = PeriodType.getCalendar().formattedDate(startPattern, start);
endDate = PeriodType.getCalendar().formattedDate(endPattern, end);
}
try {
return Character.toUpperCase(startDate.charAt(0)) + startDate.substring(1) + endDate;
} catch (IllegalArgumentException ex) {
return INVALID_DATE;
}
}
use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class BiWeeklyAbstractPeriodType method getRewindedDate.
@Override
public Date getRewindedDate(Date date, Integer rewindedPeriods) {
Calendar cal = getCalendar();
Date rewindedDate = date != null ? date : new Date();
rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1;
DateTimeUnit dateTimeUnit = createLocalDateUnitInstance(rewindedDate);
dateTimeUnit = cal.minusWeeks(dateTimeUnit, rewindedPeriods * 2);
return cal.toIso(dateTimeUnit).toJdkDate();
}
use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class BiWeeklyAbstractPeriodType method generateRollingPeriods.
/**
* Generates the last 26 bi-weeks where the last one is the week which the
* given date is inside.
*/
@Override
public List<Period> generateRollingPeriods(DateTimeUnit end, Calendar calendar) {
List<Period> periods = Lists.newArrayList();
DateTimeUnit date = adjustToStartOfBiWeek(end, calendar);
date = calendar.minusDays(date, 350);
for (int i = 0; i < 26; i++) {
periods.add(createPeriod(date, calendar));
date = calendar.plusWeeks(date, 2);
}
return periods;
}
use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class DailyPeriodType method generateRollingPeriods.
/**
* Generates the last 365 days where the last one is the day of the given
* date.
*/
@Override
public List<Period> generateRollingPeriods(DateTimeUnit dateTimeUnit, Calendar calendar) {
Calendar cal = getCalendar();
DateTimeUnit iterationDateTimeUnit = cal.minusDays(dateTimeUnit, 364);
List<Period> periods = Lists.newArrayList();
for (int i = 0; i < 365; i++) {
periods.add(createPeriod(iterationDateTimeUnit, calendar));
iterationDateTimeUnit = cal.plusDays(iterationDateTimeUnit, 1);
}
return periods;
}
use of org.hisp.dhis.calendar.DateTimeUnit in project dhis2-core by dhis2.
the class MonthlyPeriodType method createPeriod.
@Override
public Period createPeriod(DateTimeUnit dateTimeUnit, Calendar calendar) {
DateTimeUnit start = new DateTimeUnit(dateTimeUnit);
start.setDay(1);
DateTimeUnit end = new DateTimeUnit(dateTimeUnit);
end.setDay(calendar.daysInMonth(end.getYear(), end.getMonth()));
return toIsoPeriod(start, end, calendar);
}
Aggregations