Joda-time of Java Date calculation

Source: Internet
Author: User

http://rensanning.iteye.com/blog/1546652

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.

http://joda-time.sourceforge.net/

Version: Joda-time-2.1.jar

1, time class to be made

Java code
  1. Method One: Take the system between points
  2. DateTime DT1 = new DateTime ();
  3. Method Two: Generate by Java.util.Date object
  4. DateTime DT2 = new DateTime (new Date ());
  5. Method Three: Specify the day of the month and the second generation (the parameters are: years, months, days, hours, minutes, seconds, milliseconds)
  6. DateTime DT3 = new DateTime (5, 0, 0);
  7. Method Four: ISO8601 form generation
  8. DateTime DT4 = new DateTime ("2012-05-20");
  9. DateTime DT5 = new DateTime ("2012-05-20t13:14:00");
  10. It only takes years and days
  11. Localdate localdate = new Localdate ( 9, 6); September 6,
  12. It just takes a minute, seconds, minutes.
  13. LocalTime localtime = new LocalTime ( 0); 1:30:26pm



2, get the date of the day and seconds.

Java code
  1. DateTime dt = new DateTime ();
  2. Years
  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. Score of
  13. int min = Dt.getminuteofhour ();
  14. Seconds
  15. int sec = Dt.getsecondofminute ();
  16. Milliseconds
  17. int msec = Dt.getmillisofsecond ();



3, the special treatment of the week

Java code
  1. DateTime dt = new DateTime ();
  2. Week
  3. Switch (Dt.getdayofweek ()) {
  4. Case Datetimeconstants.sunday:
  5. System.out.println ("Sunday");
  6. Break ;
  7. Case Datetimeconstants.monday:
  8. System.out.println ("Monday");
  9. Break ;
  10. Case Datetimeconstants.tuesday:
  11. System.out.println ("Tuesday");
  12. Break ;
  13. Case Datetimeconstants.wednesday:
  14. System.out.println ("Wednesday");
  15. Break ;
  16. Case Datetimeconstants.thursday:
  17. System.out.println ("Thursday");
  18. Break ;
  19. Case Datetimeconstants.friday:
  20. System.out.println ("Friday");
  21. Break ;
  22. Case Datetimeconstants.saturday:
  23. System.out.println ("Saturday");
  24. Break ;
  25. }



4. Conversion to JDK Date Object

Java code
    1. DateTime dt = new DateTime ();
    2. Convert to Java.util.Date Object
    3. Date D1 = new Date (Dt.getmillis ());
    4. Date D2 = Dt.todate ();
    5. Convert to Java.util.Calendar Object
    6. Calendar C1 = Calendar.getinstance ();
    7. C1.settimeinmillis (Dt.getmillis ());
    8. Calendar C2 = Dt.tocalendar (Locale.getdefault ());



5, the date before and after the calculation

Java code
  1. DateTime dt = new DateTime ();
  2. Yesterday
  3. DateTime yesterday = dt.minusdays (1);
  4. Tomorrow
  5. DateTime tomorrow = Dt.plusdays (1);
  6. 1 months ago
  7. DateTime before1month = dt.minusmonths (1);
  8. 3 months later
  9. DateTime after3month = dt.plusmonths (3);
  10. 2 years ago
  11. DateTime before2year = Dt.minusyears (2);
  12. 5 years later
  13. DateTime after5year = Dt.plusyears (5);



6. Take special dates

Java code
    1. DateTime dt = new DateTime ();
    2. Month End Date
    3. DateTime Lastday = Dt.dayofmonth (). Withmaximumvalue ();
    4. 90 days later, Monday of that week.
    5. DateTime FirstDay = dt.plusdays (All). DayOfWeek (). Withminimumvalue ();



7. Time Zone

Java code
    1. The default setting is Japan time
    2. Datetimezone.setdefault (Datetimezone.forid ("Asia/tokyo"));
    3. DateTime DT1 = new DateTime ();
    4. London time
    5. DateTime DT2 = new DateTime (Datetimezone.forid ("Europe/london"));



8. Calculation Interval

Java code
  1. DateTime begin = new DateTime ("2012-02-01");
  2. DateTime end = new DateTime ("2012-05-01");
  3. Calculate interval Millisecond Number
  4. Duration d = new Duration (begin, end);
  5. Long time = D.getmillis ();
  6. Calculate Interval Days
  7. Period p = new Period (Begin, End, Periodtype.days ());
  8. int days = P.getdays ();
  9. Calculates whether a specific date is within the range
  10. Interval i = new Interval (begin, end);
  11. Boolean contained = I.contains (new DateTime ("2012-03-01"));



9. Date comparison

Java code
  1. DateTime d1 = new DateTime ("2012-02-01");
  2. DateTime d2 = new DateTime ("2012-05-01");
  3. and system time ratio
  4. Boolean B1 = D1.isafternow ();
  5. Boolean b2 = D1.isbeforenow ();
  6. Boolean b3 = D1.isequalnow ();
  7. and other dates than
  8. Boolean f1 = D1.isafter (D2);
  9. Boolean F2 = D1.isbefore (D2);
  10. Boolean f3 = D1.isequal (D2);



10. Formatted output

Java code
    1. datetime datetime = new DateTime ();
    2. String S1 = datetime.tostring ("Yyyy/mm/dd hh:mm:ss.  SSSa ");
    3. String s2 = datetime.tostring ("Yyyy-mm-dd HH:mm:ss");
    4. String s3 = datetime.tostring ("eeee dd MMMM, yyyy HH:mm:ssa");
    5. String S4 = datetime.tostring ("Yyyy/mm/dd hh:mm ZZZZ");
    6. String S5 = datetime.tostring ("Yyyy/mm/dd hh:mm Z");



Jodd's Jdatetime also offers a very good time API.

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

Joda-time of Java Date calculation

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.