最近的项目要实现实时显示系统时间日期,所以就进行了一些简单的封装保存:
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));//显示星期几
}
}