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