Joda Time 使用
对于系统的一些时间操作很是不方便,为了方便转化,有时候用date,有时候用timestmp,有时候用calendar,忍不住想更改了.
但是任务巨大,先把笔记收藏了,后面有机会再更改了.下面只是几个使用的例子.
1、 获取今天的开始时间:比如:2014-06-19 00:00:00
//joda
DateTime nowTime = new DateTime();
DateTime startOfDay = nowTime.withTimeAtStartOfDay();
//jdk
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(timeInMs);
c1.set(Calendar.HOUR_OF_DAY, 0);
c1.set(Calendar.MINUTE, 0);
c1.set(Calendar.SECOND, 0);
c1.set(Calendar.MILLISECOND, 0);
return c1.getTimeInMillis();
2、 获取今天的结束时间:比如:2014-06-19 23:59:59
//joda
DateTime nowTime = new DateTime();
DateTime endOfDay = nowTime.millisOfDay().withMaximumValue();
//jdk
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(timeInMs);
c1.set(Calendar.HOUR_OF_DAY, 23);
c1.set(Calendar.MINUTE, 59);
c1.set(Calendar.SECOND, 59);
c1.set(Calendar.MILLISECOND, 0);
return c1.getTimeInMillis();
3、 获取现在距离今天结束还有多长时间
//joda
DateTimenowTime = new DateTime();
DateTime endOfDay = nowTime.millisOfDay().withMaximumValue();
endOfDay.getMillis()-nowTime.getMillis()
4、 计算两个日期的相隔天数
- JodaTime
//joda
DateTime nowTime = new DateTime();
DateTime futureTime = new DateTime(2015, 10, 1, 0, 0, 0);
Int days = Days.daysBetween(nowTime, futureTime).getDays();
//jdk
public static int getBetweenDay(long startTime, long endTime) {
Calendar start = Calendar.getInstance();
start.setTimeInMillis(startTime);
Calendar end = Calendar.getInstance();
end.setTimeInMillis(endTime);
return (int) ((end.getTimeInMillis() - start.getTimeInMillis()) / 86400000);
}
5、 创建任意时间对象
//Joda-time
DateTime dateTime=new DateTime(2012, 12, 15, 18, 23,55);
//jdk
Calendar calendar=Calendar.getInstance();
calendar.set(2012, Calendar.NOVEMBER, 15, 18, 23,55);
6、 与JDK互操作
//通过jdk时间对象构造
Date date = new Date();
DateTime dateTime = new DateTime(date);
Calendar calendar = Calendar.getInstance();
dateTime = new DateTime(calendar);
// Joda-time 各种操作.....
dateTime = dateTime.plusDays(1) // 增加天
.plusYears(1)// 增加年
.plusMonths(1)// 增加月
.plusWeeks(1)// 增加星期
.minusMillis(1)// 减分钟
.minusHours(1)// 减小时
.minusSeconds(1);// 减秒数
// 计算完转换成jdk 对象
Date date2 = dateTime.toDate();
Calendar calendar2 = dateTime.toCalendar(Locale.CHINA);
7、 加上90天后输出
//joda
DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0);
System.out.println(dateTime.plusDays(90).toString("E MM/dd/yyyy HH:mm:ss.SSS");
jdk
Calendar calendar = Calendar.getInstance();
calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);
SimpleDateFormat sdf =
new SimpleDateFormat("E MM/dd/yyyy HH:mm:ss.SSS");
calendar.add(Calendar.DAY_OF_MONTH, 90);
System.out.println(sdf.format(calendar.getTime()));
8、 时间的输出显示
//通过标准的 ISO-8601输出
DateTime dateTime = SystemFactory.getClock().getDateTime();
dateTime.toString(ISODateTimeFormat.basicDateTime());
dateTime.toString(ISODateTimeFormat.basicDateTimeNoMillis());
dateTime.toString(ISODateTimeFormat.basicOrdinalDateTime());
dateTime.toString(ISODateTimeFormat.basicWeekDateTime());
//传递 SimpleDateFormat字符串输出
DateTime dateTime = SystemFactory.getClock().getDateTime();
dateTime.toString("MM/dd/yyyy hh:mm:ss.SSSa");
dateTime.toString("dd-MM-yyyy HH:mm:ss");
dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa");
dateTime.toString("MM/dd/yyyy HH:mm ZZZZ");
dateTime.toString("MM/dd/yyyy HH:mm Z");
//输出的结果如下:
20090906T080000.000-0500
20090906T080000-0500
2009249T080000.000-0500
2009W367T080000.000-0500
09/06/2009 02:30:00.000PM
06-Sep-2009 14:30:00
Sunday 06 September, 2009 14:30:00PM
09/06/2009 14:30 America/Chicago
09/06/2009 14:30 -0500
参考的博客
Joda Time 使用 - CSDN博客
http://blog.csdn.net/zeus_9i/article/details/32318771
Overview (Joda-Time 2.9.9 API)
http://www.joda.org/joda-time/apidocs/
强大易用的日期和时间库 Joda Time - 坚持而不浮躁 - ITeye博客
http://persevere.iteye.com/blog/1755237
Joda-Time 简介
https://www.ibm.com/developerworks/cn/java/j-jodatime.html
Releases · JodaOrg/joda-time(github架包和源码地址)
https://github.com/JodaOrg/joda-time/releases