Android Studio的代码笔记--网络时间、系统时间

Android获取网络时间、系统时间

  • URLConnection获取时间
  • Time获取时间
  • Calendar获取前后几天的时间、判断日期为星期几

URLConnection获取时间

	public Date get_Nettime(){
        String web = "http://www.ntsc.ac.cn";//中国科学院国家授时中心
        //http://www.bjtime.cn、http://www.baidu.com
        try {
            URL url = new URL(web);
            URLConnection uc = url.openConnection();
            uc.setReadTimeout(5000);
            uc.setConnectTimeout(5000);
            uc.connect();
            long web_time = uc.getDate();
            Date date = new Date(web_time);
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }

获取当前网络时间

	String net_time1= new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(get_Nettime());
	Log.i("LXH", "URLConnection获取网络时间: "+net_time1);
	I/LXH: URLConnection获取网络时间: 2021-11-17-09-45-55

用子线程每隔一秒更新一次网络时间

	public class Thread_Nettime extends Thread {
        @Override
        public void run() {
            super.run();
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj= get_Nettime();
                    handler_Nettime.sendMessage(msg);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }
    Handler handler_Nettime =  new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String net_time1= new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(get_Nettime());
                    Log.i("LXH", "URLConnection获取网络时间: "+net_time1);
                    break;
            }
            super.handleMessage(msg);
        }
    };

启动线程每隔 1秒获取一次网络时间

	new Thread_Nettime().start();
	I/LXH: URLConnection获取网络时间: 2021-11-17-09-51-33
	I/LXH: URLConnection获取网络时间: 2021-11-17-09-51-34
	I/LXH: URLConnection获取网络时间: 2021-11-17-09-51-35
	I/LXH: URLConnection获取网络时间: 2021-11-17-09-51-36

Time获取时间

	public void get_Time(){
        Time t=new Time();
        t.setToNow();
        int year=t.year;
        int month=t.month+1;
        int date=t.monthDay;
        int week=t.weekDay;
        int hour=t.hour;
        int minute=t.minute;
        int second=t.second;
        String time=year+"-"+month+"-"+date+"-周"+week+"  "+hour+":"+minute+":"+second;
        Log.i("LXH", "Time获取时间: "+time);
    }
	I/LXH: Time获取时间: 2021-11-17-3  10:11:58

Calendar获取前后几天的时间、判断日期为星期几

	public void get_Calendartime(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        Log.i("LXH", "今天:"+format.format(c.getTime())+" 周"+(c.get(Calendar.DAY_OF_WEEK)-1));
        for(int i=0;i<7;i++){
            c.add(Calendar.DAY_OF_YEAR, 1);
            Log.i("LXH", (i+1)+"天后:"+format.format(c.getTime())+" 周"+(c.get(Calendar.DAY_OF_WEEK)-1));
        }
        for(int i=0;i<7;i++){
            c.add(Calendar.DAY_OF_YEAR, -1);
            Log.i("LXH", (i+1)+"天前:"+format.format(c.getTime())+" 周"+(c.get(Calendar.DAY_OF_WEEK)-1));
        }
    }
	I/LXH: 今天:2021-11-17 10:55:383
	I/LXH: 1天后:2021-11-18 10:55:384
	I/LXH: 2天后:2021-11-19 10:55:385
	I/LXH: 3天后:2021-11-20 10:55:386
	I/LXH: 4天后:2021-11-21 10:55:380
	I/LXH: 5天后:2021-11-22 10:55:381
	I/LXH: 6天后:2021-11-23 10:55:382
	I/LXH: 7天后:2021-11-24 10:55:383
	I/LXH: 1天前:2021-11-23 10:55:382
	I/LXH: 2天前:2021-11-22 10:55:381
	I/LXH: 3天前:2021-11-21 10:55:380
	I/LXH: 4天前:2021-11-20 10:55:386
	I/LXH: 5天前:2021-11-19 10:55:385
	I/LXH: 6天前:2021-11-18 10:55:384
	I/LXH: 7天前:2021-11-17 10:55:383

判断输入的日期时间为星期几

 	/**
     * 判断输入的日期时间为星期几
     * @param data  "2021-11-17"
     * @return "星期"+weekid
     */
    public String judge_week(String data){
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(data));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int weekid=c.get(Calendar.DAY_OF_WEEK)-1;
        if (weekid == 0) {
            weekid=7;
        }else{}
        Week += "星期"+weekid;
        Log.i("LXH", data+"是"+Week);
        return Week;
    }
	judge_week("2021-11-17");
	I/LXH: 2021-11-17是星期3

欢迎指错,一起学习

你可能感兴趣的:(Android,android,studio,android,android-studio)