Android實時顯示系統時間日期以及星期幾
最近的專案要實現實時顯示系統時間日期,所以就進行了一些簡單的封裝儲存:
1、通過SimpleDateFormat進行時間日期格式
/**
* Created by admin on 2017/12/19.
* 時間日期轉換工具類
*/
public class DateTimeUtil {
private static DateTimeUtil instance;
private DateTimeUtil(){
}
public static DateTimeUtil getInstance(){
if (instance == null){
instance = new DateTimeUtil();
}
return instance;
}
//獲取系統當前日期時間
public String getCurrentDateTime() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
String curDate = formatter.format(date);
return curDate;
}
//獲取系統當前日期
public String getCurrentDate() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
Date date = new Date(System.currentTimeMillis());
String curDate = formatter.format(date);
return curDate;
}
//獲取系統當前日期(英文格式)
public String getCurrentDateEnglish() {
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy", Locale.ENGLISH);
Date date = new Date(System.currentTimeMillis());
String curDate = formatter.format(date);
return curDate;
}
//獲取系統當前時間
public String getCurrentTime() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date date = new Date(System.currentTimeMillis());
String curDate = formatter.format(date);
return curDate;
}
//獲取系統當前時間
public String getCurrentTimeHHMM() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
Date date = new Date(System.currentTimeMillis());
String curDate = formatter.format(date);
return curDate;
}
//獲取系統當前是星期幾
public String getCurrentWeekDay(int type) {
String week = "";
Calendar c1 = Calendar.getInstance();
int day = c1.get(Calendar.DAY_OF_WEEK);
if (type == 2){
switch (day){
case 1:
week = "Sunday";
break;
case 2:
week = "Monday";
break;
case 3:
week = "Tuesdays";
break;
case 4:
week = "Wednesday";
break;
case 5:
week = "Thursday";
break;
case 6:
week = "Fridays";
break;
case 7:
week = "Saturday";
break;
}
}else {
switch (day){
case 1:
week = "星期日";
break;
case 2:
week = "星期一";
break;
case 3:
week = "星期二";
break;
case 4:
week = "星期三";
break;
case 5:
week = "星期四";
break;
case 6:
week = "星期五";
break;
case 7:
week = "星期六";
break;
}
}
return week;
}
}
2、建立一個介面用於會調,讓時間實時更新
/**
* Created by admin on 2017/12/19.
* 更新時間介面
*/
public interface OnGetCurrentDateTimeListener {
void onGetDateTime();
}
3、使用執行緒更行時間
/**
* Created by admin on 2017/12/19.
* 實時更新時間子執行緒
*/
public class TimeThreadUtil extends Thread {
private static final int CURRENTDATETIME = 1;
private static TimeThreadUtil timeThreadUtil;
private OnGetCurrentDateTimeListener listener;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case CURRENTDATETIME:
if (listener !=null){
listener.onGetDateTime();
}
break;
}
}
};
public TimeThreadUtil(OnGetCurrentDateTimeListener listener) {
this.listener = listener;
}
@Override
public void run() {
super.run();
do {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = CURRENTDATETIME;
mHandler.sendMessage(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
} while (true);
}
}
4、顯示時間日期以及星期幾
佈局如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context="com.example.admin.currentdatetime.MainActivity">
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30sp"
/>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30sp"
/>
<TextView
android:id="@+id/week"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30sp"
/>
</LinearLayout>
顯示的MainActivity
public class MainActivity extends AppCompatActivity implements OnGetCurrentDateTimeListener{
private DateTimeUtil dateTimeUtil;
private TextView date;
private TextView time;
private TextView week;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date = (TextView) findViewById(R.id.date);
time = (TextView)findViewById(R.id.time);
week = (TextView)findViewById(R.id.week);
dateTimeUtil = DateTimeUtil.getInstance();
new TimeThreadUtil(this).start();
}
@Override
public void onGetDateTime() {
time.setText(dateTimeUtil.getCurrentTime());//顯示時間
date.setText(dateTimeUtil.getCurrentDate());//顯示年月日
week.setText(dateTimeUtil.getCurrentWeekDay(0));//顯示星期幾
}
}