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

概述

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

效果图

源码:

import android.app.Activity; 
import android.os.Bundle; 
import android.text.format.Time; 
import android.view.View; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import java.util.Calendar; 
import butterknife.BindView; 
import butterknife.ButterKnife; 
import butterknife.OnClick; 
public class MainActivity extends Activity { 
 @BindView(R.id.tv_time) 
 TextView tvTime; 
 @BindView(R.id.activity_main) 
 RelativeLayout activityMain; 
 @BindView(R.id.tv_cal) 
 TextView tvCal; 
 Time time; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  ButterKnife.bind(this); 
  tvTime.setText("Time类"); 
  tvCal.setText("Calender类"); 
  initTime(); 
 } 
 private void initTime() { 
  time = new Time(); 
  time.setToNow(); 
 } 
 @OnClick({R.id.tv_cal,R.id.tv_time}) 
 public void onClick(View view) { 
  switch (view.getId()) { 
   case R.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; 
   case R.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 
 protected void onDestroy() { 
  super.onDestroy(); 
//  Unbinder unbinder=ButterKnife.bind(this); 
//  unbinder.unbind(); 
  ButterKnife.bind(this).unbind(); 
 } 
} 

布局就略了。。

这里获取布局id和点击事件用了(ButterKnife),可以参考:ButterKnife详解

以上所述是小编给大家介绍的Android 用Time和Calendar获取系统当前时间源码分享(年月日时分秒周几),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

以上是来客网为你收集整理的Android 用Time和Calendar获取系统当前时间源码分享(年月日时分秒周几)全部内容,希望文章能够帮你解决Android 用Time和Calendar获取系统当前时间源码分享(年月日时分秒周几)所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。