Android工具类篇 超详细的时间工具类
Posted 彭老希
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android工具类篇 超详细的时间工具类相关的知识,希望对你有一定的参考价值。
/**
* 时间工具类
*/
@Component
public class TimeUtil
/**
* 获取当天开始时间
* @return
*/
public static Date getDayBegin()
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);//0点
cal.set(Calendar.MINUTE, 0);//0分
cal.set(Calendar.SECOND, 0);//0秒
cal.set(Calendar.MILLISECOND, 0);//0毫秒
return cal.getTime();
/**
* 获取当天结束时间
* @return
*/
public static Date getDayEnd()
Calendar cal=Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 23);//23点
cal.set(Calendar.MINUTE, 59);//59分
cal.set(Calendar.SECOND, 59);//59秒
return cal.getTime();
/**
* 获取昨天开始时间
* @return
*/
public static Date getBeginDayOfYesterday()
Calendar cal=Calendar.getInstance();
cal.setTime(getDayBegin());//当天开始时间
cal.add(Calendar.DAY_OF_MONTH, -1);//当天月份天数减1
return cal.getTime();
/**
* 获取昨天结束时间
* @return
*/
public static Date getEndDayOfYesterday()
Calendar cal=Calendar.getInstance();
cal.setTime(getDayEnd());//当天结束时间
cal.add(Calendar.DAY_OF_MONTH, -1);//当天月份天数减1
return cal.getTime();
/**
* 获取明天开始时间
* @return
*/
public static Date getBeginDayOfTomorrow()
Calendar cal=Calendar.getInstance();
cal.setTime(getDayBegin());//当天开始时间
cal.add(Calendar.DAY_OF_MONTH, 1);//当天月份天数加1
return cal.getTime();
/**
* 获取明天结束时间
* @return
*/
public static Date getEndDayOfTomorrow()
Calendar cal=Calendar.getInstance();
cal.setTime(getDayEnd());//当天结束时间
cal.add(Calendar.DAY_OF_MONTH, 1);//当天月份天数加1
return cal.getTime();
/**
* 获取某个日期的开始时间
* @param d
* @return
*/
public static Timestamp getDayStartTime(Date d)
Calendar calendar=Calendar.getInstance();
if(null!=d)
calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
/**
* 获取某个日期的结束时间
* @param d
* @return
*/
public static Timestamp getDayEndTime(Date d)
Calendar calendar=Calendar.getInstance();
if(null!=d)
calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
/**
* 获取本周的开始时间
* @return
*/
@SuppressWarnings("unused")
public static Date getBeginDayOfWeek()
Date date=new Date();
if(date==null)
return null;
Calendar cal=Calendar.getInstance();
cal.setTime(date);
int dayOfWeek=cal.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek==1)
dayOfWeek+=7;
cal.add(Calendar.DATE, 2-dayOfWeek);
return getDayStartTime(cal.getTime());
/**
* 获取本周的结束时间
* @return
*/
public static Date getEndDayOfWeek()
Calendar cal=Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
/**
* 获取上周开始时间
*/
@SuppressWarnings("unused")
public static Date getBeginDayOfLastWeek()
Date date=new Date();
if (date==null)
return null;
Calendar cal=Calendar.getInstance();
cal.setTime(date);
int dayofweek=cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek==1)
dayofweek+=7;
cal.add(Calendar.DATE, 2-dayofweek-7);
return getDayStartTime(cal.getTime());
/**
* 获取上周的结束时间
* @return
*/
public static Date getEndDayOfLastWeek()
Calendar cal=Calendar.getInstance();
cal.setTime(getBeginDayOfLastWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
/**
* 获取今年是哪一年
* @return
*/
public static Integer getNowYear()
Date date = new Date();
GregorianCalendar gc=(GregorianCalendar)Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
/**
* 获取本月是哪一月
* @return
*/
public static int getNowMonth()
Date date = new Date();
GregorianCalendar gc=(GregorianCalendar)Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
/**
* 获取本月的开始时间
* @return
*/
public static Date getBeginDayOfMonth()
Calendar calendar=Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth()-1, 1);
return getDayStartTime(calendar.getTime());
/**
* 获取本月的结束时间
* @return
*/
public static Date getEndDayOfMonth()
Calendar calendar=Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth()-1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth()-1, day);
return getDayEndTime(calendar.getTime());
/**
* 获取上月的开始时间
* @return
*/
public static Date getBeginDayOfLastMonth()
Calendar calendar=Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth()-2, 1);
return getDayStartTime(calendar.getTime());
/**
* 获取上月的结束时间
* @return
*/
public static Date getEndDayOfLastMonth()
Calendar calendar=Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth()-2, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth()-2, day);
return getDayEndTime(calendar.getTime());
/**
* 获取本年的开始时间
* @return
*/
public static java.util.Date getBeginDayOfYear()
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTime());
/**
* 获取本年的结束时间
* @return
*/
public static java.util.Date getEndDayOfYear()
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DATE, 31);
return getDayEndTime(cal.getTime());
/**
* 两个日期相减得到的天数
* @param beginDate
* @param endDate
* @return
*/
public static int getDiffDays(Date beginDate, Date endDate)
if(beginDate==null||endDate==null)
throw new IllegalArgumentException("getDiffDays param is null!");
long diff=(endDate.getTime()-beginDate.getTime())/(1000*60*60*24);
int days = new Long(diff).intValue();
return days;
/**
* 两个日期相减得到的毫秒数
* @param beginDate
* @param endDate
* @return
*/
public static long dateDiff(Date beginDate, Date endDate)
long date1ms=beginDate.getTime();
long date2ms=endDate.getTime();
return date2ms-date1ms;
/**
* 获取两个日期中的最大日起
* @param beginDate
* @param endDate
* @return
*/
public static Date max(Date beginDate, Date endDate)
if(beginDate==null)
return endDate;
if(endDate==null)
return beginDate;
if(beginDate.after(endDate)) //beginDate日期大于endDate
return beginDate;
return endDate;
/**
* 获取两个日期中的最小日期
* @param beginDate
* @param endDate
* @return
*/
public static Date min(Date beginDate, Date endDate)
if(beginDate==null)
return endDate;
if(endDate==null)
return beginDate;
if(beginDate.after(endDate))
return endDate;
return beginDate;
/**
* 获取某月该季度的第一个月
* @param date
* @return
*/
public static Date getFirstSeasonDate(Date date)
final int[] SEASON= 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 ;
Calendar cal=Calendar.getInstance();
cal.setTime(date);
int sean = SEASON[cal.get(Calendar.MONTH)];
cal.set(Calendar.MONTH, sean*3-3);
return cal.getTime();
/**
* 返回某个日期下几天的日期
* @param date
* @param i
* @return
*/
public static Date getNextDay(Date date, int i)
Calendar cal=new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE,cal.get(Calendar.DATE)+i);
return cal.getTime();
/**
* 返回某个日期前几天的日期
* @param date
* @param i
* @return
*/
public static Date getFrontDay(Date date, int i)
Calendar cal=new GregorianCalendar()以上是关于Android工具类篇 超详细的时间工具类的主要内容,如果未能解决你的问题,请参考以下文章