Monday, 24 June 2013

Set System Date& Time With Internet Date & Time Automatic...

Run This Program and set date & time your system......


:( you need internet Connection...!!!)


import java.io.IOException;
import java.net.InetAddress;
import java.util.Date;


import org.apache.commons.net.ntp.NTPUDPClient;

import org.apache.commons.net.ntp.TimeInfo;

//For this library you need commons-net-3.3 Jar File....

public class Data {
    public static final String TIME_SERVER = "in.pool.ntp.org";  
   
   
    public static void main(String...s) throws IOException{

//Code For gate Date & Time From Time Server....
 
    NTPUDPClient timeClient = new NTPUDPClient();  
    InetAddress inetAddress =

InetAddress.getByName(TIME_SERVER);
    TimeInfo timeInfo = timeClient.getTime(inetAddress);
    long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();
    Date time = new Date(returnTime);
    System.out.println(time);
    Date d = new Date();
       String strDateToSet = time.getDate()+"-"+(time.getMonth()+1)+"-"+(time.getYear()+1900);
       String strTimeToSet = time.getHours()+":"+time.getMinutes()+":"+time.getSeconds();
 

  //Code For Set Date & time.....

if(d!=time){
     Runtime.getRuntime().exec("cmd /C date " + strDateToSet); // dd-MM-yy
    Runtime.getRuntime().exec("cmd /C time " + strTimeToSet); // hh:mm:ss
   }
    }
}

Any Query ????

Contact ....

Karmadip Dodiya

+91 8980216689



No comments:

Post a Comment