0

My app contains listview that are time limited. In another activity, I have a PostActivity that let's user post something to mysql server using volley. User post are time-limited. After posting their content, they can post again after a certain hour or minutes. As I mentioned before that my listview is time-limited, they contain timeCreated & timeExpiry. Whenever the user logins and tries to post something, I get the value of timeExpiry and check if timeExpiry - currentTime (timeExpiry, currentTime & timeCreated) are all in milliseconds

Long currentTime = System.currentTimeMillis()

Long remainingTime = userDate.getTimeExpiry - currentTime;

if (remainingTime > 0 || remainingTime == 0) { 
//User is allowed to post
}

!Wait User can change their mobile time and post unlimitedly!!

Now for the sake of simplicity, is there anyway to get the time from my mysql server or from a NTP-Server. I tried alot of classes such as the SntpClient class**(link below)** `

but I get the date before 1970-1-1

https://gist.github.com/prasann/9003350

I used this function but the method DateUtils.getFormattedDateTime() was not implemened in any DateUtils class.

public String getUTCTime(){
    long nowAsPerDeviceTimeZone = 0;
    SntpClient sntpClient = new SntpClient();

    if (sntpClient.requestTime("0.de.pool.ntp.org", 30000)) {
        nowAsPerDeviceTimeZone = sntpClient.getNtpTime();
        Calendar cal = Calendar.getInstance();
        TimeZone timeZoneInDevice = cal.getTimeZone();
        int differentialOfTimeZones = timeZoneInDevice.getOffset(System.currentTimeMillis());
        nowAsPerDeviceTimeZone -= differentialOfTimeZones;
    }
    return DateUtils.getFormattedDateTime(new Date(nowAsPerDeviceTimeZone));
}

"0.de.pool.ntp.org" NTP-Server time for Germany

mikaeel
  • 57
  • 6
  • a simple way could be listen to time change event in mobile and act based on this, like setting a flag in preferences that user have manipulated time, and should not post... etc, check this question http://stackoverflow.com/questions/5481386/date-and-time-change-listener-in-android – Yazan Mar 20 '16 at 13:11
  • Thanks! For the sake of my protocol, i will use this method. – mikaeel Mar 20 '16 at 13:43
  • Did you try any of these: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java%20get%20time%20from%20internet – nasch Mar 20 '16 at 14:32

0 Answers0