0

I´m trying to get the real current in my country, however the methods I used are dependant on the time on my device. I set the time on my device to 9 AM while in reality it was 7 PM and the following method returned 8 AM, which is ofcourse incorrect.

simpledate.setTimeZone(TimeZone.getTimeZone("Europe/Slovakia"));
to_time = simpledate.format(new Date());

Is there any way to get the real time in the country without it being dependant on system time? I assume the device must be connected to the Internet. Also my country uses DST, so it must take that into consideration too. Browsed similar question, none helped.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Chris Fodor
  • 119
  • 21
  • 1
    Possible duplicate of [How can I get the "network" time, (from the "Automatic" setting called "Use network-provided values"), NOT the time on the phone?](https://stackoverflow.com/questions/8049912/how-can-i-get-the-network-time-from-the-automatic-setting-called-use-netw) – TheWanderer Sep 08 '18 at 18:57
  • 1
    Add an SNTP client to your app and get the current time from a time server. – CommonsWare Sep 08 '18 at 19:01
  • The name of Slovakia time zone is Europe/Bratislava (not Europe/Slovakia). The majority of time zones are named after largest city (though some islands are named after the island name, for example Europe/Jersey). – Ole V.V. Sep 08 '18 at 19:31
  • Possible duplicate of [How to use an Internet time server to get the time?](https://stackoverflow.com/questions/4442192/how-to-use-an-internet-time-server-to-get-the-time) – Ole V.V. Sep 08 '18 at 19:35

2 Answers2

1

There are two ways to do that whether you will need to have access to a webservice that provides current date and time in JSON format or XML, OR, you could parse the time from a website, like

http://www.timeanddate.com/worldclock/

Another alternative code snippet for getting time from a Time Server. You need an Apache Commons Net libray for this to work.

import org.apache.commons.net.time.TimeTCPClient;

public final class GetTime {

public static final void main(String[] args) {
    try {
        TimeTCPClient client = new TimeTCPClient();
        try {
            client.setDefaultTimeout(60000);
            client.connect("time.nist.gov");
            System.out.println(client.getDate());
        }
        catch (IOException e) {
           e.printStackTrace();
        }
        finally {
           client.disconnect();
        }
    }
}

You can find other time servers at HERE. Just replace time.nist.gov with one.

Hussam
  • 1,606
  • 9
  • 20
  • I used the code snippet but I am getting `android.os.NetworkOnMainThreadException ` on line `client.connect("time.nist.gov"); ` – Chris Fodor Sep 08 '18 at 20:22
  • Exception arises due to network operation on main thread of a program. Try creating a separate thread for this operation or use Async Task. – Hussam Sep 08 '18 at 20:34
  • For a quick check try adding `StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); ` in your class just to check if this code will work for you. – Hussam Sep 08 '18 at 20:37
  • Yeah, slightly changed the code, wrapped it into an Asynctask and it is now working. Thank you for your answer. – Chris Fodor Sep 09 '18 at 20:09
  • glad to help. :) – Hussam Sep 09 '18 at 20:15
0
package com.myapp.utils;

import android.os.StrictMode;
import java.io.IOException;
import org.apache.commons.net.time.TimeTCPClient;


public class InternetTime {

    public static long getFechaHora() {
        long time = 0;
        try {
            TimeTCPClient client = new TimeTCPClient();

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            try {
                client.setDefaultTimeout(5000);
                client.connect("time.nist.gov");
                //System.out.println(client.getDate());
                time = client.getTime();
            } finally {
                client.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return time;
    }

}

To avoid the networkonmainthreadexception

Angel
  • 190
  • 2
  • 10