Created
December 9, 2015 00:30
-
-
Save jreece1567/2bef7136a71be538e135 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
private Date adjustDateTimezone(final Date rawDate, final TimeZone tz) { | |
// the 'rawDate' was passed by the API as a 'local' date, | |
// and this date has been parsed as if it were in UTC, so it needs to be | |
// adjusted | |
// to the 'local' time indicated by the TimeZone param. | |
// get the 'raw' GMT-offset for the timezone (not accounting for DST) | |
final Long tzOffset = (tz.getRawOffset() * -1L); | |
int dstOffset; | |
// account for DST if it is in effect for this date | |
if (tz.inDaylightTime(rawDate)) { | |
dstOffset = tz.getDSTSavings(); | |
if (tzOffset < 0) { | |
dstOffset = dstOffset * -1; | |
} | |
} else { | |
dstOffset = 0; | |
} | |
// adjust the UTC date back to 'local' time, including DST offset | |
final Date adjustedDate = new Date(rawDate.getTime() | |
+ (tzOffset + dstOffset)); | |
return adjustedDate; | |
} | |
private void adjustDealTimezone(final DealInstance deal, final TimeZone tz) { | |
// Adjust and update the deal-dates | |
deal.setPublished_at(adjustDateTimezone(deal.getPublished_at(), tz)); | |
deal.setStarts_at(adjustDateTimezone(deal.getStarts_at(), tz)); | |
deal.setEnds_at(adjustDateTimezone(deal.getEnds_at(), tz)); | |
} | |
private void adjustDealsTimezone(final List<DealInstance> deals, | |
final TimeZone tz) { | |
// adjust the dates for all deals in the list | |
for (final DealInstance deal : deals) { | |
adjustDealTimezone(deal, tz); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment