调用Android本机日期选择器对话框,即DatePickerDialog
package com.example.my application;
import Android.app.date picker dialog;
import android.os.Bundle;
import Android.support.V7.app.appcompatactivity;
import android.view.View;
import android.widget.Button;
import Android.widget.date picker;
import android.widget.TextView;
import java.util.Calendar;
publicclassmainactivityextendsappcompatactivityimplementsview.onclick listener {
私有文本电视;
私有button but;
私有int my ear;
private int mMonth;
private int mDay;
@Override
保护性视频创建(bundlesavedinstancestate )。
super.oncreate (savedinstancestate;
setcontentview (r.layout.activity _ main;
initView (;
//通过//Activity的onCreate方法获取当时的年、月、日
calendar calendar=calendar.getinstance (;
my ear=calendar.get (calendar.year;
m month=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
公共void onclick (viewv ) {
sitch(v.getid ) ) ) )。
case R.id.but:
//TODO调用时间选择器
newdatepickerdialog (main activity.this,onDateSetListener,mYear,mMonth,mDay ).show );
黑;
}
}
//*
*监听日期选择器对话框
*/
privatedatepickerdialog.ondatesetlistenerondatesetlistener=newdatepickerdialog.ondatesetlistener {
@Override
publicvoidondateset (date picker view,int year,int monthOfYear,int dayOfMonth ) {
我的年=年;
mMonth=monthOfYear;
mDay=dayOfMonth;
字符串日期;
if(mmonth110 ) {
if(mday10 ) {
days=new StringBuffer ().append (my ear ).append ('年').append () '0' )。
append(mmonth1).append )、月)、append )、0 )、append )、mday )、append )、日)、toString );
} else {
days=new StringBuffer ().append (my ear ).append ('年').append () '0' )。
append(mmonth1).append (月).append (mday ).append ) )天).toString );
}
} else {
if(mday10 ) {
days=new StringBuffer ().append (my ear ).append ) '年')。
append(mmonth1).append )、月)、append )、0 )、append )、mday )、append )、日)、toString );
} else {
days=new StringBuffer ().append (my ear ).append ) '年')。
append(mmonth1).append (月).append (mday ).append ) )天).toString );
}
}
TV.settext(days;
}
(;
}
获取当前年月:
simpledateformatsdateformat=newsimpledateformat (yyyy-mm-ddhh : mm : ss );
string date=sdate format.format (new Java.util.date () );
如果想取得现在的年月,可以这样写: (
simpledateformatsdf=newsimpledateformat (yyyy-mm );
string date=SDF.format (new Java.util.date ) );
当然,也有可以指定时区的时间(: )
df=date format.getdatetimeinstance (dateformat.full、date format.full、Locale.CHINA );
system.out.println(df.format ) newdate () );
计算时差
date format df=newsimpledateformat (yyyy-mm-ddhh : mm : ss );
托里
{
date D1=df.parse (2006-05-261233600336000 );
date D2=df.parse (2006-07-0211336020336000 );
//date D2=new date (system.current time millis () ); //你也能取得现在的时间
long diff=D1.gettime (-D2.gettime ); //这样得到的差是微秒级
longdays=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 )
{
}