java判断今天,昨天,前天,不能用秒间隔的简单实例
实例如下:
一jsonoptString解析的TimesTampstring 二longdateSec=(long)(Double.valueOf(timestamp)*1000); 三Datedate=newDate(dateSec); 四getTimestampString(date); publicstaticStringgetTimestampString(DateparamDate){ Stringstr=null; longl=paramDate.getTime(); CalendarlocalCalendar=GregorianCalendar.getInstance(); localCalendar.setTime(paramDate); intyear=localCalendar.get(Calendar.YEAR); if(!isSameYear(year)){//去年,直接返回 StringparamDate2str=newSimpleDateFormat("yyyyMMdd",Locale.CHINA).format(paramDate); returnparamDate2str; } if(isSameDay(l)){ inti=localCalendar.get(Calendar.HOUR_OF_DAY); if(i>17){ str="晚上HH:mm";//HH表示24小时,hh表示12小时进制, }elseif((i>=0)&&(i<=6)){ str="凌晨HH:mm"; }elseif((i>11)&&(i<=17)){ str="下午HH:mm"; }else{ str="上午HH:mm"; } }elseif(isYesterday(l)){ str="昨天HH:mm"; }elseif(isBeforeYesterday(l)){ str="前天HH:mm"; }else{ str="M月d日HH:mm"; } StringparamDate2str=newSimpleDateFormat(str,Locale.CHINA).format(paramDate); returnparamDate2str; } 。。。 //获取今天开始结束时间 publicstaticTimeInfogetTodayStartAndEndTime(){ CalendarlocalCalendar1=Calendar.getInstance(); localCalendar1.set(Calendar.HOUR_OF_DAY,0); localCalendar1.set(Calendar.MINUTE,0); localCalendar1.set(Calendar.SECOND,0); localCalendar1.set(Calendar.MILLISECOND,0); DatelocalDate1=localCalendar1.getTime(); longl1=localDate1.getTime(); CalendarlocalCalendar2=Calendar.getInstance(); localCalendar2.set(Calendar.HOUR_OF_DAY,23); localCalendar2.set(Calendar.MINUTE,59); localCalendar2.set(Calendar.SECOND,59); localCalendar2.set(Calendar.MILLISECOND,999); DatelocalDate2=localCalendar2.getTime(); longl2=localDate2.getTime(); TimeInfolocalTimeInfo=newTimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); returnlocalTimeInfo; } //获取昨天开始结束时间 publicstaticTimeInfogetYesterdayStartAndEndTime(){ CalendarlocalCalendar1=Calendar.getInstance(); localCalendar1.add(Calendar.DAY_OF_MONTH,-1);//5 localCalendar1.set(Calendar.HOUR_OF_DAY,0);//11 localCalendar1.set(Calendar.MINUTE,0);//12 localCalendar1.set(Calendar.SECOND,0);//13 localCalendar1.set(Calendar.MILLISECOND,0);//Calendar.MILLISECOND DatelocalDate1=localCalendar1.getTime(); longl1=localDate1.getTime(); CalendarlocalCalendar2=Calendar.getInstance(); localCalendar2.add(Calendar.DAY_OF_MONTH,-1);//5 localCalendar2.set(Calendar.HOUR_OF_DAY,23);//11 localCalendar2.set(Calendar.MINUTE,59);//12 localCalendar2.set(Calendar.SECOND,59);//13 localCalendar2.set(Calendar.MILLISECOND,999);//Calendar.MILLISECOND DatelocalDate2=localCalendar2.getTime(); longl2=localDate2.getTime(); TimeInfolocalTimeInfo=newTimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); returnlocalTimeInfo; } //获取前天开始结束时间 publicstaticTimeInfogetBeforeYesterdayStartAndEndTime(){ CalendarlocalCalendar1=Calendar.getInstance(); localCalendar1.add(Calendar.DAY_OF_MONTH,-2); localCalendar1.set(Calendar.HOUR_OF_DAY,0); localCalendar1.set(Calendar.MINUTE,0); localCalendar1.set(Calendar.SECOND,0); localCalendar1.set(Calendar.MILLISECOND,0); DatelocalDate1=localCalendar1.getTime(); longl1=localDate1.getTime(); CalendarlocalCalendar2=Calendar.getInstance(); localCalendar2.add(Calendar.DAY_OF_MONTH,-2); localCalendar2.set(Calendar.HOUR_OF_DAY,23); localCalendar2.set(Calendar.MINUTE,59); localCalendar2.set(Calendar.SECOND,59); localCalendar2.set(Calendar.MILLISECOND,999); DatelocalDate2=localCalendar2.getTime(); longl2=localDate2.getTime(); TimeInfolocalTimeInfo=newTimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); returnlocalTimeInfo; } privatestaticbooleanisSameDay(longparamLong){ TimeInfolocalTimeInfo=getTodayStartAndEndTime(); return(paramLong>localTimeInfo.getStartTime())&&(paramLonglocalTimeInfo.getStartTime())&&(paramLong localTimeInfo.getStartTime())&&(paramLong 以上这篇java判断今天,昨天,前天,不能用秒间隔的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。