Java Code Examples for org.joda.time.DateTime#minusYears()

The following examples show how to use org.joda.time.DateTime#minusYears() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private DateInterval toYearIsoInterval( DateTimeUnit dateTimeUnit, int offset, int length )
{
    DateTime from = dateTimeUnit.toJodaDateTime( chronology );

    if ( offset > 0 )
    {
        from = from.plusYears( offset );
    }
    else if ( offset < 0 )
    {
        from = from.minusYears( -offset );
    }

    DateTime to = new DateTime( from ).plusYears( length ).minusDays( 1 );

    DateTimeUnit fromDateTimeUnit = DateTimeUnit.fromJodaDateTime( from );
    DateTimeUnit toDateTimeUnit = DateTimeUnit.fromJodaDateTime( to );

    fromDateTimeUnit.setDayOfWeek( isoWeekday( fromDateTimeUnit ) );
    toDateTimeUnit.setDayOfWeek( isoWeekday( toDateTimeUnit ) );

    return new DateInterval( toIso( fromDateTimeUnit ), toIso( toDateTimeUnit ), DateIntervalType.ISO8601_YEAR );
}
 
Example 2
Source Project: nomulus   File: TimeOfYear.java    License: Apache License 2.0 4 votes vote down vote up
/** Get the first {@link DateTime} with this month/day/millis that is at or before the end. */
public DateTime getLastInstanceBeforeOrAt(DateTime end) {
  DateTime withSameYear = getDateTimeWithYear(end.getYear());
  return isBeforeOrAt(withSameYear, end) ? withSameYear : withSameYear.minusYears(1);
}
 
Example 3
Source Project: orianna   File: MatchAPI.java    License: MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Get(Matchlist.class)
public Matchlist getMatchlist(final Map<String, Object> query, final PipelineContext context) {
    final Platform platform = (Platform)query.get("platform");
    final String accountId = (String)query.get("accountId");
    final Set<Integer> queues = query.get("queues") == null ? Collections.<Integer> emptySet() : (Set<Integer>)query.get("queues");
    final Set<Integer> seasons = query.get("seasons") == null ? Collections.<Integer> emptySet() : (Set<Integer>)query.get("seasons");
    final Set<Integer> champions = query.get("champions") == null ? Collections.<Integer> emptySet() : (Set<Integer>)query.get("champions");
    Number beginTime = (Number)query.get("beginTime");
    Number endTime = (Number)query.get("endTime");
    Number beginIndex = (Number)query.get("beginIndex");
    Number endIndex = (Number)query.get("endIndex");
    Utilities.checkNotNull(platform, "platform", accountId, "accountId");

    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final DateTime historyStart = now.minusYears(3);

    // Time Handling
    if(beginTime != null && beginTime.longValue() < historyStart.getMillis()) {
        beginTime = historyStart.getMillis();
    }

    if(endTime != null && endTime.longValue() > now.getMillis()) {
        endTime = now.getMillis();
    }

    if(endTime != null && (beginTime == null || endTime.longValue() - beginTime.longValue() > ONE_WEEK_IN_MILLISECONDS)) {
        beginTime = Math.max(endTime.longValue() - ONE_WEEK_IN_MILLISECONDS, 0);
    }

    // Index Handling
    if(beginIndex != null && endIndex != null && beginIndex.intValue() + MAX_MATCH_INDEX_DIFFERENCE < endIndex.intValue()) {
        endIndex = beginIndex.intValue() + MAX_MATCH_INDEX_DIFFERENCE;
    }

    if(beginIndex != null && endIndex == null && endTime == null) {
        endIndex = beginIndex.intValue() + MAX_MATCH_INDEX_DIFFERENCE;
    }

    if(endIndex != null && beginIndex == null) {
        beginIndex = Math.max(endIndex.intValue() - MAX_MATCH_INDEX_DIFFERENCE, 0);
    }

    final String endpoint = "lol/match/v4/matchlists/by-account/" + accountId;

    final Multimap<String, String> parameters = HashMultimap.create();
    parameters.put("platform", platform.getTag());
    if(beginTime != null) {
        parameters.put("beginTime", beginTime.toString());
    }
    if(endTime != null) {
        parameters.put("endTime", endTime.toString());
    }
    if(beginIndex != null) {
        parameters.put("beginIndex", beginIndex.toString());
    }
    if(endIndex != null) {
        parameters.put("endIndex", endIndex.toString());
    }
    for(final Integer queue : queues) {
        parameters.put("queue", queue.toString());
    }
    for(final Integer season : seasons) {
        parameters.put("season", season.toString());
    }
    for(final Integer champion : champions) {
        parameters.put("champion", champion.toString());
    }

    final Matchlist data = get(Matchlist.class, endpoint, parameters);
    if(data == null) {
        final Matchlist empty = new Matchlist();
        empty.setMatches(Collections.<MatchReference> emptyList());
        empty.setPlatform(platform.getTag());
        empty.setAccountId(accountId);
        empty.setQueues(queues);
        empty.setSeasons(seasons);
        empty.setChampions(champions);
        empty.setStartTime(beginTime == null ? 0L : beginTime.longValue());
        empty.setEndTime(endTime == null ? 0L : endTime.longValue());
        empty.setStartIndex(beginIndex == null ? 0 : beginIndex.intValue());
        empty.setEndIndex(endIndex == null ? 0 : endIndex.intValue());
        empty.setMaxSize(beginTime != null && endTime != null ? Integer.MAX_VALUE : MAX_MATCH_INDEX_DIFFERENCE);
        empty.setMaxTimeRange(endTime != null ? Long.MAX_VALUE : ONE_WEEK_IN_MILLISECONDS);
        empty.setHistoryLength(HISTORY_LENGTH);
        return empty;
    }

    return data;
}
 
Example 4
Source Project: orianna   File: MatchAPI.java    License: MIT License 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Get(MatchList.class)
public MatchList getMatchList(final Map<String, Object> query, final PipelineContext context) {
    final Platform platform = (Platform)query.get("platform");
    final String accountId = (String)query.get("accountId");
    final Set<Integer> queues = query.get("queues") == null ? Collections.<Integer> emptySet() : (Set<Integer>)query.get("queues");
    final Set<Integer> seasons = query.get("seasons") == null ? Collections.<Integer> emptySet() : (Set<Integer>)query.get("seasons");
    final Set<Integer> champions = query.get("champions") == null ? Collections.<Integer> emptySet() : (Set<Integer>)query.get("champions");
    Number beginTime = (Number)query.get("beginTime");
    Number endTime = (Number)query.get("endTime");
    Number beginIndex = (Number)query.get("beginIndex");
    Number endIndex = (Number)query.get("endIndex");
    Utilities.checkNotNull(platform, "platform", accountId, "accountId");

    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final DateTime historyStart = now.minusYears(3);

    // Time Handling
    if(beginTime != null && beginTime.longValue() < historyStart.getMillis()) {
        beginTime = historyStart.getMillis();
    }

    if(endTime != null && endTime.longValue() > now.getMillis()) {
        endTime = now.getMillis();
    }

    if(endTime != null && (beginTime == null || endTime.longValue() - beginTime.longValue() > ONE_WEEK_IN_MILLISECONDS)) {
        beginTime = Math.max(endTime.longValue() - ONE_WEEK_IN_MILLISECONDS, 0);
    }

    // Index Handling
    if(beginIndex != null && endIndex != null && beginIndex.intValue() + MAX_MATCH_INDEX_DIFFERENCE < endIndex.intValue()) {
        endIndex = beginIndex.intValue() + MAX_MATCH_INDEX_DIFFERENCE;
    }

    if(beginIndex != null && endIndex == null && endTime == null) {
        endIndex = beginIndex.intValue() + MAX_MATCH_INDEX_DIFFERENCE;
    }

    if(endIndex != null && beginIndex == null) {
        beginIndex = Math.max(endIndex.intValue() - MAX_MATCH_INDEX_DIFFERENCE, 0);
    }

    final String endpoint = "lol/match/v4/matchlists/by-account/" + accountId;

    final Multimap<String, String> parameters = HashMultimap.create();
    parameters.put("platform", platform.getTag());
    if(beginTime != null) {
        parameters.put("beginTime", beginTime.toString());
    }
    if(endTime != null) {
        parameters.put("endTime", endTime.toString());
    }
    if(beginIndex != null) {
        parameters.put("beginIndex", beginIndex.toString());
    }
    if(endIndex != null) {
        parameters.put("endIndex", endIndex.toString());
    }
    for(final Integer queue : queues) {
        parameters.put("queue", queue.toString());
    }
    for(final Integer season : seasons) {
        parameters.put("season", season.toString());
    }
    for(final Integer champion : champions) {
        parameters.put("champion", champion.toString());
    }

    final MatchList data = get(MatchList.class, endpoint, parameters);
    if(data == null) {
        final MatchList empty = new MatchList();
        empty.setPlatform(platform.getTag());
        empty.setAccountId(accountId);
        empty.setQueues(queues);
        empty.setSeasons(seasons);
        empty.setChampions(champions);
        empty.setStartTime(new DateTime(beginTime == null ? 0L : beginTime.longValue()));
        empty.setEndTime(new DateTime(endTime == null ? 0L : endTime.longValue()));
        empty.setStartIndex(beginIndex == null ? 0 : beginIndex.intValue());
        empty.setEndIndex(endIndex == null ? 0 : endIndex.intValue());
        empty.setMaxSize(beginTime != null && endTime != null ? Integer.MAX_VALUE : MAX_MATCH_INDEX_DIFFERENCE);
        empty.setMaxTimeRange(Duration.millis(endTime != null ? Long.MAX_VALUE : ONE_WEEK_IN_MILLISECONDS));
        empty.setHistoryLength(HISTORY_LENGTH);
        return empty;
    }

    return data;
}
 
Example 5
@Test
public void subtract_years_from_date_in_java_joda () {

	DateTime superBowlXLV = new DateTime(2011, 2, 6, 0, 0, 0, 0);
	DateTime numberFour = superBowlXLV.minusYears(14);

	DateTimeFormatter fmt = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss z");
	
	logger.info(superBowlXLV.toString(fmt));
	logger.info(numberFour.toString(fmt));

	assertTrue(numberFour.isBefore(superBowlXLV));
}