>  기사  >  Java  >  Android는 현재 시스템 날짜와 시간을 가져옵니다.

Android는 현재 시스템 날짜와 시간을 가져옵니다.

little bottle
little bottle앞으로
2019-04-09 14:24:258978검색


이 기사에서는 Android에서 시간 획득 기능을 구현하는 세 가지 방법을 사용합니다. 마음에 들면 저장하고 사용해 보세요!

첫 번째 방법

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
//获取当前时间
Date date = new Date(System.currentTimeMillis());
time1.setText("Date获取当前日期时间"+simpleDateFormat.format(date));

두 번째 방법

Calendar calendar = Calendar.getInstance();
//获取系统的日期
//年
int year = calendar.get(Calendar.YEAR);
//月
int month = calendar.get(Calendar.MONTH)+1;
//日
int day = calendar.get(Calendar.DAY_OF_MONTH);
//获取系统时间
//小时
int hour = calendar.get(Calendar.HOUR_OF_DAY);
//分钟
int minute = calendar.get(Calendar.MINUTE);
//秒
int second = calendar.get(Calendar.SECOND);
 
time2.setText("Calendar获取当前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);

세 번째 방법

Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
t.setToNow(); // 取得系统时间。
int year = t.year;
int month = t.month+1;
int day = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;
time3.setText("Calendar获取当前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);

주의하면 1이 나올 수도 있습니다. 달에 추가됨 . 0부터 계산이 시작되기 때문에 사용시에는 1을 더해줍니다. Android를 처음 사용하시는 분이라면 이해가 안 되실 수도 있습니다. Time1, time2, time3은 사용하기 전에 꼭 초기화해두시기 바랍니다. 아니면 당신을 위해 게시할까요?

TextView time1 = (TextView) findViewById(R.id.tv_time1);
TextView time2 = (TextView) findViewById(R.id.tv_time2);
TextView time3 = (TextView) findViewById(R.id.tv_time3);

마지막으로 시뮬레이터에서 실행했는데 세 가지 방법이 모두 사용 가능합니다.

【추천 강좌:

Android 동영상 튜토리얼

위 내용은 Android는 현재 시스템 날짜와 시간을 가져옵니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 csdn.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제