需要jar:joda-time.jar
DateTime time = new DateTime()
:獲得當(dāng)前時(shí)間time.toString("yyyy-MM-dd HH:mm:ss EE")
:格式化字符串(EE是星期)例:2017-10-17 20:06:05 星期二時(shí)間解析
DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
DateTime d = DateTime.parse("2017-10-17 20:06:05",format) ;
- 增加兴枯、減少日期(數(shù)值為負(fù)就是減少日期)
DateTime dateTime1 = DateTime.parse("2012-12-03");
dateTime1 = dateTime1.plusDays(30); 等于 time.dayOfMonth().addToCopy(3)
dateTime1 = dateTime1.plusHours(3); 等于time.hourOfDay().addToCopy(3)
dateTime1 = dateTime1.plusMinutes(3); 等于time.minuteOfHour().addToCopy(3)
dateTime1 = dateTime1.plusMonths(2); 等于time.dayOfMonth().addToCopy(3)
dateTime1 = dateTime1.plusSeconds(4); 等于time.secondOfMinute().addToCopy(3)
dateTime1 = dateTime1.plusWeeks(5); 等于time.weekOfWeekyear().addToCopy(3)
dateTime1 = dateTime1.plusYears(3);等于time.year().addToCopy(3)
new DateTime().monthOfYear().isLeap();
判斷是否閏月得到某一日期的年與日時(shí)分秒
DateTime time = new DateTime() ;
time.getYear():年
time.getMonthOfYear():月
time.getDayOfMonth():日
time.getHourOfDay():時(shí)
time.getMinuteOfHour():分
time.getSecondOfMinute():秒
- 獲得兩個(gè)時(shí)間相差的年、月、日、時(shí)、分功蜓、秒
LocalDate start = new LocalDate(new DateTime("2017-02-02")) ;
LocalDate end = new LocalDate(new DateTime("2017-03-02")) ;
int years = Years.yearsBetween(start, end).getYears() ; //相差多少年
int months = Months.monthsBetween(start, end).getMonths() ; //相差多少月
int days = Days.daysBetween(start, end).getDays() ; //相差多少日
int hours = Hours.hoursBetween(start, end).getHours() ; //相差多少日
int minutes = Minutes.minutesBetween(start, end).getMinutes() ; //相差多少分
int seconds = Seconds.secondsBetween(start, end).getSeconds() ; //相差多少秒
- 獲取今年的第幾天
time.getWeekOfWeekyear();
- 獲取18天之后的某天在下個(gè)月的當(dāng)前周的第一天/最后一天日期
new DateTime().plusDays(18).plusMonths(1).dayOfWeek().withMinimumValue().toString("yyyy-MM-dd HH:mm:ss") ;
new DateTime().plusDays(18).plusMonths(1).dayOfWeek().withMaximumValue().toString("yyyy-MM-dd HH:mm:ss") ;
- 與JDK的互操作
DateTime time = new DateTime() ;
time.toDate() ; //轉(zhuǎn)換成Date
time.toCalendar(Locale.CHINA) //轉(zhuǎn)換成Calendar