Learn
& Implement

Get Started




Happy New Year

Get Date & Time From Internet

In this tutorial, I will tell you how to get date and time from internet.

build.gradle(module:app)


                            implementation 'org.jsoup:jsoup:1.13.1'
                        

Now add Internet permission in file
AndroidManifest.XML

                            <uses-permission android:name="android.permission.INTERNET"/>
                        

Create new Java class named

TimeClass.java

                        import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TimeClass {

    public static final String URL = "https://time.is/Unix_time_now/";
    public static final String TIME_PATTERN = "dd/MM/yyyy hh:mm a";

    public String getTimeMillis() throws IOException {

        Document document = Jsoup.parse(new URL(URL).openStream(), "UTF-8", URL);

        String[] tags = new String[]{
                "div[id=time_section]",
                "div[id=clock0_bg]"
        };

        Elements elements = document.select(tags[0]);

        for (String tag : tags) {
            elements = elements.select(tag);
        }
        return elements.text() + "111";
    }

    public String toReadableTime(long timeMillis) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timeMillis);
        SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_PATTERN, Locale.ENGLISH);

        return dateFormat.format(cal.getTime());
    }

    public Date toDate(String timeString) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_PATTERN, Locale.ENGLISH);

        return dateFormat.parse(timeString);
    }

}        

                            

activity_main.xml


                        <?xml version="1.0" encoding="utf-8"?>
                        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                            xmlns:tools="http://schemas.android.com/tools"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="@android:color/white"
                            tools:context=".MainActivity">
                        
                            <TextView
                                android:id="@+id/textView"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:text="Loading..."
                                android:textAlignment="center"
                                android:textColor="@android:color/black"
                                android:textSize="24sp" />
                        
                            <Button
                                android:id="@+id/button"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/textView"
                                android:layout_centerHorizontal="true"
                                android:layout_centerVertical="true"
                                android:layout_margin="22dp"
                                android:text="Show Time"
                                android:textColor="@android:color/white" />
                        
                        </RelativeLayout>


MainActivity.java

                        public class MainActivity extends AppCompatActivity {

                            TextView textView;
                            Button button;
                        
                            Thread thread;
                            String time;
                        
                            @Override
                            protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
                        
                                textView = findViewById(R.id.textView);
                                button = findViewById(R.id.button);
                        
                                button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        getDateTime();
                                    }
                                });
                        
                            }
                        
                            private void getDateTime() {
                        
                                Runnable runnable = new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            TimeClass timeClass = new TimeClass();
                                            String t = timeClass.getTimeMillis();
                        
                                            String timeString = timeClass.toReadableTime(Long.parseLong(t));
                        
                                            Date date = timeClass.toDate(timeString);
                        
                                            time = timeString;
                        
                                            System.out.println("TIME_MILLIS: " + t);
                                            System.out.println("TIME_STRING: " + timeString);
                                            System.out.println("TIME_IN_DATE: " + date);
                        
                                        } catch (IOException | ParseException e) {
                                            e.printStackTrace();
                                        }
                                    }
                        
                                };
                        
                                textView.setText(time);
                        
                                thread = new Thread(runnable);
                                thread.start();
                        
                            }
                        
                            @Override
                            protected void onDestroy() {
                                if (thread != null && thread.isAlive())
                                    thread.interrupt();
                        
                                super.onDestroy();
                            }
                        }