Fodis
Fodis

Reputation: 67

Get The Current Time From Website

Is there any way to Get the current Time From a Website ?

I Have Created a App . I want to Get the current Time . If user change the time Of the device and the time is not equals to the website don't let user to use the app. I don't understanding how to get the current time . i just need help to get the current time of world .

Upvotes: 0

Views: 4106

Answers (3)

Luca Corradi
Luca Corradi

Reputation: 2131

If you need a web API that gives you the actual time you can use timezonedb:

You can get the current time for any timezone

Request

http://api.timezonedb.com/v2/get-time-zone?key=YOUR_API_KEY&format=json&by=zone&zone=America/Chicago

Response

{
    "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"
}

Update: Android example

API Key is a value given to you from the API server when you (free) register to the website

I wrote an example for get the data.

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;
}

Also, create the model for the response

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;
}

And add Gson library to your app's build.gradle

dependencies {
    ..

    compile 'com.google.code.gson:gson:2.8.2'
}

Upvotes: 0

from56
from56

Reputation: 4127

Easy, adding a php page to the web site. I can post some code later, when I go back home.

ADDED:

Just create this gettime.php file in the web server:

<?php
date_default_timezone_set('UTC');
$currentdatetime = date('Y-m-d H:i:s');
print ($currentdatetime);
?>

And doing http://www.mywebsite.com/gettime.php you will get a string with the UTC web server time

Upvotes: 0

iamsaquib8
iamsaquib8

Reputation: 414

If you just need to output a time stamp in format YYYY.MM.DD-HH.MM.SS (very frequent case) then here's the way to do it:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());

Upvotes: 1

Related Questions