2

I am using the following code to get time from online server:

I have included the following headers

import java.net.InetAddress;
import java.util.Date;
import org.apache.commons.net.ntp.NTPUDPClient; 
import org.apache.commons.net.ntp.TimeInfo;

This is my main code

        public static void main(String args[]) throws Exception {
                String TIME_SERVER = "time-a.nist.gov";   
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);

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

But the problem is, that this code returns me the time set on my local machine and not one from the server.!!!

  • @MattJones Sir, Still the same problem! –  Oct 01 '13 at 05:01
  • Now I am interested... let me give this a whirl – Matt Jones Oct 01 '13 at 05:09
  • 1
    @MattJones NOTE, Sir i want Indian Standard Time –  Oct 01 '13 at 05:15
  • 1
    @MattJones Sorry sir, what i said was wrong, it does take time from internet but now even transmit one is working but the problem is that i always want it to display greenwich time but it shows according to the region set on the machine; because if someone changes their time zone , the time changes SO I WANT GMT I dont now how it suddenly started working –  Oct 01 '13 at 05:26
  • Ok. I found your NTP code on SO: http://stackoverflow.com/questions/4442192/how-to-use-an-internet-time-server-to-get-the-time. You need to convert the timeZone of a date. Perhaps you can find this on SO too. http://stackoverflow.com/questions/9429357/date-and-time-conversion-to-some-other-timezone-in-java – Matt Jones Oct 01 '13 at 05:29
  • @MattJones Sir also one more thing, i get a error if i do this below my code str = (time.toString()); jLabel1.setText("a"); –  Oct 01 '13 at 05:32
  • @MattJones It says non static variable jLabel1 cannot be reffered from a static context –  Oct 01 '13 at 05:33
  • because you are accessing a non-static variable from a static context (main) – Matt Jones Oct 01 '13 at 05:38
  • @MattJones Sir so how to perform what i want?? –  Oct 01 '13 at 05:40
  • @MattJones Sir, and one more thing; i had coded after seeing answer of http://stackoverflow.com/questions/4442192/how-to-use-an-internet-time-server-to-get-the-time BUT i saw the other link you gave and i started trying it but i am unable to know on what headers should i include for Calender and all and all?????? –  Oct 01 '13 at 05:42

1 Answers1

0

Give something like this a go?

String TIME_SERVER = "time-a.nist.gov";   
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);
TheDudeAbides
  • 420
  • 1
  • 7
  • 21