在屏幕上显示日期时间星期的方法

Posted bbqopdd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在屏幕上显示日期时间星期的方法相关的知识,希望对你有一定的参考价值。

一个获取当前时间的类:

  1 import android.annotation.SuppressLint;
  2 import android.os.Handler;
  3 import android.os.Message;
  4 import android.util.Log;
  5 import android.widget.TextView;
  6 
  7 import com.acmeinte.idl.sample.common.Constants;
  8 import com.acmeinte.idl.sample.common.EventMsg;
  9 import com.acmeinte.idl.sample.manager.DBFileManager;
 10 import com.acmeinte.idl.sample.manager.DataMessage;
 11 
 12 import org.greenrobot.eventbus.EventBus;
 13 
 14 import java.text.SimpleDateFormat;
 15 import java.util.Calendar;
 16 import java.util.Date;
 17 import java.util.TimeZone;
 18 
 19 //***
 20 //更新时间类
 21 //***
 22 public class ThreadTime extends Thread {
 23     public TextView tvDate;
 24     private int msgKey1 = 22;
 25 
 26     public ThreadTime(TextView tvDate) {
 27         this.tvDate = tvDate;
 28     }
 29 
 30     @Override
 31     public void run() {
 32         do {
 33             try {
 34                 Thread.sleep(1000);
 35                 Message msg = new Message();
 36                 msg.what = msgKey1;
 37                 mHandler.sendMessage(msg);
 38             } catch (InterruptedException e) {
 39                 e.printStackTrace();
 40             }
 41         } while (true);
 42     }
 43 
 44 
 45     @SuppressLint("HandlerLeak")
 46     private Handler mHandler = new Handler() {
 47         @SuppressLint("SetTextI18n")
 48         @Override
 49         public void handleMessage(Message msg) {
 50             super.handleMessage(msg);
 51             switch (msg.what) {
 52                 case 22:
 53                     SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
 54                     String date = sdf.format(new Date());
 55                     tvDate.setText(deviceTime() + date + " " + getWeek());
 56                     65                     break;
 66                 default:
 67                     break;
 68             }
 69         }
 70     };
 71 
 72     /**
 73      * 获取今天星期几
 74      *
 75      * @return
 76      */
 77     public static String deviceTime() {
 78         Calendar calendar = Calendar.getInstance();
 79         calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
 80         String year;
 81         String month;
 82         String day;
 83         String deviceTime;
 84 
 85         year = String.valueOf(calendar.get(Calendar.YEAR));
 86         month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
 87         day = String.valueOf(calendar.get(Calendar.DATE));
 88         deviceTime = year + "-" + month + "-" + day + " ";
 89 
 90         return deviceTime;
 91     }
 92 
 93     /**
 94      * 获取今天星期几
 95      *
 96      * @return
 97      */
 98     public static String getWeek() {
 99         Calendar calendar = Calendar.getInstance();
100         int i = calendar.get(Calendar.DAY_OF_WEEK);
101         switch (i) {
102             case 1:
103                 return "周日";
104             case 2:
105                 return "周一";
106             case 3:
107                 return "周二";
108             case 4:
109                 return "周三";
110             case 5:
111                 return "周四";
112             case 6:
113                 return "周五";
114             case 7:
115                 return "周六";
116             default:
117                 return "";
118         }
119     }
120 
121  131 }

然后再UI界面修改txt参数实现时间显示:

 

1 //时间
2 
3         TextView time_device ;
4         time_device = findViewById(R.id.txt_time_device);
5         ThreadTime threadTime = new ThreadTime(time_device);
6         threadTime.start();

 

以上是关于在屏幕上显示日期时间星期的方法的主要内容,如果未能解决你的问题,请参考以下文章

用js在网页上显示当前日期和时间,并显示是星期几

求js详细时间代码,年月日星期时分(秒),阴历日期

js显示当前日期时间和星期几

atitit.日期,星期,时候的显示方法ISO 8601标准

想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉

片段布局不覆盖整个屏幕