Joda-Time研究

最近在做一个数据统计的需求,需要多种对时间的计算。由于Calendar对时间的计算实在是过于复杂繁琐,因此就想寻求一种比较简单粗暴的方式。很幸运,找到了joda。下面是一些代码实例。
/**

可以看出,利用joda确实是比calendar快了很多,类似的获取当前时间的后几个月有plusMonths(),后几年plusYears(),后几个小时plusHours()等等甚至可以精确到毫秒。若要获取之前的时间,只需要将plus换成minus即可。br/>当然joda的计算功能远不止如此,它可以很灵活的计算出任何你想要的或者想进行对比的时间。看下面这个例子。
@Test
public void demoOthers() {
String formatString = "yyyy-MM-dd HH:mm:ss";

    DateTime dateTime = new DateTime();
    // 获取当前时间月的第一天
    LocalDate firstDayOfMonth = dateTime.toLocalDate().withDayOfMonth(1);
    System.out.println("本月第一天日期:"+ firstDayOfMonth.toString(formatString,Locale.CHINESE));

    // 获取当前周的周一和周末
    LocalDate firstDayOfWeek = dateTime.toLocalDate().withDayOfWeek(1);
    LocalDate lastDayOfWeek = dateTime.toLocalDate().withDayOfWeek(7);
    System.out.println("本周第一天日期:"+ firstDayOfWeek.toString(formatString,Locale.CHINESE));
    System.out.println("本周最后一天日期:"+ lastDayOfWeek.toString(formatString,Locale.CHINESE));

    // 当前月的第一天和最后一天
    DateTime firstDayOfCurrentMonth = dateTime.dayOfMonth().withMinimumValue();
    DateTime lastDayOfCurrentMonth = dateTime.dayOfMonth().withMaximumValue();
    System.out.println("本月第一天日期:"+ firstDayOfCurrentMonth.toString(formatString,Locale.CHINESE));
    System.out.println("本月最后一天日期:"+ lastDayOfCurrentMonth.toString(formatString,Locale.CHINESE));

    // 当前年的第一天和最后一天
    DateTime firstDayOfCurrentYear = dateTime.dayOfYear().withMinimumValue();
    DateTime lastDayOfCurrentYear = dateTime.dayOfYear().withMaximumValue();
    System.out.println("本年第一天日期:"+ firstDayOfCurrentYear.toString(formatString,Locale.CHINESE));
    System.out.println("本年最后一天日期:"+ lastDayOfCurrentYear.toString(formatString,Locale.CHINESE));

    DateTime begin = new DateTime(1994, 10, 1, 0, 0, 0,0);
    Duration duration = new Duration(begin, dateTime);

    // 两个时间之间 所差秒
    System.out.println(begin.toString(formatString) + " 与 "  + dateTime.toString(formatString) + " 相差的秒:" + duration.getStandardSeconds());
    //计算区间天数  ,小时,秒
    Period p = new Period(new DateTime(1994, 10, 1, 0, 0, 0,0),new DateTime(), PeriodType.days());
    System.out.println("相隔的天:"+p.getDays());

    // 计算之前月份的时间操作
    LocalDate d = new DateTime().toLocalDate();
    // 上个月(可以是之前的任意月)的最后一天
    LocalDate lastDayOfPreviousMonth = d.minusMonths(1).dayOfMonth().withMaximumValue();

    LocalDate.Property e = d.minusWeeks(1).dayOfWeek();
    System.out.println("上周的周一:" + e.withMinimumValue().toString(formatString, Locale.CHINESE));
    System.out.println("上周的周日:" + e.withMaximumValue().toString(formatString, Locale.CHINESE));

    //汉字形式标识今年和去年时间
    System.out.println("汉字形式:" + dateTime.minusYears(0).year().getAsText(Locale.CHINESE));
    System.out.println("汉字形式:" + dateTime.minusYears(1).monthOfYear().getAsText(Locale.CHINESE));
    System.out.println("汉字形式:"+ dateTime.minusYears(1).dayOfMonth().getAsText(Locale.CHINESE));
    System.out.println("汉字形式:" + dateTime.minusYears(1).dayOfWeek().getAsText(Locale.CHINESE));

    // 判断是否是闰年 闰月
    System.out.println("是否闰月:" + dateTime.monthOfYear().isLeap());
    System.out.println("是否闰年:" + dateTime.year().isLeap());
    System.out.println("去年是否闰年:" + dateTime.minusYears(1).year().isLeap());

    //获取现在距离今天结束还有多久时间
    System.out.println( dateTime.millisOfDay().withMaximumValue().getMillis() - dateTime.getMillis());

    //10 天后 那周的周一是
    System.out.println("十天后的那周的周一是:" + dateTime.plusDays(10).dayOfWeek().withMinimumValue().toString(formatString));

    // DateTime与java.util.Date对象,当前系统TimeMillis转换
    DateTime temp1 = new DateTime(new Date());
    Date temp2 = dateTime.toDate();
    DateTime temp3 = new DateTime(System.currentTimeMillis());

    Calendar calendar = Calendar.getInstance();
    DateTime  temp4 = new DateTime(calendar);
}

总之,这个joda用起来比较快速,也比较灵活。是一个上手很快的工具。

猜你喜欢

转载自blog.51cto.com/12122148/2344155