Joda-time open source Library

Source: Internet
Author: User

Joda-time is a Java? The easy-to-use open-source time library of the platform.

Dependency

1 <dependency>2     <groupId>joda-time</groupId>3     <artifactId>joda-time</artifactId>4     <version>2.9.9</version>5 </dependency>

Usage

1. Generation of time and date classes

1 // Method 1: Take the system point 2 datetime dt1 = new datetime (); 3 4 // Method 2: Use Java. util. date object Generation 5 datetime dt2 = new datetime (new date (); 6 7 // method 3: specify year, month, day, minute, and second generation (the parameters are year, month, day, hour, minute, second, millisecond) 8 datetime dt3 = new datetime (2012, 5, 20, 13, 14, 0, 0); 9 10 // Method 4: generate 11 datetime dt4 = new datetime ("2012-05-20") in iso8601 format; 12 datetime dt5 = new datetime ("2012-05-20t13: 14: 00 "); 13 14 // only 15 localdate = new localdate (2009, 9, 6) when the year, month, and day are required; // September 6, 2009 16 17 // 18 localtime = new localtime (13, 30, 26, 0) when only hours, minutes, And seconds are required; // 1:30:26

2. Get year, month, day, minute, and second

1 datetime dt = new datetime (); 2 // year 3 int year = DT. getyear (); 4 // month 5 Int month = DT. getmonthofyear (); 6 // day 7 int day = DT. getdayofmonth (); 8 // week 9 int week = DT. getdayofweek (); 10 // point 11 int hour = DT. gethourofday (); 12 // minute 13 int min = DT. getminuteofhour (); 14 // second 15 int sec = DT. getsecondofminute (); 16 // millisecond 17 int MSEC = DT. getmillisofsecond ();

3. Weekly Processing

1 datetime dt = new datetime (); 2 3 // 4 switch (DT. getdayofweek () {5 case datetimeconstants. sunday: 6 system. out. println ("Sunday"); 7 break; 8 case datetimeconstants. monday: 9 system. out. println ("Monday"); 10 break; 11 case datetimeconstants. tuesday: 12 system. out. println ("Tuesday"); 13 break; 14 case datetimeconstants. wednesday: 15 system. out. println ("Wednesday"); 16 break; 17 case datetimeconstants. thursday: 18 system. out. println ("Thursday"); 19 break; 20 case datetimeconstants. friday: 21 system. out. println ("Friday"); 22 break; 23 case datetimeconstants. saturday: 24 system. out. println ("Saturday"); 25 break; 26}

4. Conversion from JDK objects

1 datetime dt = new datetime (); 2 3 // convert to Java. util. date object 4 Date d1 = new date (DT. getmillis (); 5 Date D2 = DT. todate (); 6 7 // convert to Java. util. calendar Object 8 Calendar C1 = calendar. getinstance (); 9 c1.settimeinmillis (DT. getmillis (); 10 calendar C2 = DT. tocalendar (locale. getdefault ());

5. Calculation before and after the date

1 datetime dt = new datetime (); 2 3 // Yesterday 4 datetime yesterday = DT. minusdays (1); 5 // tomorrow 6 datetime Tomorrow = DT. plusdays (1); 7 // the first 8 datetime before1month = DT. minusmonths (1); 9 // 10 datetime after3month = DT after 3 months. plusmonths (3); 11 // 12 datetime before2year = DT 2 years ago. minusyears (2); 13 // 14 datetime after5year = DT after 5 years. plusyears (5 );

6. Time Comparison

Datetime d1 = new datetime ("2012-02-01"); datetime D2 = new datetime ("2012-05-01"); // compare with the system time boolean b1 = d1.isafternow (); boolean b2 = d1.isbeforenow (); Boolean B3 = d1.isequalnow (); // comparison with other dates: Boolean F1 = d1.isafter (D2); Boolean F2 = d1.isbefore (D2 ); boolean F3 = d1.isequal (D2 );

7. Time Difference

1 datetime begin = new datetime ("2012-02-01"); 2 datetime end = new datetime ("2012-05-01 "); 3 4 // Number of milliseconds in the calculation interval 5 Duration d = new duration (begin, end); 6 long time = D. getmillis (); 7 8 // number of days in the calculation interval 9 period p = New Period (begin, end, periodtype. days (); 10 int days = P. getdays (); 11 12 // calculate whether the specified date is within this range 13 interval I = new interval (begin, end); 14 Boolean contained = I. contains (New datetime ("2012-03-01 "));

8. format the output.

1 DateTime dateTime = new DateTime();  2   3 String s1 = dateTime.toString("yyyy/MM/dd hh:mm:ss.SSSa");  4 String s2 = dateTime.toString("yyyy-MM-dd HH:mm:ss");  5 String s3 = dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa");  6 String s4 = dateTime.toString("yyyy/MM/dd HH:mm ZZZZ");  7 String s5 = dateTime.toString("yyyy/MM/dd HH:mm Z"); 

 

Util template for joda-time

 1 public class DataTimeUtil{ 2      3     public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss"; 4  5     public static Date strToDate(String dataTimeStr){ 6         DataTimeFormatter dateTimeFormatter = DataTimeFormat.forPattern(STANDARD_FORMAT); 7         DateTime dateTime = dateTimeFormatter.parseDateTime(dataTimeStr); 8         return dateTime.toDate(); 9     }10     11     public static String dateToStr(Date date){12         if (date = null){13             return StringUtils.EMPTY;14         }15         DateTime dateTime = new Datetime(date);16         return datetime.toString(STANDARD_FORMAT);17     }18 }

 

 

Reference: https://www.ibm.com/developerworks/cn/java/j-jodatime.html

Joda-time open source Library

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.