private static String toIsoDate( String periodName, Date start, final Calendar calendar ) { return PeriodType.getPeriodTypeByName( periodName ).createPeriod( start, calendar ).getIsoDate(); }
public static PeriodType getByNameIgnoreCase( String name ) { for ( PeriodType periodType : getAvailablePeriodTypes() ) { if ( name != null && periodType.getName().toLowerCase().trim().equals( name.toLowerCase().trim() ) ) { return periodType; } } return null; }
private String getCacheKey( Date date ) { return getCalendar().name() + getName() + date.getTime(); }
/** * Returns an iso8601 formatted string representation of the dataUnit * * @param dateTimeUnit Period * @return the period as string */ public String getIsoDate( DateTimeUnit dateTimeUnit ) { return getIsoDate( dateTimeUnit, getCalendar() ); }
/** * 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 ) { return createLocalDateUnitInstance( date, getCalendar() ); }
/** * Creates a valid Period based on the current date. E.g. if today is * January 5. 2007, a monthly PeriodType should return January 2007. * * @return a valid Period based on the current date. */ public Period createPeriod() { return createPeriod( createCalendarInstance() ); }
@Override public String toString() { return "[" + getName() + "]"; } }
@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 ); }
Calendar calendar = PeriodType.getCalendar(); Period period = PeriodType.getPeriodFromIsoString( isoPeriod ); if ( !period.getPeriodType().getName().contains( WeeklyPeriodType.NAME ) )
public Period createPeriod( Calendar cal ) { org.hisp.dhis.calendar.Calendar calendar = getCalendar(); return createPeriod( calendar.fromIso( DateTimeUnit.fromJdkCalendar( cal ) ), calendar ); }
/** * Generates a period based on the given iso8601 formatted string. * * @param isoDate the iso8601 string. * @return the period. */ public Period createPeriod( String isoDate ) { return createPeriod( dateUnitFormat.parse( isoDate ) ); }
/** * This method checks whether the String inDate is a valid date following * the format "yyyy-MM-dd". * * @param dateString the string to be checked. * @return true/false depending on whether the string is a date according to the format "yyyy-MM-dd". */ public static boolean dateIsValid( String dateString ) { return dateIsValid( PeriodType.getCalendar(), dateString ); }
/** * Returns the latest period which is open for data input. Returns null if * data element is not associated with any data sets. * * @return the latest period which is open for data input. */ public Period getLatestOpenFuturePeriod() { int periods = getOpenFuturePeriods(); PeriodType periodType = getPeriodType(); if ( periodType != null ) { Period period = periodType.createPeriod(); // Rewind one as 0 open periods implies current period is locked period = periodType.getPreviousPeriod( period ); return periodType.getNextPeriod( period, periods ); } return null; }
/** * 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. * * @param date the date which is contained by the created period. * @return the valid Period based on the given date */ public Period createPeriod( final Date date ) { return PERIOD_CACHE.get( getCacheKey( date ), s -> createPeriod( createCalendarInstance( date ) ) ); }
/** * @param isoPeriod the ISO period string. */ public static Period createPeriod( String isoPeriod ) { return PeriodType.getPeriodFromIsoString( isoPeriod ); }
private Period getPeriod( SMSCommand command, Date date ) { Period period = null; period = command.getDataset().getPeriodType().createPeriod(); PeriodType periodType = period.getPeriodType(); if ( command.isCurrentPeriodUsedForReporting() ) { period = periodType.createPeriod( new Date() ); } else { period = periodType.getPreviousPeriod( period ); } if ( date != null ) { period = periodType.createPeriod( date ); } return period; }
/** * Returns a Period which is the previous of the given Period. Only valid * Periods are returned. If the given Period is of different PeriodType than * the executing PeriodType, or the given Period is invalid, the returned * Period might overlap the given Period. * * @param period the Period to base the previous Period on. * @return a Period which is the previous of the given Period. */ public final Period getPreviousPeriod( Period period ) { return getPreviousPeriod( period, getCalendar() ); }
/** * Returns a Period which is the previous of the given Period. Only valid * Periods are returned. If the given Period is of different PeriodType than * the executing PeriodType, or the given Period is invalid, the returned * Period might overlap the given Period. * * @param period the Period to base the previous Period on. * @return a Period which is the previous of the given Period. */ public Period getPreviousPeriod( Period period, org.hisp.dhis.calendar.Calendar calendar ) { DateTimeUnit dateWithOffset = getDateWithOffset( createLocalDateUnitInstance( period.getStartDate(), calendar ), -1, calendar ); return createPeriod( dateWithOffset, calendar ); }
@Override public int compare( Period period1, Period period2 ) { PeriodType a = period1.getPeriodType(); PeriodType b = period2.getPeriodType(); int freqCompare = Integer.compare( a.getFrequencyOrder(), b.getFrequencyOrder() ); int nameCompare = a.getName().compareTo( b.getName() ); return freqCompare == 0 ? ( nameCompare == 0 ? period1.getStartDate().compareTo(period2.getStartDate() ) : nameCompare ) : freqCompare; } }
/** * Returns a period based on the given date string in ISO format. Returns * null if the date string cannot be parsed to a period. * * @param isoPeriod the date string in ISO format. * @return a period. */ public static Period getPeriodFromIsoString( String isoPeriod ) { if ( isoPeriod != null ) { PeriodType periodType = getPeriodTypeFromIsoString( isoPeriod ); try { return periodType != null ? periodType.createPeriod( isoPeriod ) : null; } catch ( Exception ex ) { // Do nothing and return null } } return null; }