Danke schön auch dir, ARadauer. Ich werde die Systemzeit aber nicht verstellen, wenn ich dadurch einen Teil Plattformunabhängig verliere. Ansonsten habe ich jetzt eine funktionierende Lösung gefunden, die zentrale Hilfsklasse habe ich angefügt.
Die kleine, aber feine Bibliothek, die alle meine Probleme gelöst hat, heißt AtomicDate und ist hier verfügbar:
http://atomicdate.sourceforge.net. Manchmal kann es so einfach sein, wenn man das richtige Stichwort (in diesem Fall SNTP) bekommt.
[HIGHLIGHT="Java"]/**
* Provides methods to retrieve current network time and local time offset.
*
* @param haddr The time server host address.
*/
class AtomicTime{
/**
* The default time server.
*/
private String haddr = "ptbtime1.ptb.de";
/**
* Default value for the number of attempts to be made to retrieve the network time offset.
*/
private int attempts = 3;
/**
* The error message that will be returned if network time or offset can't be retrieved.
*/
private String errorMessage;
/**
* Default format for times.
*/
final static public SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss.SSS");
/**
* Default format for date and time.
*/
final static public SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("z yyyy-MM-dd HH:mm:ss.SSS");
/**
* @param haddr The time server host address.
*/
public AtomicTime(String haddr){
this.haddr = haddr;
this.errorMessage = "Can't receive current time from + " + haddr + ".";
}
/**
* Uses the default time server.
*/
public AtomicTime(){
this.errorMessage = "Can't receive current time from + " + haddr + ".";
}
/**
* @return The local time offset relative to the network time, or Long.MIN_VALUE if it couldn't be retrieved.
*
* @param n The number of attempts to be made to retrieve the network time offset.
*/
public long getOffset(int n){
long offset = Long.MIN_VALUE;
net.sf.atomicdate.Client client = null;
try{
client = new net.sf.atomicdate.Client(1000);
}
catch(java.net.SocketException e){
System.out.println(errorMessage);
// e.printStackTrace();
}
if(client != null){
long[] offsets = new long[n];
boolean[] success = new boolean[n]; for(int i=0; i<n; i++){ success
= true; }
int cnt = 0;
for(int i=0; i<n; i++){ // n attempts
try{
offsets = client.getOffset(haddr); // retrieve the network time offset from the specified SNTP server
// System.out.println("" + i + ": " + offsets);
}
catch(IOException e){
success = false;
cnt++;
// e.printStackTrace();
}
}
int validOffsets = n - cnt;
if(validOffsets > 0){
offset = 0;
for(int i=0; i<n; i++){
if(success){
// System.out.println("" + i + ": " + offsets);
offset += offsets;
}
}
// System.out.println("" + offset + ", " + validOffsets + ", " + ((double)offset/validOffsets));
offset = Math.round((double)offset/validOffsets);
}
else{ System.out.println(errorMessage); }
client.stopListening();
}
return offset;
}
/**
* @return The local time offset relative to the network time, or Long.MIN_VALUE if it couldn't be retrieved.
*/
public long getOffset(){
return getOffset(attempts);
}
/**
* @return The current network time, or null if it couldn't be retrieved.
*
* @param n The maximum number of attempts to be made to retrieve network time.
*/
public Date getNetworkTime(int n){
net.sf.atomicdate.Date date = null;
int count = 0;
while(date == null && count < n){ // n attempts
try{
date = new net.sf.atomicdate.Date(haddr);
// System.out.println(dateTimeFormat.format(date));
count++;
}
catch(IOException ioe){
// ioe.printStackTrace();
}
}
if(date == null){ System.out.println(errorMessage); }
return date;
}
}[/HIGHLIGHT]