Last active
January 15, 2016 21:32
-
-
Save lekant/7a8020d3ed31d0f11a3a to your computer and use it in GitHub Desktop.
Get previous quarter with JodaTime and google RangeMap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.collect.Range; | |
import com.google.common.collect.RangeMap; | |
import com.google.common.collect.TreeRangeMap; | |
import org.joda.time.DateTime; | |
import org.joda.time.Interval; | |
import java.util.Iterator; | |
import java.util.Map; | |
/** | |
* Created by qlehenaff on 11/23/15. | |
*/ | |
public class JodaTimeUtils { | |
public static Interval getQuarter(DateTime currentDate) { | |
RangeMap<DateTime, Integer> quarters = buildQuartersRanges(currentDate); | |
Range<DateTime> range = quarters.getEntry(currentDate).getKey(); | |
return new Interval(range.lowerEndpoint(), range.upperEndpoint()); | |
} | |
public static Interval getPreviousQuarter(DateTime currentDate) { | |
RangeMap<DateTime, Integer> quarters = buildQuartersRanges(currentDate); | |
final int previousQuarter = getPreviousQuarterKey(currentDate, quarters); | |
Range<DateTime> range = getDateTimeRange(quarters, previousQuarter); | |
return new Interval(range.lowerEndpoint(), range.upperEndpoint()); | |
} | |
protected static Range<DateTime> getDateTimeRange(RangeMap<DateTime, Integer> quarters, int previousQuarter) { | |
final Iterator<Map.Entry<Range<DateTime>, Integer>> iterator = quarters.asMapOfRanges().entrySet().iterator(); | |
Range<DateTime> range = null; | |
while (iterator.hasNext()) { | |
final Map.Entry<Range<DateTime>, Integer> entry = iterator.next(); | |
if (entry.getValue() == previousQuarter) { | |
range = entry.getKey(); | |
break; | |
} | |
} | |
return range; | |
} | |
protected static int getPreviousQuarterKey(DateTime currentDate, RangeMap<DateTime, Integer> quarters) { | |
return ((quarters.get(currentDate) - 1) +4) % 4; | |
} | |
protected static RangeMap<DateTime, Integer> buildQuartersRanges(DateTime currentDate) { | |
RangeMap<DateTime, Integer> quarters = TreeRangeMap.create(); | |
// set quarters | |
DateTime q0BeginDate = new DateTime(currentDate.getYear()-1, 10, 1, 0, 0, 0, 0); | |
DateTime q0EndDate = new DateTime(currentDate.getYear()-1, 12, 31, 23, 59, 59, 999); | |
DateTime q1BeginDate = new DateTime(currentDate.getYear(), 1, 1, 0, 0, 0, 0); | |
DateTime q1EndDate = new DateTime(currentDate.getYear(), 3, 31, 23, 59, 59, 999); | |
DateTime q2BeginDate = new DateTime(currentDate.getYear(), 4, 1, 0, 0, 0, 0); | |
DateTime q2EndDate = new DateTime(currentDate.getYear(), 6, 30, 23, 59, 59, 999); | |
DateTime q3BeginDate = new DateTime(currentDate.getYear(), 7, 1, 0, 0, 0, 0); | |
DateTime q3EndDate = new DateTime(currentDate.getYear(), 9, 30, 23, 59, 59, 999); | |
//add to quarters | |
quarters.put(Range.closed(q0BeginDate, q0EndDate), 0); | |
quarters.put(Range.closed(q1BeginDate, q1EndDate), 1); | |
quarters.put(Range.closed(q2BeginDate, q2EndDate), 2); | |
quarters.put(Range.closed(q3BeginDate, q3EndDate), 3); | |
return quarters; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment