/** * Returns an instance of a DateUnit. * * @param date date of calendar in local calendar * @return an instance of a Calendar without any time of day. */ public static DateTimeUnit createLocalDateUnitInstance( Date date, org.hisp.dhis.calendar.Calendar calendar ) { return calendar.fromIso( date ); }
/** * Sets the current time. * * @param date the date to base time on. */ public Cal set( Date date ) { dateTimeUnit = getCalendar().fromIso( DateTimeUnit.fromJdkDate( date ) ); return this; }
/** * Returns a list of calendar specific period identifiers for the given collection of * periods and calendar. * * @param periods the list of periods. * @param calendar the calendar to use for generation of iso periods. * @return a list of iso period identifiers. */ public static <T extends IdentifiableObject> List<String> getLocalPeriodIdentifiers( Collection<T> periods, Calendar calendar ) { List<String> localIdentifiers = new ArrayList<>(); for ( IdentifiableObject object : periods ) { Period period = (Period) object; DateTimeUnit dateTimeUnit = calendar.fromIso( period.getStartDate() ); localIdentifiers.add( period.getPeriodType().getIsoDate( dateTimeUnit ) ); } return localIdentifiers; }
public Period createPeriod( Calendar cal ) { org.hisp.dhis.calendar.Calendar calendar = getCalendar(); return createPeriod( calendar.fromIso( DateTimeUnit.fromJdkCalendar( cal ) ), calendar ); }
/** * Returns the years which the given period spans. * * @param period the period. * @return a set of years. */ private static Set<Integer> getYears( Period period ) { Set<Integer> years = new HashSet<>(); int startYear = PeriodType.getCalendar().fromIso( period.getStartDate() ).getYear(); int endYear = PeriodType.getCalendar().fromIso( period.getEndDate() ).getYear(); while ( startYear <= endYear ) { years.add( startYear ); startYear++; } return years; }
@Override public Date getRewindedDate( Date date, Integer rewindedPeriods ) { Calendar cal = getCalendar(); date = date != null ? date : new Date(); rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1; DateTimeUnit dateTimeUnit = cal.fromIso( DateTimeUnit.fromJdkDate( date ) ); dateTimeUnit = cal.minusMonths( dateTimeUnit, rewindedPeriods ); return cal.toIso( dateTimeUnit ).toJdkDate(); }
@Override public Date getRewindedDate( Date date, Integer rewindedPeriods ) { Calendar cal = getCalendar(); date = date != null ? date : new Date(); rewindedPeriods = rewindedPeriods != null ? rewindedPeriods : 1; DateTimeUnit dateTimeUnit = cal.fromIso( DateTimeUnit.fromJdkDate( date ) ); dateTimeUnit = cal.minusMonths( dateTimeUnit, rewindedPeriods ); return cal.toIso( dateTimeUnit ).toJdkDate(); } }
final int year = PeriodType.getCalendar().fromIso( period.getStartDate() ).getYear();
@Override public String getIsoDate( DateTimeUnit dateTimeUnit, Calendar calendar ) { int month = dateTimeUnit.getMonth(); if ( dateTimeUnit.isIso8601() ) { month = calendar.fromIso( dateTimeUnit ).getMonth(); } switch ( month ) { case 1: return dateTimeUnit.getYear() + "S1"; case 7: return dateTimeUnit.getYear() + "S2"; default: throw new IllegalArgumentException( String.format( "Month not valid [1,7]: %d", month ) ); } }
/** * Creates a valid Period based on the given date. E.g. the given date is * February 10. 2007, a monthly PeriodType should return February 2007. This * method is intended for use in situations where a huge number of of periods * will be generated and its desirable to re-use the calendar. * * @param date the date which is contained by the created period. * @param calendar the calendar implementation to use. * @return the valid Period based on the given date */ public Period createPeriod( final Date date, final org.hisp.dhis.calendar.Calendar calendar ) { return PERIOD_CACHE.get( getCacheKey( calendar, date ), p -> createPeriod( calendar.fromIso( DateTimeUnit.fromJdkDate( date ) ), calendar ) ); }
@Override public Optional<List<Object[]>> getPopulateTempTableContent() { List<PeriodType> periodTypes = PeriodType.getAvailablePeriodTypes(); List<Object[]> batchArgs = new ArrayList<>(); Date startDate = new Cal( 1975, 1, 1, true ).time(); //TODO Date endDate = new Cal( 2025, 1, 1, true ).time(); List<Period> dailyPeriods = new DailyPeriodType().generatePeriods( startDate, endDate ); List<Date> days = new UniqueArrayList<>( dailyPeriods.stream().map( Period::getStartDate ).collect( Collectors.toList() ) ); Calendar calendar = PeriodType.getCalendar(); for ( Date day : days ) { List<Object> values = new ArrayList<>(); final int year = PeriodType.getCalendar().fromIso( day ).getYear(); values.add( day ); values.add( year ); for ( PeriodType periodType : periodTypes ) { values.add( periodType.createPeriod( day, calendar ).getIsoDate() ); } batchArgs.add( values.toArray() ); } return Optional.of( batchArgs ); }
@Override public String getIsoDate( DateTimeUnit dateTimeUnit, Calendar calendar ) { int month = dateTimeUnit.getMonth(); if ( dateTimeUnit.isIso8601() ) { month = calendar.fromIso( dateTimeUnit ).getMonth(); } switch ( month ) { case 11: return dateTimeUnit.getYear() + 1 + "NovS1"; case 5: return dateTimeUnit.getYear() + "NovS2"; default: throw new IllegalArgumentException( "Month not valid [11,5]" ); } }
@Override public String getIsoDate( DateTimeUnit dateTimeUnit, Calendar calendar ) { int month = dateTimeUnit.getMonth(); if ( dateTimeUnit.isIso8601() ) { month = calendar.fromIso( dateTimeUnit ).getMonth(); } switch ( month ) { case 4: return dateTimeUnit.getYear() + "AprilS1"; case 10: return dateTimeUnit.getYear() + "AprilS2"; default: throw new IllegalArgumentException( "Month not valid [4,10]" ); } }
/** * Returns a local period identifier for a specific period / calendar. * * @param period the list of periods. * @param calendar the calendar to use for generation of iso periods. * @return Period identifier based on given calendar */ public static String getLocalPeriodIdentifier( Period period, Calendar calendar ) { if ( calendar.isIso8601() ) { return period.getIsoDate(); } return period.getPeriodType().getIsoDate( calendar.fromIso( period.getStartDate() ) ); }
/** * Generates bi-weekly Periods for the whole year in which the given Period's * startDate exists. */ @Override public List<Period> generatePeriods( DateTimeUnit start ) { Calendar calendar = getCalendar(); List<Period> periods = new ArrayList<>(); DateTimeUnit date = adjustToStartOfBiWeek( start, calendar ); date = adjustToStartOfBiWeek( calendar.fromIso( date.getYear(), 1, 1 ), calendar ); for ( int i = 0; i < calendar.weeksInYear( start.getYear() ) / 2; i++ ) { periods.add( createPeriod( date, calendar ) ); date = calendar.plusWeeks( date, 2 ); } return periods; }
dateString = calendar.formattedDate( calendar.fromIso( date ) );
@Override public String getIsoDate( DateTimeUnit dateTimeUnit, Calendar calendar ) { DateTimeUnit newUnit = dateTimeUnit; if ( !calendar.name().equals( ISO_CALENDAR_NAME ) && newUnit.isIso8601() ) { newUnit = calendar.fromIso( newUnit ); } switch ( newUnit.getMonth() ) { case 1: return newUnit.getYear() + "Q1"; case 4: return newUnit.getYear() + "Q2"; case 7: return newUnit.getYear() + "Q3"; case 10: return newUnit.getYear() + "Q4"; default: throw new IllegalArgumentException( "Month not valid [1,4,7,10], was given " + dateTimeUnit.getMonth() ); } }
DateTimeUnit dateTimeUnit = calendar.fromIso( period.getStartDate() );
DateTimeUnit dateTimeUnit = calendar.fromIso( period.getStartDate() ); String isoDate = period.getPeriodType().getIsoDate( dateTimeUnit ); map.put( isoDate, new MetadataItem( period.getDisplayName(), includeMetadataDetails ? period : null ) );