Joda time provides a set of Java class packages to handle date and time, including the ISO8601 standard. It can be used to completely replace JDK Date and Calendar classes, and still provide good integration.
Joda time's main features include:
1. Easy to use: Calendar makes it difficult to get the "normal" date, which makes it impossible to provide a simple method. Joda time can directly access the domain and the index value 1 represents January.
2. Easy to expand: JDK supports multi Calendar system through the subclass of Calendar, so it is very cumbersome to display and in fact it is very difficult to implement other Calendar systems. Joda time supports multi Calendar system through plug-in system based on Chronology class.
3. Provide a complete set of functions: it intends to provide all functions related to date time calculation. Joda time currently supports eight calendar systems, and will continue to add in the future, with better overall performance than JDK Calendar and so on.
Several examples are attached:
1. Create any time object
- //jdk
- Calendar calendar=Calendar.getInstance();
- calendar.set(2012, Calendar.NOVEMBER, 15, 18, 23,55);
- //Joda-time
- DateTime dateTime=new DateTime(2012, 12, 15, 18, 23,55);
2. Days to calculate the difference between two dates
- //jdk
- Calendar start = Calendar.getInstance();
- start.set(2012, Calendar.NOVEMBER, 14);
- Calendar end = Calendar.getInstance();
- end.set(2012, Calendar.NOVEMBER, 15);
- long startTim = start.getTimeInMillis();
- long endTim = end.getTimeInMillis();
- long diff = endTim-startTim;
- int days=(int) (diff/1000 / 3600 / 24);
- //joda-time
- LocalDate start=new LocalDate(2012, 12,14);
- LocalDate end=new LocalDate(2012, 12, 15);
- int days = Days.daysBetween(start, end).getDays();
3. Gets the first day of the current week in the next month on a day after 18 days
- //jdk
- Calendar current = Calendar.getInstance();
- current.add(Calendar.DAY_OF_MONTH, 18);
- current.add(Calendar.MONTH, 1);
- ......
- DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = current.getTime();
- String dateStr = dateFormat.format(date);
- System.out.println(dateStr);
- //joda-time
- String dateStr = new DateTime().plusDays(18).plusMonths(1)
- .dayOfWeek().withMinimumValue().toString("yyyy-MM-dd HH:mm:ss");
- System.out.println(dateStr);
4. Time format
- DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");
- //Time resolution
- DateTime dateTime = DateTime.parse("2012-12-21 23:22:45", format);
- //Time format, output = = > 2012 / 12 / 21 23:22:45 Fri
- String string_u = dateTime.toString("yyyy/MM/dd HH:mm:ss EE");
- System.out.println(string_u);
- //Format with Locale, output = = > Friday, December 21, 2012 23:22:45
- String string_c = dateTime.toString("yyyy year MM month dd day HH:mm:ss EE",Locale.CHINESE);
- System.out.println(string_c);
5. Interoperation with JDK
- //Construction by jdk time object
- Date date = new Date();
- DateTime dateTime = new DateTime(date);
- Calendar calendar = Calendar.getInstance();
- dateTime = new DateTime(calendar);
- //Joda time operations
- dateTime = dateTime.plusDays(1) //Increase day
- .plusYears(1)//Increase year
- .plusMonths(1)//Increase month
- .plusWeeks(1)//Add week
- .minusMillis(1)//Minute reduction
- .minusHours(1)//Reduce hours
- .minusSeconds(1);//Minus seconds
- //Convert to jdk object after calculation
- Date date2 = dateTime.toDate();
- Calendar calendar2 = dateTime.toCalendar(Locale.CHINA);