I check date from what an atomic time server will send me at the beginning at the code.
Then, I will compare that date with a specific date. If the atomic time is less, then System.exit(0).
public static GregorianCalendar getAtomicTime() throws IOException {
BufferedReader in = null;
try {
URLConnection conn = new URL("http://64.90.182.55:13").openConnection();
GregorianCalendar calendar = new GregorianCalendar();
conn.setConnectTimeout(1000);
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String atomicTime;
while (true) {
if ((atomicTime = in.readLine()).indexOf("*") > -1) {
break;
}
}
//System.out.println("DEBUG : " + atomicTime);
String[] fields = atomicTime.split(" ");
String[] date = fields[1].split("-");
calendar.set(Calendar.YEAR, 2000 + Integer.parseInt(date[0]));
calendar.set(Calendar.MONTH, Integer.parseInt(date[1]) - 1);
calendar.set(Calendar.DATE, Integer.parseInt(date[2]));
// deals with the timezone and the daylight-saving-time
TimeZone tz = TimeZone.getDefault();
int gmt = (tz.getRawOffset() + tz.getDSTSavings()) / 3600000;
//System.out.println("DEBUG : " + gmt);
String[] time = fields[2].split(":");
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(time[0]) + gmt);
calendar.set(Calendar.MINUTE, Integer.parseInt(time[1]));
calendar.set(Calendar.SECOND, Integer.parseInt(time[2]));
return calendar;
} catch (IOException e) {
throw e;
} finally {
if (in != null) {
in.close();
}
}
}