Android 는 Time 과 Calendar 로 시스템 의 현재 시간 원본 을 가 져 와 공유 합 니 다(년 월 일 시 분 초 몇 주)

2703 단어 android목전시간.
개술
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)를 참고 할 수 있 습 니 다버터 나이프 상세 설명
위 에서 말 한 것 은 소 편 이 소개 한 안 드 로 이 드 용 Time 과 Calendar 가 져 오기 시스템 의 현재 시간 소스 코드 공유(년 월 일 시간 분 초 몇 주)입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다.여기 서도 저희 사이트 에 대한 여러분 의 지지 에 감 사 드 립 니 다!

좋은 웹페이지 즐겨찾기