Powerful and easy to use date and time base Joda Time

Keywords: JDK Java

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

  1. //jdk  
  2. Calendar calendar=Calendar.getInstance();  
  3. calendar.set(2012, Calendar.NOVEMBER, 151823,55);  
  4.   
  5. //Joda-time  
  6. DateTime dateTime=new DateTime(201212151823,55);  
 

2. Days to calculate the difference between two dates

 

  1. //jdk  
  2. Calendar start = Calendar.getInstance();   
  3. start.set(2012, Calendar.NOVEMBER, 14);  
  4.   
  5. Calendar end = Calendar.getInstance();  
  6. end.set(2012, Calendar.NOVEMBER, 15);  
  7.   
  8. long startTim = start.getTimeInMillis();  
  9. long endTim = end.getTimeInMillis();  
  10. long diff = endTim-startTim;  
  11. int days=(int) (diff/1000 / 3600 / 24);  
  12.   
  13.   
  14. //joda-time  
  15. LocalDate start=new LocalDate(201212,14);  
  16. LocalDate end=new LocalDate(20121215);  
  17. 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

 

  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.   
  11. //joda-time  
  12. String dateStr = new DateTime().plusDays(18).plusMonths(1)  
  13.     .dayOfWeek().withMinimumValue().toString("yyyy-MM-dd HH:mm:ss");  
  14. System.out.println(dateStr);  
 

 

4. Time format

  1. DateTimeFormatter format = DateTimeFormat .forPattern("yyyy-MM-dd HH:mm:ss");  
  2.   
  3.         //Time resolution  
  4.         DateTime dateTime = DateTime.parse("2012-12-21 23:22:45", format);  
  5.           
  6.         //Time format, output = = > 2012 / 12 / 21 23:22:45 Fri  
  7.         String string_u = dateTime.toString("yyyy/MM/dd HH:mm:ss EE");  
  8.         System.out.println(string_u);  
  9.           
  10.         //Format with Locale, output = = > Friday, December 21, 2012 23:22:45  
  11.         String string_c = dateTime.toString("yyyy year MM month dd day HH:mm:ss EE",Locale.CHINESE);  
  12.         System.out.println(string_c);  
 

 

5. Interoperation with JDK

  1.              //Construction by jdk time object  
  2. Date date = new Date();  
  3. DateTime dateTime = new DateTime(date);  
  4.   
  5. Calendar calendar = Calendar.getInstance();  
  6. dateTime = new DateTime(calendar);  
  7.   
  8. //Joda time operations  
  9. dateTime = dateTime.plusDays(1//Increase day  
  10.                     .plusYears(1)//Increase year  
  11.                     .plusMonths(1)//Increase month  
  12.                     .plusWeeks(1)//Add week  
  13.                     .minusMillis(1)//Minute reduction  
  14.                     .minusHours(1)//Reduce hours  
  15.                     .minusSeconds(1);//Minus seconds  
  16.                       
  17. //Convert to jdk object after calculation  
  18. Date date2 = dateTime.toDate();  
  19. Calendar calendar2 = dateTime.toCalendar(Locale.CHINA);  
 

 



Posted by phpcode on Tue, 31 Mar 2020 16:09:36 -0700