最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java判断今天,昨天,前天,不能用秒间隔的简单实例
时间:2022-06-29 01:27:58 编辑:袖梨 来源:一聚教程网
一 json optString 解析的TimesTamp string
二longdateSec = (long) (Double.valueOf(timestamp) *1000);
三 Date date=newDate(dateSec);
四 getTimestampString(date);
代码如下 | 复制代码 |
publicstaticString getTimestampString(Date paramDate) { String str =null; longl = paramDate.getTime(); Calendar localCalendar = GregorianCalendar.getInstance(); localCalendar.setTime(paramDate); intyear = localCalendar.get(Calendar.YEAR); if(! isSameYear(year)){//去年,直接返回 String paramDate2str =newSimpleDateFormat("yyyy MM dd", 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"; } String paramDate2str =newSimpleDateFormat(str, Locale.CHINA).format(paramDate); returnparamDate2str; }
。。。
// 获取 今天开始结束 时间
publicstaticTimeInfo getTodayStartAndEndTime() {
Calendar localCalendar1 = Calendar.getInstance(); localCalendar1.set(Calendar.HOUR_OF_DAY,0); localCalendar1.set(Calendar.MINUTE,0); localCalendar1.set(Calendar.SECOND,0); localCalendar1.set(Calendar.MILLISECOND,0); Date localDate1 = localCalendar1.getTime();
longl1 = localDate1.getTime();
Calendar localCalendar2 = Calendar.getInstance(); localCalendar2.set(Calendar.HOUR_OF_DAY,23); localCalendar2.set(Calendar.MINUTE,59); localCalendar2.set(Calendar.SECOND,59); localCalendar2.set(Calendar.MILLISECOND,999); Date localDate2 = localCalendar2.getTime();
longl2 = localDate2.getTime();
TimeInfo localTimeInfo =newTimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); returnlocalTimeInfo;
}
// 获取 昨天开始结束 时间
publicstaticTimeInfo getYesterdayStartAndEndTime() { Calendar localCalendar1 = 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 Date localDate1 = localCalendar1.getTime(); longl1 = localDate1.getTime();
Calendar localCalendar2 = 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 Date localDate2 = localCalendar2.getTime(); longl2 = localDate2.getTime();
TimeInfo localTimeInfo =newTimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); returnlocalTimeInfo; }
// 获取 前天开始结束 时间
publicstaticTimeInfo getBeforeYesterdayStartAndEndTime() { Calendar localCalendar1 = 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); Date localDate1 = localCalendar1.getTime(); longl1 = localDate1.getTime();
Calendar localCalendar2 = 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); Date localDate2 = localCalendar2.getTime(); longl2 = localDate2.getTime(); TimeInfo localTimeInfo =newTimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); returnlocalTimeInfo; }
privatestaticbooleanisSameDay(longparamLong) { TimeInfo localTimeInfo = getTodayStartAndEndTime(); return(paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime()); }
privatestaticbooleanisYesterday(longparamLong) { TimeInfo localTimeInfo = getYesterdayStartAndEndTime(); return(paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime()); }
privatestaticbooleanisBeforeYesterday(longparamLong) { TimeInfo localTimeInfo = getBeforeYesterdayStartAndEndTime(); return(paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime()); }
publicstaticbooleanisSameYear(intyear) { Calendar cal = Calendar.getInstance(); intCurYear = cal.get(Calendar.YEAR); // Log.e("","CurYear="+CurYear);//2015 returnCurYear==year; }
。。。。。。
publicclassTimeInfo { privatelongstartTime; privatelongendTime;
publiclonggetStartTime() { returnthis.startTime; }
publicvoidsetStartTime(longparamLong) { this.startTime = paramLong; }
publiclonggetEndTime() { returnthis.endTime; }
publicvoidsetEndTime(longparamLong) { this.endTime = paramLong; } } |
相关文章
- 《燕云十六声》红尘无眼完成图文攻略 12-25
- 《燕云十六声》阴阳如影完成图文攻略 12-25
- 《燕云十六声》悬檐之下四架椽屋图文攻略 12-25
- 《燕云十六声》2024最新公测时间介绍 12-25
- 《燕云十六声》有没有藏宝阁 12-25
- 《燕云十六声》制作公司介绍 12-25