创建
DateTime dateTime = new DateTime(year,month,day,hour ,minute,second,milliseconds);
DateTime dateTime = new DateTime(1910, 1, 1, 0, 0, 0, 0);
日期的增减
例: 计算 11 月中第一个星期一之后的第一个星期二
LocalDate now = SystemFactory.getClock().getLocalDate(); LocalDate electionDate = now.monthOfYear() .setCopy(11) // November .dayOfMonth() // Access Day Of Month Property .withMinimumValue() // Get its minimum value .plusDays(6) // Add 6 days .dayOfWeek() // Access Day Of Week Property .setCopy("Monday") // Set to Monday (it will round down) .plusDays(1); // Gives us Tuesday
日期的区间
Period p = new Period(old.getMillis(), now.getMillis());
1、时间类得作成
//方法一:取系统点间
DateTime dt1 = new DateTime();
//方法二:通过java.util.Date对象生成
DateTime dt2 = new DateTime(new Date());
//方法三:指定年月日点分秒生成(参数依次是:年,月,日,时,分,秒,毫秒)
DateTime dt3 = new DateTime(2012, 5, 20, 13, 14, 0, 0);
//方法四:ISO8601形式生成
DateTime dt4 = new DateTime("2012-05-20");
DateTime dt5 = new DateTime("2012-05-20T13:14:00");
//只需要年月日的时候
LocalDate localDate = new LocalDate(2009, 9, 6);// September 6, 2009
//只需要时分秒毫秒的时候
LocalTime localTime = new LocalTime(13, 30, 26, 0);// 1:30:26PM
2、获取年月日点分秒
DateTime dt = new DateTime();
//年
int year = dt.getYear();
//月
int month = dt.getMonthOfYear();
//日
int day = dt.getDayOfMonth();
//星期
int week = dt.getDayOfWeek();
//点
int hour = dt.getHourOfDay();
//分
int min = dt.getMinuteOfHour();
//秒
int sec = dt.getSecondOfMinute();
//毫秒
int msec = dt.getMillisOfSecond();
DateTime dt = new DateTime();
3、星期的特殊处理
//星期
switch(dt.getDayOfWeek()) {
case DateTimeConstants.SUNDAY:
System.out.println("星期日");
break;
case DateTimeConstants.MONDAY:
System.out.println("星期一");
break;
case DateTimeConstants.TUESDAY:
System.out.println("星期二");
break;
case DateTimeConstants.WEDNESDAY:
System.out.println("星期三");
break;
case DateTimeConstants.THURSDAY:
System.out.println("星期四");
break;
case DateTimeConstants.FRIDAY:
System.out.println("星期五");
break;
case DateTimeConstants.SATURDAY:
System.out.println("星期六");
break;
}
DateTime dt = new DateTime();
4、与JDK日期对象的转换
DateTime dt = new DateTime();
//转换成java.util.Date对象
Date d1 = new Date(dt.getMillis());
Date d2 = dt.toDate();
//转换成java.util.Calendar对象
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(dt.getMillis());
Calendar c2 = dt.toCalendar(Locale.getDefault());
5、日期前后推算
//昨天
DateTime yesterday = dt.minusDays(1);
//明天
DateTime tomorrow = dt.plusDays(1);
//1个月前
DateTime before1month = dt.minusMonths(1);
//3个月后
DateTime after3month = dt.plusMonths(3);
//2年前
DateTime before2year = dt.minusYears(2);
//5年后
DateTime after5year = dt.plusYears(5);
7、时区
//默认设置为日本时间
DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo"));
DateTime dt1 = new DateTime();
//伦敦时间
DateTime dt2 = new DateTime(DateTimeZone.forID("Europe/London"));
DateTime begin = new DateTime("2012-02-01");
DateTime end = new DateTime("2012-05-01");
8、计算区间
//计算区间毫秒数
Duration d = new Duration(begin, end);
long time = d.getMillis();
//计算区间天数
Period p = new Period(begin, end, PeriodType.days());
int days = p.getDays();
//计算特定日期是否在该区间内
Interval i = new Interval(begin, end);
boolean contained = i.contains(new DateTime("2012-03-01"));
9、日期比较
DateTime d1 = new DateTime("2012-02-01");
DateTime d2 = new DateTime("2012-05-01");
//和系统时间比
boolean b1 = d1.isAfterNow();
boolean b2 = d1.isBeforeNow();
boolean b3 = d1.isEqualNow();
//和其他日期比
boolean f1 = d1.isAfter(d2);
boolean f2 = d1.isBefore(d2);
boolean f3 = d1.isEqual(d2);
10、格式化输出
清单 11. 传递 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"); 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
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.8</version>
</dependency>