Skip to content

Instantly share code, notes, and snippets.

@ManuelPeinado
Created November 1, 2012 16:32
Show Gist options
  • Save ManuelPeinado/3994869 to your computer and use it in GitHub Desktop.
Save ManuelPeinado/3994869 to your computer and use it in GitHub Desktop.
See if a certain time has already passed in a given time zone
public class IsPastTest {
public static final String SPAIN = "Europe/Madrid";
public static final String PORTUGAL = "Europe/Lisbon";
public boolean isPast(int year, int month, int day, int hour, int minute) {
return isPast(year, month, day, hour, minute, null);
}
public boolean isPast(int year, int month, int day, int hour, int minute, String timeZone) {
TimeZone tz = timeZone == null ? TimeZone.getDefault() : TimeZone.getTimeZone(timeZone);
Calendar query = new GregorianCalendar(tz);
query.set(year, month, day, hour, minute);
Calendar now = new GregorianCalendar();
return now.after(query);
}
public static void main(String[] args) {
// Suppose we are in Madrid and local time is 17:30 of Nov 1st, 2012
boolean b1 = isPast(2012, 10, 1, 17, 0, PORTUGAL); // False, it's 16:30 in Portugal
boolean b2 = isPast(2012, 10, 1, 16, 0, PORTUGAL); // True
// Suppose we are in New York and local time is 12:30 of Nov 1st, 2012
boolean b3 = isPast(2012, 10, 1, 18, 0, SPAIN); // False, it's 17:30 in Spain
boolean b4 = isPast(2012, 10, 1, 17, 0, SPAIN); // True
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment