Android - Network Date/Time

前端 未结 2 1244
醉话见心
醉话见心 2021-01-14 08:24

I\'m in the process of writing an app for Android. I need to allow my users to capture their current location & log the date/time that this happened. The catch is that t

2条回答
  •  迷失自我
    2021-01-14 09:08

     public static final String TIME_SERVER = "time-a.nist.gov";
     public static void printTimes() throws IOException {
            NTPUDPClient timeClient = new NTPUDPClient();
            InetAddress inetAddress = InetAddress.getByName(TIME_SERVER);
            TimeInfo timeInfo = timeClient.getTime(inetAddress);
            //long returnTime = timeInfo.getReturnTime();   //local device time
            long returnTime = timeInfo.getMessage().getTransmitTimeStamp().getTime();   //server time
    
            //Get Current Time
            Long tsLong = System.currentTimeMillis()/1000;
            String ts = tsLong.toString();
            Log.e("After get PrintTime..","After get PrintTime..>>"+ts);
    
        enter code here
            Log.e("getCurrentNetworkTime", "Time from " + TIME_SERVER + ": " + returnTime/*time*/);
    
        }
    

提交回复
热议问题