Https://www.ibm.com/developerworks/cn/java/j-jodatime.html
Non-denaturing
The Joda classes I discussed in this article have immutability, so their instances cannot be modified. (One advantage of immutable classes is that they are thread-safe)
Joda-time provides a set of Java class packages for working with date and time, including the ISO8601 standard. It can be used to completely replace the JDK date and calendar classes, and still provide good integration.
The main features of Joda-time include:
1. Easy to use: Calendar makes it difficult to get a "normal" date, so it doesn't provide a simple method, and Joda-time can access the domain directly and the index value 1 represents January.
2. Easy to scale: JDK support multi-calendar system is implemented by the subclass of calendar, so it is very cumbersome to display and in fact, it is difficult to implement other calendar systems. Joda-time support multi-calendar system is implemented by the chronology class-based plug-in system.
3. Provide a complete set of functions: it intends to provide all the functions related to Date-time computing. Joda-time currently supports 8 calendar systems, and will continue to be added in the future, with better overall performance than the JDK calendar, and more.
Attach a few examples:
1. Create any time object
Java code
- Jdk
- Calendar calendar=calendar.getinstance ();
- Calendar.set (calendar.november, 55);
- Joda-time
- DateTime datetime=New datetime (+, 55);
2. Calculate the number of days difference between two dates
Java code
- Jdk
- Calendar start = Calendar.getinstance ();
- Start.set (calendar.november, 14);
- Calendar end = Calendar.getinstance ();
- End.set (calendar.november, 15);
- Long Starttim = Start.gettimeinmillis ();
- Long Endtim = End.gettimeinmillis ();
- Long diff = Endtim-starttim;
- int days= (int) (diff// 3600/ 24);
- Joda-time
- Localdate start=New Localdate ( 14);
- Localdate end=New Localdate ( 15);
- int days = Days.daysbetween (start, end). GetDays ();
3. Get the date of the first day of the current week in the next month after 18 days
Java code
- //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. Plusmonths (1)
- . DayOfWeek (). Withminimumvalue (). ToString ("Yyyy-mm-dd HH:mm:ss");
- System.out.println (DATESTR);
4. Time format
Java code
- 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 ==> December 21, 2012 23:22:45 Friday
- String String_c = datetime.tostring ("yyyy mm month dd HH:mm:ss EE", Locale.chinese);
- System.out.println (String_c);
5. Interoperability with JDK
Java code
- //Constructed with JDK time object
- Date date = new Date ();
- datetime datetime = new datetime (date);
- Calendar calendar = Calendar.getinstance ();
- datetime = new datetime (Calendar);
- Joda-time various operations .....
- DateTime = Datetime.plusdays (1) //Increase day
- . Plusyears (1)//Increased year
- . Plusmonths (1)//Added month
- . Plusweeks (1)//Added week
- . Minusmillis (1)//minus minutes
- . minushours (1)//minus hours
- . Minusseconds (1); Number of seconds reduced
- Convert to JDK object after calculation
- Date date2 = Datetime.todate ();
- Calendar calendar2 = Datetime.tocalendar (Locale.china);
Powerful and easy-to-use date and time library thread-safe Joda