創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!
創(chuàng)新互聯(lián)是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,專業(yè)領(lǐng)域包括網(wǎng)站建設(shè)、網(wǎng)站制作、電商網(wǎng)站制作開發(fā)、微信小程序開發(fā)、微信營銷、系統(tǒng)平臺開發(fā),與其他網(wǎng)站設(shè)計(jì)及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗(yàn)和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!本篇文章給大家分享的是有關(guān)利用java如何實(shí)現(xiàn)判斷日期,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
實(shí)例如下:
一 json optString 解析的TimesTamp string 二 long dateSec = (long) (Double.valueOf(timestamp) * 1000); 三 Date date=new Date(dateSec); 四 getTimestampString(date); public static String getTimestampString(Date paramDate) { String str = null; long l = paramDate.getTime(); Calendar localCalendar = GregorianCalendar.getInstance(); localCalendar.setTime(paramDate); int year = localCalendar.get(Calendar.YEAR); if(! isSameYear(year)){ //去年,直接返回 String paramDate2str = new SimpleDateFormat("yyyy MM dd", Locale.CHINA).format(paramDate); return paramDate2str; } if (isSameDay(l)) { int i = localCalendar.get(Calendar.HOUR_OF_DAY); if (i > 17) { str = "晚上 HH:mm";//HH表示24小時,hh表示12小時進(jìn)制, } else if ((i >= 0) && (i <= 6)) { str = "凌晨 HH:mm"; } else if ((i > 11) && (i <= 17)) { str = "下午 HH:mm"; } else { str = "上午 HH:mm"; } } else if (isYesterday(l)) { str = "昨天 HH:mm"; } else if (isBeforeYesterday(l)) { str = "前天 HH:mm"; } else { str = "M月d日 HH:mm"; } String paramDate2str = new SimpleDateFormat(str, Locale.CHINA).format(paramDate); return paramDate2str; } 。。。 // 獲取 今天開始結(jié)束 時間 public static TimeInfo 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(); long l1 = 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(); long l2 = localDate2.getTime(); TimeInfo localTimeInfo = new TimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); return localTimeInfo; } // 獲取 昨天開始結(jié)束 時間 public static TimeInfo 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(); long l1 = 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(); long l2 = localDate2.getTime(); TimeInfo localTimeInfo = new TimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); return localTimeInfo; } // 獲取 前天開始結(jié)束 時間 public static TimeInfo 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(); long l1 = 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(); long l2 = localDate2.getTime(); TimeInfo localTimeInfo = new TimeInfo(); localTimeInfo.setStartTime(l1); localTimeInfo.setEndTime(l2); return localTimeInfo; } private static boolean isSameDay(long paramLong) { TimeInfo localTimeInfo = getTodayStartAndEndTime(); return (paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime()); } private static boolean isYesterday(long paramLong) { TimeInfo localTimeInfo = getYesterdayStartAndEndTime(); return (paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime()); } private static boolean isBeforeYesterday(long paramLong) { TimeInfo localTimeInfo = getBeforeYesterdayStartAndEndTime(); return (paramLong > localTimeInfo.getStartTime()) && (paramLong < localTimeInfo.getEndTime()); } public static boolean isSameYear(int year) { Calendar cal = Calendar.getInstance(); int CurYear = cal.get(Calendar.YEAR); // Log.e("","CurYear="+CurYear);//2015 return CurYear==year; } 。。。。。。 public class TimeInfo { private long startTime; private long endTime; public long getStartTime() { return this.startTime; } public void setStartTime(long paramLong) { this.startTime = paramLong; } public long getEndTime() { return this.endTime; } public void setEndTime(long paramLong) { this.endTime = paramLong; } }
當(dāng)前題目:利用java如何實(shí)現(xiàn)判斷日期-創(chuàng)新互聯(lián)
URL地址:http://www.rwnh.cn/article48/djihep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、企業(yè)建站、搜索引擎優(yōu)化、電子商務(wù)、建站公司、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容