Android处理时间各种方法汇总
本文实例为大家分享了Android处理时间的各种方法,供大家参考,具体内容如下
/**
*获取当前时间
*
*@return当前时间
*/
publicstaticStringgetdate(){
SimpleDateFormatformatter=newSimpleDateFormat("HH:mm");
DatecurDate=newDate(System.currentTimeMillis());//获取当前时间
Stringtime=formatter.format(curDate);
returntime;
}
/**
*获取当前秒数
*@return
*/
publicstaticStringgetSecond(){
Calendarcalendar=Calendar.getInstance();
intsec=calendar.get(Calendar.SECOND);
Stringvalue=String.valueOf(sec<10?"0"+sec:sec);
returnvalue;
}
/**
*获取当天日期
*
*@return当前时间
*/
publicstaticStringgetriqi(){
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-dd");
DatecurDate=newDate(System.currentTimeMillis());//获取当天日期
Stringtime=formatter.format(curDate);
returntime;
}
/**
*获取当前全部时间
*
*@return当前时间
*/
publicstaticStringgetalldate(){
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
DatecurDate=newDate(System.currentTimeMillis());//获取当前时间
Stringtime=formatter.format(curDate);
returntime;
}
publicstaticDategetDate(Stringtime){
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
Datedate=null;
try{
date=formatter.parse(time);
}catch(ParseExceptione){
e.printStackTrace();
}
returndate;
}
/**
*天数减一
*/
publicstaticStringYesterday(intdata){
SimpleDateFormatdft=newSimpleDateFormat("yyyy-MM-dd");
DatebeginDate=newDate();
Calendardate=Calendar.getInstance();
date.setTime(beginDate);
date.set(Calendar.DATE,date.get(Calendar.DATE)-data);
//DateendDate=dft.parse(dft.format(date.getTime()));
Stringtime=dft.format(date.getTime());
returntime;
}
/**
*获取星期
*
*@paramdate日期
*****-**-**
*@return
*/
publicstaticintgetDay(Datedate){
Calendarcal=Calendar.getInstance();
cal.setTime(date);
returncal.get(Calendar.DAY_OF_WEEK);
}
/**
*时间顺序判断
*@paramdata_ruke
*@paramdata_choice
*@return
*/
publicstaticbooleanconpare_data(Stringdata1,Stringdata2){
if(data1==null||"".equals(data1)){
returntrue;
}
DateFormatdFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
try{
Datedt1=dFormat.parse(data1);
Datedt2=dFormat.parse(data2);
if(dt1.getTime()>dt2.getTime()){
Log.i("time_compare","dt1在dt2之后");
returnfalse;
}elseif(dt1.getTime()dt2.getTime()){
if(dt1.getTime()-dt2.getTime()==86400000){
return1;
}elseif(dt1.getTime()-dt2.getTime()==(86400000*2)){
return2;
}elseif(dt1.getTime()-dt2.getTime()==(86400000*3)){
return3;
}else{
return0;
}
}else{
return0;
}
}catch(Exceptionexception){
exception.printStackTrace();
return0;
}
}
/**
*返回两个string类型日期之间相差的天数
*/
publicstaticintdaysBetween(Stringsmdate,Stringbdate){
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd");
Calendarcal=Calendar.getInstance();
longtime1=0;
longtime2=0;
try{
cal.setTime(sdf.parse(smdate));
time1=cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
time2=cal.getTimeInMillis();
}catch(Exceptione){
e.printStackTrace();
}
longbetween_days=(time2-time1)/(1000*3600*24);
returnInteger.parseInt(String.valueOf(between_days));
}
/**
*返回两个string类型日期相差的小时数
*/
publicstaticintdaysBetween2(StringstartTime,StringendTime){
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH");
Calendarcal=Calendar.getInstance();
longtime1=0;
longtime2=0;
try{
cal.setTime(sdf.parse(startTime));
time1=cal.getTimeInMillis();
cal.setTime(sdf.parse(endTime));
time2=cal.getTimeInMillis();
}catch(Exceptione){
e.printStackTrace();
}
longbetween_days=(time2-time1)/(1000*3600);
returnInteger.parseInt(String.valueOf(between_days));
}
/**
*计算两段日期的重合日期
*/
/**
*计算两段日期的重合日期
*@paramstr1开始日期1
*@paramstr2结束日期1
*@paramstr3开始日期2
*@paramstr4结束日期2
*@return
*@throwsException
*/
publicstaticMapcomparisonRQ(Stringstr1,Stringstr2,Stringstr3,
Stringstr4)throwsException{
Stringmesg="";
DateFormatdf=newSimpleDateFormat("yyyy-MM-dd");
Stringstartdate="";
Stringenddate="";
try{
Datedt1=df.parse(str1);
Datedt2=df.parse(str2);
Datedt3=df.parse(str3);
Datedt4=df.parse(str4);
if(dt1.getTime()<=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()){
mesg="f";//重合
startdate=str3;
enddate=str2;
}
if(dt1.getTime()>=dt3.getTime()&&dt3.getTime()<=dt2.getTime()&&dt2.getTime()<=dt4.getTime()){
mesg="f";//重合
startdate=str1;
enddate=str2;
}
if(dt3.getTime()<=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()){
mesg="f";//重合
startdate=str1;
enddate=str4;
}
if(dt3.getTime()>=dt1.getTime()&&dt1.getTime()<=dt4.getTime()&&dt4.getTime()<=dt2.getTime()){
mesg="f";//重合
startdate=str3;
enddate=str4;
}
System.out.println(startdate+"----"+enddate);
}catch(ParseExceptione){
e.printStackTrace();
thrownewParseException(e.getMessage(),0);
}catch(Exceptione){
e.printStackTrace();
thrownewException(e);
}
Mapmap=newHashMap();
map.put("startdate",startdate);
map.put("enddate",enddate);
returnmap;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。