-3

I have browsed the whole site to find a solution. But none of the ones I found worked. And most of them were pretty old. So I want to get the current UTC time from the internet. Completely independent from the phone.

Would be nice if someone could help me.

  • Please [edit] your question to show the solutions you've attempted, and to explain exactly how they did not work – Mike M. Apr 26 '19 at 02:19
  • 1
    The [NTP RFC](https://www.ietf.org/rfc/rfc5905.txt) is dated 2010, and its predecessor [RFC 1305](https://www.ietf.org/rfc/rfc1305.txt) is dated 1992. So of course you're going to encounter 'old' solutions. That doesn't mean they won't work. – user207421 Apr 26 '19 at 02:36

3 Answers3

1

This is a free time service which returns the number of minutes since the Unix epoch: https://currentmillis.com/time/minutes-since-unix-epoch.php

The Unix epoch is 00:00:00 UTC Thursday, 1 January 1970

Here is some code to fetch this number and create an Instant object out of it in Java:

URL url = new URL("https://currentmillis.com/time/minutes-since-unix-epoch.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
long minutes = Long.parseLong(in.readLine());
in.close();
con.disconnect();
Instant instant = Instant.ofEpochSecond(minutes * 60);

Note that the time resolution of this service is in minutes, so it will not be accurate to the second, you have to decide whether it's sufficient to your purposes. Also note that if you ship this code it would be nice to make it more robust (e.g. proper exception handling). If there's need for more precise times, NTP is the way: use of ntp service

Sandman
  • 2,577
  • 2
  • 21
  • 32
  • The troublesome `Calendar` class was supplanted years ago by the modern *java.tome* classes. – Basil Bourque Apr 26 '19 at 09:20
  • Force of habit i guess.. Updated answer with Instant instead of Calendar – Sandman Apr 26 '19 at 09:53
  • Thanks a lot Sandman, loved it, I just wanted to know is there any daily usage type of limit on it, and does this php call url stay for long in future? –  Aug 11 '19 at 10:14
  • No daily usage limit as it's served via Cloudflare. This is my flagship hobby project so, yes, this is a service for the long run :) – Sandman Jan 12 '20 at 18:09
0

Just ask google. :)

curl -i google.com | grep Date
Paul Nikonowicz
  • 3,883
  • 21
  • 39
  • Hmm i wonder if this is full compliance with Google's TOS, especially if it would be executed from multiple Android users installing the app. Normally services don't "like" when they're used this way. Also, because it's Android, the author probably prefers a Java-based solution – Sandman Apr 26 '19 at 10:00
0

I am guessing you meant on coding, if yes these are the ways of getting curent time and date on JAVA:

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class currentTime {
 public static void main(String[] args) {
    //1
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd 
    HH:mm:ss");
    LocalDateTime now = LocalDateTime.now();
    System.out.println(dtf.format(now));
    //2
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    Date date = new Date();
    System.out.println(formatter.format(date));
    //3
    System.out.println(java.time.LocalDate.now());
    //4
    System.out.println(java.time.LocalTime.now());
    //5
    System.out.println(java.time.LocalDateTime.now());
    //6
    System.out.println(java.time.Clock.systemUTC().instant());
    //7
    java.util.Date date1=new java.util.Date();
    System.out.println(date1);
    //8
    long millis=System.currentTimeMillis();
    java.util.Date date2=new java.util.Date(millis);
    System.out.println(date2);
    //9
    Date date3=java.util.Calendar.getInstance().getTime();
    System.out.println(date3);

}

}

Liam Wilson
  • 127
  • 7
  • This Answer fails to address the Question. The Question asked specifically to get the current moment from the internet, *not* the local device. – Basil Bourque Apr 26 '19 at 09:14
  • `LocalDateTime` cannot track a moment and is the wrong class to be using here. Calling `LocalDateTime.now()` is almost always the wrong thing to do. – Basil Bourque Apr 26 '19 at 09:16
  • Mixing the legacy classes such as `Date` and `SimpleDateFormat` with the modern *java.time* is needless, confusing, and impractical. – Basil Bourque Apr 26 '19 at 09:18