Si vous avez besoin API Web qui vous donne le temps réel que vous pouvez utiliser timezonedb:
vous pouvez obtenir l'heure actuelle pour tout fuseau horaire
Demande
http://api.timezonedb.com/v2/get-time-zone?key=YOUR_API_KEY&format=json&by=zone&zone=America/Chicago
Réponse
{
"status":"OK",
"message":"",
"countryCode":"US",
"countryName":"United States",
"zoneName":"America\/Chicago",
"abbreviation":"CST",
"gmtOffset":-21600,
"dst":"0",
"dstStart":1446361200,
"dstEnd":1457856000,
"nextAbbreviation":"CDT",
"timestamp":1454446991,
"formatted":"2016-02-02 21:03:11"
}
Mise à jour: Exemple Android
API Key est une valeur donnée pour vous à partir du serveur API lorsque vous (gratuit) register to the website
j'ai écrit un exemple pour obtenir le Les données.
private Response getServerTime(){
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://api.timezonedb.com/v2/get-time-zone?key=YOUR_API_KEY&format=json&by=zone&zone=America/Chicago");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
StringBuilder sb = new StringBuilder();
String s;
while ((s = in.readLine()) != null)
sb.append(s);
String json = sb.toString();
Response response = new GsonBuilder().create().fromJson(in, Response.class);
return response;
} catch(Exception e) {
e.printStackTrace();
}
finally {
if(urlConnection != null)
urlConnection.disconnect();
}
return null;
}
Aussi, créez le modèle pour la réponse
public class Response {
public String status;
public String message;
public String countryCode;
public String countryName;
public String zoneName;
public String abbreviation;
public int gmtOffset;
public String dst;
public int dstStart;
public int dstEnd;
public String nextAbbreviation;
public int timestamp;
public String formatted;
}
Et ajouter bibliothèque Gson au build.gradle de votre application
dependencies {
..
compile 'com.google.code.gson:gson:2.8.2'
}
i doesn ne pas dire que pour obtenir le temps à partir de l'appareil :( – Fodis