最新下载
热门教程
- 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; } } |
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21