How to get current time from internet in android
You can get time from internet time servers using the below program
import java.io.IOException;
import org.apache.commons.net.time.TimeTCPClient;
public final class GetTime {
public static final void main(String[] args) {
try {
TimeTCPClient client = new TimeTCPClient();
try {
// Set timeout of 60 seconds
client.setDefaultTimeout(60000);
// Connecting to time server
// Other time servers can be found at : http://tf.nist.gov/tf-cgi/servers.cgi#
// Make sure that your program NEVER queries a server more frequently than once every 4 seconds
client.connect("time.nist.gov");
System.out.println(client.getDate());
} finally {
client.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.You would need Apache Commons Net library for this to work. Download the library and add to your project build path.
(Or you can also use the trimmed Apache Commons Net Library here : https://www-us.apache.org/dist//commons/net/binaries/commons-net-3.6-bin.tar.gz This is enough to get time from internet )
2.Run the program. You will get the time printed on your console.
How to get current time and date in Android
You could use:
import java.util.Calendar
Date currentTime = Calendar.getInstance().getTime();
There are plenty of constants in Calendar for everything you need.
Check the Calendar class documentation.
Get actual time from internet ?
You can use a rest full api provided by geo names http://www.geonames.org/login it will require lat and long for this purpose for example
http://api.geonames.org/timezoneJSON?lat=51.5034070&lng=-0.1275920&username=your_user_name
Android get actual time ( When internet is off and device time is incorrect)
What about this solution:
GPS-time in Android?
This will require the GPS receiver though.
Get current date and time from Google in Android
Try this:
private long getTime() throws Exception {
String url = "https://time.is/Unix_time_now";
Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
String[] tags = new String[] {
"div[id=time_section]",
"div[id=clock0_bg]"
};
Elements elements= doc.select(tags[0]);
for (int i = 0; i <tags.length; i++) {
elements = elements.select(tags[i]);
}
return Long.parseLong(elements.text() + "000");
}
Gradle:
compile 'org.jsoup:jsoup:1.10.2'
Related Topics
Push Notifications in Android Platform
Difference Between Fragmentpageradapter and Fragmentstatepageradapter
Android: How to Have Viewpager Wrap_Content
Listen to Volume Buttons in Background Service
Finished with Non Zero Exit Value
The Setmobiledataenabled Method Is No Longer Callable as of Android L and Later
How to Get the Actionbar Height
Material Effect on Button with Background Color
Android: Java.Lang.Securityexception: Permission Denial: Start Intent
Proper Implementation of Viewpager2 in Android
How to Dynamically Update a Listview on Android
How to Force Use of Overflow Menu on Devices With Menu Button
How to Pass a Variable from Activity to Fragment, and Pass It Back
How to Start Activity in Another Application
Using Intent in an Android Application to Show Another Activity
How to Store(Bitmap Image) and Retrieve Image from SQLite Database in Android
You Have Not Accepted the License Agreements of the Following Sdk Components