安卓原生時間選擇器

調用Android原生日期選擇器對話框也就是DatePickerDialogjava

package com.example.myapplication;


import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv;
    private Button but;
    private int mYear;
    private int mMonth;
    private int mDay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //Activity的onCreate方法中獲取當時的年 ,月,日
        Calendar calendar = Calendar.getInstance();
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
    }
    private void initView() {
        tv = (TextView) findViewById(R.id.tv);
        but = (Button) findViewById(R.id.but);

        but.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but:
                //TODO 調用時間選擇器
                new DatePickerDialog(MainActivity.this, onDateSetListener,mYear,mMonth,mDay).show();
                break;
        }
    }
    /**
     * 日期選擇器對話框監聽
     */
    private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            String days;
            if (mMonth + 1 < 10) {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("年").append("0").
                            append(mMonth + 1).append("月").append("0").append(mDay).append("日").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("年").append("0").
                            append(mMonth + 1).append("月").append(mDay).append("日").toString();
                }
            } else {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("年").
                            append(mMonth + 1).append("月").append("0").append(mDay).append("日").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("年").
                            append(mMonth + 1).append("月").append(mDay).append("日").toString();
                }
            }
            tv.setText(days);
        }
    };
}

獲取當前的年月時分:android

SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd    hh:mm:ss");
String date = sDateFormat.format(new java.util.Date());

若是想獲取當前的年月,則能夠這樣寫(只獲取時間或秒種同樣):app

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
String date=sdf.format(new java.util.Date());

固然還有就是能夠指定時區的時間(待):ide

df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL, Locale.CHINA);
System.out.println(df.format(new Date()));

計算時差

DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try
        {
            Date d1 = df.parse("2006-05-26 12:00:00");
            Date d2 = df.parse("2006-07-02 11:20:00");
            //Date   d2 = new   Date(System.currentTimeMillis());//你也能夠獲取當前時間
            long diff = d1.getTime() - d2.getTime();//這樣獲得的差值是微秒級別
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);

//            System.out.println(""+days+"天"+hours+"小時"+minutes+"分");
            Toast.makeText(this, days+"天"+hours+"小時"+minutes+"分", Toast.LENGTH_SHORT).show();
        }
        catch (Exception e)
        {
        }
相關文章
相關標籤/搜索