Powerful and easy-to-use date and time library thread-safe Joda

Source: Internet
Author: User
Tags dateformat diff locale

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
    1. Jdk
    2. Calendar calendar=calendar.getinstance ();
    3. Calendar.set (calendar.november, 55);
    4. Joda-time
    5. DateTime datetime=New datetime (+, 55);

2. Calculate the number of days difference between two dates

Java code
  1. Jdk
  2. Calendar start = Calendar.getinstance ();
  3. Start.set (calendar.november, 14);
  4. Calendar end = Calendar.getinstance ();
  5. End.set (calendar.november, 15);
  6. Long Starttim = Start.gettimeinmillis ();
  7. Long Endtim = End.gettimeinmillis ();
  8. Long diff = Endtim-starttim;
  9. int days= (int) (diff// 3600/ 24);
  10. Joda-time
  11. Localdate start=New Localdate ( 14);
  12. Localdate end=New Localdate ( 15);
  13. 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
  1. //JDK
  2. Calendar current = Calendar.getinstance ();
  3. Current.add (Calendar.day_of_month, 18);
  4. Current.add (Calendar.month, 1);
  5. ......
  6. DateFormat dateformat=New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
  7. Date date = Current.gettime ();
  8. String datestr = Dateformat.format (date);
  9. System.out.println (DATESTR);
  10. Joda-time
  11. String datestr = new DateTime (). Plusdays. Plusmonths (1)
  12. . DayOfWeek (). Withminimumvalue (). ToString ("Yyyy-mm-dd HH:mm:ss");
  13. System.out.println (DATESTR);

4. Time format

Java code
  1. DateTimeFormatter format = DateTimeFormat. Forpattern ("Yyyy-mm-dd HH:mm:ss");
  2. //Time Resolution
  3. datetime datetime = DateTime.Parse ("2012-12-21 23:22:45", format);
  4. //Time format, output ==> 2012/12/21 23:22:45 Fri
  5. String String_u = datetime.tostring ("Yyyy/mm/dd HH:mm:ss EE");
  6. System.out.println (String_u);
  7. //format with locale, output ==> December 21, 2012 23:22:45 Friday
  8. String String_c = datetime.tostring ("yyyy mm month dd HH:mm:ss EE", Locale.chinese);
  9. System.out.println (String_c);

5. Interoperability with JDK

Java code
    1. //Constructed with JDK time object
    2. Date date = new Date ();
    3. datetime datetime = new datetime (date);
    4. Calendar calendar = Calendar.getinstance ();
    5. datetime = new datetime (Calendar);
    6. Joda-time various operations .....
    7. DateTime = Datetime.plusdays (1) //Increase day
    8. . Plusyears (1)//Increased year
    9. . Plusmonths (1)//Added month
    10. . Plusweeks (1)//Added week
    11. . Minusmillis (1)//minus minutes
    12. . minushours (1)//minus hours
    13. . Minusseconds (1); Number of seconds reduced
    14. Convert to JDK object after calculation
    15. Date date2 = Datetime.todate ();
    16. Calendar calendar2 = Datetime.tocalendar (Locale.china);

Powerful and easy-to-use date and time library thread-safe Joda

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.