LocalDate和Date的互换,以及LocalDate的使用

时间:2022-01-18 22:05:25

   今天在做项目的时候,遇到一个需求,统计时间段内用户的数量(包括关注的用户/取消关注的用户/净增长的用户/总用户数量等),我当时考虑到计算两个时间段的天数差,然后循环对每一天分别做统计,我想循环的时候需要考虑加一天之后到了下一个月或者是下一年,都需要判断的。我使用的计算时间段内的天数的方法是:

   Date startDate = new Date(Long.valueOf(startTime));
Date endDate = new Date(Long.valueOf(endTime));
int count = (int)((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24));

       传递的参数开始时间和结束时间都是String类型的。后来一个老员工审查我的code的时候告诉我使用org.joda.time是best practice(最佳实践),使用org.joda.time不需要考虑加一天之后会变成下一年或者下一个月。它帮我们做了。

   //需要导的包
import org.joda.time.Days;
import org.joda.time.LocalDate;

LocalDate startLocalDate = LocalDateUtil.getLocalDate(startTime);
LocalDate endLocalDate = LocalDateUtil.getLocalDate(endTime);
int days = Days.daysBetween(startLocalDate, endLocalDate).getDays();
//还可以使用org.joda.time.Period来完成天数的计算,之后我会补充

      以下是一些常用的封装的LocalDate的方法:

   //String类型的时间撮转LocalDate
public static LocalDate getLocalDate(String dateStr) {
LocalDate localDate = null;
if (StringUtils.hasText(dateStr)) {
localDate = new LocalDate(Long.valueOf(dateStr));
} else {
localDate = new LocalDate();
}
return localDate;
}
//long类型的时间撮转LocalDate
public static LocalDate getLocalDate(long date) {
return new LocalDate(date);
}
//获取当天的Date(去掉时分秒,只有年月日)
public static Date getToday() {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime);
return date.toDate();
}
//获取昨天的Date(去掉时分秒,只有年月日)
public static Date getYesterday() {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.minusDays(1));
return date.toDate();
}
//获取明天的Date(去掉时分秒,只有年月日)
public static Date getTomorrow() {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.plusDays(1));
return date.toDate();
}
//获取这周开始的星期一的时间
public static Date getThisWeekStartday() {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime);
return date.dayOfWeek().withMinimumValue().toDate();
}
//获取上周开始的星期一的时间
public static Date getLastWeekStartday() {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.minusWeeks(1));
return date.dayOfWeek().withMinimumValue().toDate();
}
//获取这个月开始的星期一的时间
public static Date getThisMonthStartday() {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime);
return date.dayOfMonth().withMinimumValue().toDate();
}
 //获取上个月开始的星期一的时间
public static Date getLastMonthStartday() {
DateTime dateTime = new DateTime();
LocalDate date = new LocalDate(dateTime.minusMonths(1));
return date.dayOfMonth().withMinimumValue().toDate();
}
/**
* 如果传递的days大于0,表示得到boundary之后的days的日期
* 如果传递的days小于0,表示得到boundary之前的days的日期
*/
public static Date getSpecificDay(Date boundary, int days) {
DateTime dateTime = new DateTime(boundary);
LocalDate date = null;
if (days < 0) {
date = new LocalDate(dateTime.minusDays(-days));
} else {
date = new LocalDate(dateTime.plusDays(days));
}
return date.toDate();
}
//立即数:相当于高级语言中的常量(常数),它是直接出现在指令中的数,不用存储在寄存器或存储器中的数,如指令ADD AL,06H中的06H即为立即数
//获取两个时间的差距的天数
public static int diffDays(Date date1, Date date2)
   {
        Date bigDay = null;
        Date smallDay = null;
        if (date1.after(date2)) {
            bigDay = date1;
            smallDay = date2;
        } else {
            bigDay = date2;
            smallDay = date1;
        }
        //return date1.getTime() / (24*60*60*1000) - date2.getTime() / (24*60*60*1000);
        return (int) (bigDay.getTime() / 86400000 - smallDay.getTime() / 86400000);//用立即数,减少乘法计算的开销
   }
//获取的时区时间和实际的时间差距8个小时
public static Date convertByTimeZone(Date date, String timeZoneStr) {
        if (null == timeZoneStr) {
            timeZoneStr = "GMT+00";
        }
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sf.setTimeZone(TimeZone.getTimeZone(timeZoneStr));
        Date newDate = null;
        try {
            newDate = sf1.parse(sf.format(date));
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
        return newDate;
   }
 //测试一下方法:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String startDate = "1414024810000";(2014/10/23 8:40:10)
LocalDate localDate1 = getLocalDate(startDate);
LocalDate localDate2 = getLocalDate(Long.valueOf(startDate));
System.out.println("localDate1 = " + localDate1 + ",localDate2 = " + localDate2);
Date date1 = getToday();
Date date2 = getYesterday();
Date date3 = getTomorrow();
System.out.println("date1 = " + sdf.format(date1) + " ,date2 = " + sdf.format(date2) + " ,date3 = " + sdf.format(date3));
Date date4 = getThisWeekStartday();
Date date5 = getLastWeekStartday();
Date date6 = getThisMonthStartday();
Date date7 = getLastMonthStartday();
System.out.println("date4 = " + sdf.format(date4) + " ,date5 = " + sdf.format(date5) + " ,date6 = " + sdf.format(date6) + " ,date7 = " + sdf.format(date7));
//得到今天之前的倒数两天的日期 今天是10月23日
System.out.println(sdf.format(getSpecificDay(new Date(), -2)));
System.out.println(sdf.format(getSpecificDay(new Date(), 2)));
int result = diffDays(date3, date1);
        int result1 = diffDays(date1, date3);
        System.out.println("result = " + result + ", result1 = " + result1);
        System.out.println(convertByTimeZone(new Date(), "GMT+00"));
        System.out.println(sdf.format(convertByTimeZone(new Date(), "GMT+00")));
}
 
    //结果输出:    localDate1 = 2014-10-23,localDate2 = 2014-10-23    date1 = 2014/10/23 00:00:00 ,date2 = 2014/10/22 00:00:00 ,date3 = 2014/10/24 00:00:00    date4 = 2014/10/20 00:00:00 ,date5 = 2014/10/13 00:00:00 ,date6 = 2014/10/01 00:00:00 ,date7 = 2014/09/01 00:00:00    2014/10/21 00:00:00    2014/10/25 00:00:00    result = 1, result1 = 1    Thu Oct 23 11:35:23 CST 2014    2014/10/23 11:35:23