Android学习之获取系统时间

查看了大部分的代码,大概有3种,这里就简单介绍。我使用的方法。

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(System.currentTimeMillis());
System.out.println("输出当前时间");
System.out.println(simpleDateFormat.format(date));

在这里插入图片描述

public class OtherActivity extends AppCompatActivity implements View.OnClickListener, TimePicker.OnTimeChangedListener{
    EditText et_time;
    private Calendar cal;
    private int y, m, d,h,f;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        et_time = (EditText) findViewById(R.id.et_time);

        et_time.setKeyListener(null);
        et_time.setOnClickListener(this);

        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        System.out.println("输出当前时间");
        System.out.println(simpleDateFormat.format(date));
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        switch (v.getId()){
            case R.id.et_time:
                DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        et_time.setText(year + "-" + (++month) + "-" + dayOfMonth);
                    }
                };
                getDate();
                DatePickerDialog datePickerDialog = new DatePickerDialog(OtherActivity.this, listener, y, m, d);
                datePickerDialog.show();
                break;
        }

    }

    private void getDate() {
        cal = Calendar.getInstance();
        y = cal.get(Calendar.YEAR);
        m = cal.get(Calendar.MONTH);
        d = cal.get(Calendar.DAY_OF_MONTH);
    }

    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

    }
}

Android学习之获取系统时间_第1张图片
Android学习之获取系统时间_第2张图片

你可能感兴趣的:(wuluo,Android)