Android用Time和Calendar获取系统当前时间源码分享(年月日时分秒周几)

作者:袖梨 2022-11-14

概述

用Time和Calendar获取系统当前时间(年月日时分秒周几)

效果图

源码:

代码如下复制代码

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.text.format.Time;

importandroid.view.View;

importandroid.widget.RelativeLayout;

importandroid.widget.TextView;

importjava.util.Calendar;

importbutterknife.BindView;

importbutterknife.ButterKnife;

importbutterknife.OnClick;

publicclassMainActivityextendsActivity {

@BindView(R.id.tv_time)

TextView tvTime;

@BindView(R.id.activity_main)

RelativeLayout activityMain;

@BindView(R.id.tv_cal)

TextView tvCal;

Time time;

@Override

protectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ButterKnife.bind(this);

tvTime.setText("Time类");

tvCal.setText("Calender类");

initTime();

}

privatevoidinitTime() {

time =newTime();

time.setToNow();

}

@OnClick({R.id.tv_cal,R.id.tv_time})

publicvoidonClick(View view) {

switch(view.getId()) {

caseR.id.tv_time://点击第一个

//月份是从0-11算的,所以显示的话要+1

String times = time.year +"年"+ time.month+1+"月"+ time.monthDay

+"日"+ time.hour +"时"+ time.minute +"分"+ time.second +"秒"

+":现在是一年中的第"+ time.yearDay +"天";

tvTime.setText(times);

break;

caseR.id.tv_cal:

Calendar cal=Calendar.getInstance();

String time_cal=""+cal.get(Calendar.YEAR)+"-"+cal.get(Calendar.MONTH)+1+"-"+cal.get(Calendar.DATE)+" "

+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);

tvCal.setText(time_cal);

break;

}

}

@Override

protectedvoidonDestroy() {

super.onDestroy();

// Unbinder unbinder=ButterKnife.bind(this);

// unbinder.unbind();

ButterKnife.bind(this).unbind();

}

}

布局就略了。。

相关文章

精彩推荐