很实用的Android日期计算类
程序员文章站
2023-12-19 13:23:52
分享一个使用较方便的日期计算类:
package com.utils.datecount;
import java.text.simpledatef...
分享一个使用较方便的日期计算类:
package com.utils.datecount; import java.text.simpledateformat; import java.util.date; import java.util.gregoriancalendar; public class datecount { /** * datelevel 0为7天内,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());// 获取当前时间 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);// 从间隔毫秒变成间隔天数 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); } /** * 根据偏量值取得传入时间的前后天数 * * @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); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。