很實(shí)用的Android日期計(jì)算類(lèi)
分享一個(gè)使用較方便的日期計(jì)算類(lèi):
package com.utils.datecount; import java.text.SimpleDateFormat; import java.util.Date; import java.util.GregorianCalendar; public class DateCount { /** * datelevel 0為7天內(nèi),1為7到15天,2為15天以上 */ public static int datelevel = 0; public static int positionweek = -1; public static int position2week = -1; /** * 獲取與今天時(shí)間差 * @param endTime * @return */ public static double countDate(String startTime) { Date curDate = new Date(System.currentTimeMillis());// 獲取當(dāng)前時(shí)間 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// 輸入日期的格式 Date date = null; try { date = simpleDateFormat.parse(startTime); if (date == null) return 0; } catch (java.text.ParseException e) { return 0; // e.printStackTrace(); } GregorianCalendar cal1 = new GregorianCalendar(); GregorianCalendar cal2 = new GregorianCalendar(); cal1.setTime(date); cal2.setTime(curDate); double dayCount = (cal2.getTimeInMillis() - cal1.getTimeInMillis()) / (1000 * 3600 * 24);// 從間隔毫秒變成間隔天數(shù) return dayCount; } }
Android日期格式化工具類(lèi):
package com.utils.tools; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import android.text.TextUtils; import android.text.format.DateFormat; /** * 日期格式化工具類(lèi) * * @author Harryxu * */ public class DateUtil { public static Date convert2Date(String format, String dateStr) { Date date = null; try { if (TextUtils.isEmpty(format)) format = "yyyy-MM-dd HH:mm:ss"; return new java.sql.Date(new SimpleDateFormat(format).parse(dateStr).getTime()); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return date; } public static CharSequence formatDate(String format, String dateStr) { Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } if (date == null) date = new Date(); if (TextUtils.isEmpty(format)) format = "yyyy-MM-dd"; return DateFormat.format(format, date); } /** * 根據(jù)偏量值取得傳入時(shí)間的前后天數(shù) * * @param date * @param offset 時(shí)間偏量值 * @return */ public static Date getNextPreDay(Date date, int offset) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_MONTH, offset); date = calendar.getTime(); return date; } public static int getDays(Date date1, Date date2) { Date kaishi = date1, jieshu = date2; if (date1.compareTo(date2) > 0) { kaishi = date2; jieshu = date1; } long diff = jieshu.getTime() - kaishi.getTime(); float days = diff / 24f / 60 / 60 / 1000; return (int) ((days - (int) days) >= 0 ? (days + 1) : days); } public static String addMinites( String time, int addminite){ SimpleDateFormat myFormatter = new SimpleDateFormat("HH:mm"); Calendar totime = Calendar.getInstance(); java.util.Date date = null; try { date = myFormatter.parse(time); } catch (ParseException e) { e.printStackTrace(); } if (date == null) return null; totime.setTime(date); totime.add(Calendar.MINUTE, addminite); date = totime.getTime(); return myFormatter.format(date); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析android中系統(tǒng)日期時(shí)間的獲取
- android獲取時(shí)間差的方法
- Android調(diào)用系統(tǒng)時(shí)間格式顯示時(shí)間信息
- Android中日期與時(shí)間設(shè)置控件用法實(shí)例
- Android日期時(shí)間格式國(guó)際化的實(shí)現(xiàn)代碼
- Android開(kāi)發(fā)中日期工具類(lèi)DateUtil完整實(shí)例
- Android開(kāi)發(fā)之時(shí)間日期操作實(shí)例
- Android仿iPhone日期時(shí)間選擇器詳解
- Android開(kāi)發(fā)中DatePicker日期與時(shí)間控件實(shí)例代碼
- Android編程實(shí)現(xiàn)將時(shí)間轉(zhuǎn)化成幾分鐘前、幾天前等形式的工具類(lèi)
相關(guān)文章
Android實(shí)現(xiàn)多段顏色進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多段顏色進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01Android 如何從零開(kāi)始寫(xiě)一款書(shū)籍閱讀器的示例
這篇文章主要介紹了Android 如何從零開(kāi)始寫(xiě)一款書(shū)籍閱讀器的示例,詳細(xì)的介紹了搭建整個(gè)閱讀器的框架,有興趣的可以了解一下2018-01-01Android Studio Gradle 更換阿里云鏡像的方法
這篇文章主要介紹了Android Studio Gradle 更換阿里云鏡像的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android webview手動(dòng)校驗(yàn)https證書(shū)(by 星空武哥)
有些時(shí)候由于Android系統(tǒng)的bug或者其他的原因,導(dǎo)致我們的webview不能驗(yàn)證通過(guò)我們的https證書(shū),最明顯的例子就是華為手機(jī)mate7升級(jí)到Android7.0后,手機(jī)有些網(wǎng)站打不開(kāi)了,而更新了webview的補(bǔ)丁后就沒(méi)問(wèn)題了2017-09-09Android實(shí)現(xiàn)EditText輸入金額
EditText是Android中一個(gè)非常實(shí)用的控件,有很多InputType,可以來(lái)達(dá)到不同的輸入效果,下面通過(guò)實(shí)例代碼給大家解析android實(shí)現(xiàn)edittext輸入金額,需要的朋友參考下吧2016-12-12Android ScrollView實(shí)現(xiàn)橫向和豎向拖動(dòng)回彈效果
這篇文章主要為大家詳細(xì)介紹了Android ScrollView實(shí)現(xiàn)橫向和豎向拖動(dòng)回彈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09