public static void main(String[] args) { DateTime dt = new DateTime(); // Joda Date System.out.println("Date:" + dt.toDate()); // Java Date int month = dt.getMonthOfYear(); System.out.println("MonthOfYear: " + month); DateTime.Property pDoW = dt.dayOfWeek(); // Monday:1 to Sunday:7 System.out.println("dayOfWeek: " + pDoW.getAsText()); // print:Monday/Tuesday System.out.println("getDayOfMonth: " + dt.getDayOfMonth()); int maxDay = dt.dayOfMonth().getMaximumValue(); System.out.println("Last day of this month: " + maxDay + " day"); boolean leapYear = dt.yearOfEra().isLeap(); System.out.println("Leap Year: " + leapYear); DateTime datePlus20 = dt.plusDays(20); DateTimeFormatter formattedDate = DateTimeFormat.forPattern("dd/MM/yyyy"); System.out.println( dt.toString(formattedDate) + " + 20 day = " + datePlus20.toString(formattedDate)); }
@Test public void testDate() { DateTime dt = DateTime.now().millisOfDay().withMinimumValue(); ; System.out.println(JodatimeUtils.formatDateTime(dt.toDate())); DateTime date = JodatimeUtils.parse("2015-03-18"); System.out.println("date: " + date.getDayOfMonth()); Assert.assertEquals(18, date.getDayOfMonth()); date = JodatimeUtils.parse("2016-04-13"); System.out.println("week: " + date.getWeekOfWeekyear()); Assert.assertEquals(15, date.getWeekOfWeekyear()); DateTime dateTime = DateTime.parse("2016-04-13"); System.out.println("dateTime:" + dateTime); DateTime start = dateTime.dayOfWeek().withMinimumValue(); DateTime end = start.plusWeeks(1); System.out.println("start:" + start); System.out.println("end:" + end); Assert.assertEquals("2016-04-11", start.toString("yyyy-MM-dd")); Assert.assertEquals("2016-04-18", end.toString("yyyy-MM-dd")); start = dateTime.dayOfMonth().withMinimumValue(); end = start.plusMonths(1); System.out.println("start:" + start); System.out.println("end:" + end); Assert.assertEquals("2016-04-01", start.toString("yyyy-MM-dd")); Assert.assertEquals("2016-05-01", end.toString("yyyy-MM-dd")); start = dateTime.dayOfYear().withMinimumValue(); end = start.plusYears(1); System.out.println("start:" + start); System.out.println("end:" + end); Assert.assertEquals("2016-01-01", start.toString("yyyy-MM-dd")); Assert.assertEquals("2017-01-01", end.toString("yyyy-MM-dd")); }
/** * date是 星期几 * * @param date * @return */ public static int week(String date) { DateTime dateTime = DateTime.parse(date, ISODateTimeFormat.basicDate()); return dateTime.dayOfWeek().get(); }