分享一個使用較方便的日期計算類:

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)石首免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
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;
/**
* 獲取與今天時間差
* @param endTime
* @return
*/
public static double countDate(String startTime) {
Date curDate = new Date(System.currentTimeMillis());// 獲取當(dāng)前時間
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日期格式化工具類:
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;
/**
* 日期格式化工具類
*
* @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ù)
*
* @param date
* @param offset 時間偏量值
* @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);
}
} 以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文標(biāo)題:很實(shí)用的Android日期計算類
本文URL:http://www.chinadenli.net/article20/iggpco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、云服務(wù)器、定制開發(fā)、網(wǎng)站改版、企業(yè)建站、品牌網(wǎng)站設(shè)計
聲明:本網(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)