要求:开发一个手机端上报体温的手机APP,上报内容包括姓名、日期(自动生成)、时间(自动生成)和体温。
自动生成日期和时间
利用了Calendar来获取系统时间。首先用Calendar.getInstance()函数获取一个实例,再使用Calendar.get()函数来获取时间的具体信息,如年、月、日、小时、分、秒等。需要注意的是获取hour时可以利用Calendar.AM_PM来判断上下午。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_date = (TextView) findViewById(R.id.textView1); tv_time = (TextView) findViewById(R.id.textView2); cal = Calendar.getInstance(); year = String.valueOf(cal.get(Calendar.YEAR)); month = String.valueOf(cal.get(Calendar.MONTH)) + 1; day = String.valueOf(cal.get(Calendar.DATE)); if (cal.get(Calendar.AM_PM) == 0) { if (cal.get(Calendar.HOUR) < 10) { hour = "0" + cal.get(Calendar.HOUR); } else { hour = String.valueOf(cal.get(Calendar.HOUR)); } } else { hour = String.valueOf(cal.get(Calendar.HOUR) + 12); } if (cal.get(Calendar.MINUTE) < 10) { minute = "0" + cal.get(Calendar.MINUTE); } else { minute = String.valueOf(cal.get(Calendar.MINUTE)); } if (cal.get(Calendar.SECOND) < 10) { second = "0" + cal.get(Calendar.SECOND); } else { second = String.valueOf(cal.get(Calendar.SECOND)); } my_time_1 = year + "-" + month + "-" + day; my_time_2 = hour + ":" + minute + ":" + second; tv_date.setText(my_time_1); tv_time.setText(my_time_2); }