import java.net.Socket;
import java.io.InputStream;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.GregorianCalendar;
import java.util.HashMap;
public class DateTime {
private final String day;
private final String month;
private final String year;
private final String hour;
private final String minute;
private final String second;
private final String timeZone;
public DateTime(String day, String month, String year, String hour, String minute, String second, String timeZone) {
this.day = day;
this.month = month;
this.year = year;
this.hour = hour;
this.minute = minute;
this.second = second;
this.timeZone = timeZone;
}
public String getDay() {
return day;
}
public String getMonth() {
return month;
}
public String getYear() {
return year;
}
public String getHour() {
return hour;
}
public String getMinute() {
return minute;
}
public String getSecond() {
return second;
}
public String getTimeZone() {
return timeZone;
}
@Override
public String toString() {
return year + " " + month + " " + day + " " + hour + " " + minute + " " + second + " " + timeZone;
}
static HashMap<String, DateTime> ntpDateTime = new HashMap<>();
public static void getNTPDateTime() throws IOException {
String hostname = "time.inrim.it";
int port = 13;
String timeString = null;
try {
Socket socket = new Socket(hostname, port);
InputStream iStream = socket.getInputStream();
StringBuilder time = new StringBuilder();
int c;
while ((c = iStream.read()) != -1) {
time.append((char) c);
}
timeString = time.toString().trim();
String[] dts = timeString.split(" |:");
/* for (int i=0; i<dts.length; i++){
System.out.print(dts[i]+" "+i+" ");
}*/
String ntpYear = dts[0];
String ntpMonth = dts[1];
String ntpDay = dts[2];
String ntpHour = dts[3];
String ntpMin = dts[4];
String ntpSec = dts[5];
String ntpTZone = dts[6];
String id = "ntp";
DateTime dateTime = new DateTime(ntpYear, ntpMonth, ntpDay, ntpHour, ntpMin, ntpSec, ntpTZone);
ntpDateTime.put(id, dateTime);
String ntpTime = ntpYear + " " + ntpMonth + " " + ntpDay + " " + ntpHour + ":" + ntpMin + ":" + ntpSec + " " + ntpTZone;
System.out.println(ntpTime);
} catch (UnknownHostException | NumberFormatException e) {
System.err.println(e);
}
}
static HashMap<String, DateTime> gDateTime = new HashMap();
public static void gregorianDateTime() {
GregorianCalendar cal = new GregorianCalendar();
String year = String.valueOf(cal.get(GregorianCalendar.YEAR));
String month = String.valueOf(cal.get(GregorianCalendar.MONTH));
if (month.equals("1")) {
month = "FEB";
} else if (month.equals("2")) {
month = "MAR";
} else if (month.equals("3")) {
month = "APR";
} else if (month.equals("4")) {
month = "MAG";
} else if (month.equals("5")) {
month = "GIU";
} else if (month.equals("6")) {
month = "LUG";
} else if (month.equals("7")) {
month = "AGO";
} else if (month.equals("8")) {
month = "SET";
} else if (month.equals("9")) {
month = "OTT";
} else if (month.equals("10")) {
month = "NOV";
} else if (month.equals("11")) {
month = "DIC";
} else if (month.equals("12")) {
month = "GEN";
}
String day = String.valueOf(cal.get(GregorianCalendar.DAY_OF_MONTH));
String hour = String.valueOf(cal.get(GregorianCalendar.HOUR_OF_DAY));
String minute = String.valueOf(cal.get(GregorianCalendar.MINUTE));
String second = String.valueOf(cal.get(GregorianCalendar.SECOND));
String tZone = "CEST";
String id = "greg";
DateTime DateTime = new DateTime(year, month, day, hour, minute, second, tZone);
gDateTime.put(id, DateTime);
String time = day + " " + month + " " + year + " " + hour + ":" + minute + ":" + second + " " + tZone;
System.out.println(time);
}
public static void getDateTime() {
}
public static void setDateTime() throws IOException {
GregorianCalendar cal = new GregorianCalendar();
getNTPDateTime();
String rawTime = ntpDateTime.toString();
String[] ntpTime = rawTime.split(" |:");
gregorianDateTime();
String rawGTime = gDateTime.toString();
String[] gTime = rawGTime.split(" |:");
if (!gTime[0].equals(ntpTime[0])) {
// devo riconvertire i campi in interi per passarli cal cal.set()
}
}
public static void refreshDateTime() throws IOException {
gregorianDateTime();
getNTPDateTime();
if (!ntpDateTime.equals(ntpDateTime)) {
System.out.println("non sono uguali" + gDateTime + " - " + ntpDateTime);
} else {
System.out.println("sono uguali : " + gDateTime + " - " + ntpDateTime);
}
}
}
Molte cose non servono, ci sono solo perchè faccio prove )
Se non va bene il gregorian per il mio caso in alternativa come potrei dare l'orario del sistema e modificare sempre quello del sistema se non fosse uguale a quello preso in rete?
grazie ancora