Android - 获取系统时间和网络时间

版权声明:交流加qq:2313133415, 希望大家一起学习交流!!! https://blog.csdn.net/djp13276475747/article/details/87885821

Android获取时间的方法有获取网络时间或者使用系统时间,两者我感觉各有优点各有缺点。

话不多说,直接上代码,用的话拿就好!!!

通过网络获取时间,这是我写的一个工具类,在子线程直接调用就可以,参考一下:

public static Date getNetTime(){
        String webUrl = "http://www.ntsc.ac.cn";//中国科学院国家授时中心
        try {
            URL url = new URL(webUrl);
            URLConnection uc = url.openConnection();
            uc.setReadTimeout(5000);
            uc.setConnectTimeout(5000);
            uc.connect();
            long correctTime = uc.getDate();
            Date date = new Date(correctTime);
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }
获取本地系统时间代码:
long time = System.currentTimeMillis();
Date date = new Date(time);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒");
main_date.setText(format.format(date));

全部代码:

package com.djp.test.test2;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 一般耗时操作都不允许放在主线程里直接操作,如网络操作等,
 * 一般需要开一个子线程去访问网络,然后通过handler去更新界面。
 * 显示例如2019年02月22日  周五  14时56分08秒
 */
//温度  多云转晴  南风3-4级
public class TwoActivity extends AppCompatActivity {
    private  TextView textView;

    private Handler handler = new Handler(){
        //此方法在主线程中使用,用于刷新UI
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    /*long time = System.currentTimeMillis();
                    Date date = new Date(time);
                    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒");
                    main_date.setText(format.format(date));*/



                  // textView.setText(new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒").format(new Date(System.currentTimeMillis())));//获取系统时间

                    textView.setText(new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒").format(msg.obj));//获取网络时间
                    break;

            }
        }
    };

    /**
     * 获取当前时间
     * @return
     */
    public static Date getNetTime(){
        //设置了超时时长为5秒,若获取网络时间在5秒内无法获取,则返回本机时间。
        String webUrl = "http://www.ntsc.ac.cn";//中国科学院国家授时中心
        try {
            URL url = new URL(webUrl);
            URLConnection uc = url.openConnection();//生成连接对象
            uc.setReadTimeout(5000);
            uc.setConnectTimeout(5000);
            uc.connect();//发出连接
            long correctTime = uc.getDate();//取得网站日期时间
            Date date = new Date(correctTime);
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        textView = findViewById(R.id.textview1);
        new TimeThread().start();//启动线程
    }
    public class TimeThread extends Thread {
        @Override
        public void run() {
            super.run();
            do {
                try {
                    Thread.sleep(1000);


                    Message msg = new Message();
                    msg.what = 1;
                    //调用获取时间
                    msg.obj= getNetTime();

                    handler.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    e.printStackTrace();
                }
            } while (true);
        }


    }
}

猜你喜欢

转载自blog.csdn.net/djp13276475747/article/details/87885821