Created
November 1, 2012 16:32
-
-
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
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
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