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
- Method One: Take the system between points
- DateTime DT1 = new DateTime ();
- Method Two: Generate by Java.util.Date object
- DateTime DT2 = new DateTime (new Date ());
- Method Three: Specify the day of the month and the second generation (the parameters are: years, months, days, hours, minutes, seconds, milliseconds)
- DateTime DT3 = new DateTime (5, 0, 0);
- Method Four: ISO8601 form generation
- DateTime DT4 = new DateTime ("2012-05-20");
- DateTime DT5 = new DateTime ("2012-05-20t13:14:00");
- It only takes years and days
- Localdate localdate = new Localdate ( 9, 6); September 6,
- It just takes a minute, seconds, minutes.
- LocalTime localtime = new LocalTime ( 0); 1:30:26pm
2, get the date of the day and seconds.
Java code
- DateTime dt = new DateTime ();
- Years
- int year = Dt.getyear ();
- Month
- int month = Dt.getmonthofyear ();
- Day
- int day = Dt.getdayofmonth ();
- Week
- Int week = Dt.getdayofweek ();
- Point
- int hour = Dt.gethourofday ();
- Score of
- int min = Dt.getminuteofhour ();
- Seconds
- int sec = Dt.getsecondofminute ();
- Milliseconds
- int msec = Dt.getmillisofsecond ();
3, the special treatment of the week
Java code
- DateTime dt = new DateTime ();
- Week
- Switch (Dt.getdayofweek ()) {
- Case Datetimeconstants.sunday:
- System.out.println ("Sunday");
- Break ;
- Case Datetimeconstants.monday:
- System.out.println ("Monday");
- Break ;
- Case Datetimeconstants.tuesday:
- System.out.println ("Tuesday");
- Break ;
- Case Datetimeconstants.wednesday:
- System.out.println ("Wednesday");
- Break ;
- Case Datetimeconstants.thursday:
- System.out.println ("Thursday");
- Break ;
- Case Datetimeconstants.friday:
- System.out.println ("Friday");
- Break ;
- Case Datetimeconstants.saturday:
- System.out.println ("Saturday");
- Break ;
- }
4. Conversion to JDK Date Object
Java code
- DateTime dt = new DateTime ();
- Convert to Java.util.Date Object
- Date D1 = new Date (Dt.getmillis ());
- Date D2 = Dt.todate ();
- Convert to Java.util.Calendar Object
- Calendar C1 = Calendar.getinstance ();
- C1.settimeinmillis (Dt.getmillis ());
- Calendar C2 = Dt.tocalendar (Locale.getdefault ());
5, the date before and after the calculation
Java code
- DateTime dt = new DateTime ();
- Yesterday
- DateTime yesterday = dt.minusdays (1);
- Tomorrow
- DateTime tomorrow = Dt.plusdays (1);
- 1 months ago
- DateTime before1month = dt.minusmonths (1);
- 3 months later
- DateTime after3month = dt.plusmonths (3);
- 2 years ago
- DateTime before2year = Dt.minusyears (2);
- 5 years later
- DateTime after5year = Dt.plusyears (5);
6. Take special dates
Java code
- DateTime dt = new DateTime ();
- Month End Date
- DateTime Lastday = Dt.dayofmonth (). Withmaximumvalue ();
- 90 days later, Monday of that week.
- DateTime FirstDay = dt.plusdays (All). DayOfWeek (). Withminimumvalue ();
7. Time Zone
Java code
- The default setting is Japan time
- Datetimezone.setdefault (Datetimezone.forid ("Asia/tokyo"));
- DateTime DT1 = new DateTime ();
- London time
- DateTime DT2 = new DateTime (Datetimezone.forid ("Europe/london"));
8. Calculation Interval
Java code
- DateTime begin = new DateTime ("2012-02-01");
- DateTime end = new DateTime ("2012-05-01");
- Calculate interval Millisecond Number
- Duration d = new Duration (begin, end);
- Long time = D.getmillis ();
- Calculate Interval Days
- Period p = new Period (Begin, End, Periodtype.days ());
- int days = P.getdays ();
- Calculates whether a specific date is within the range
- Interval i = new Interval (begin, end);
- Boolean contained = I.contains (new DateTime ("2012-03-01"));
9. Date comparison
Java code
- DateTime d1 = new DateTime ("2012-02-01");
- DateTime d2 = new DateTime ("2012-05-01");
- and system time ratio
- Boolean B1 = D1.isafternow ();
- Boolean b2 = D1.isbeforenow ();
- Boolean b3 = D1.isequalnow ();
- and other dates than
- Boolean f1 = D1.isafter (D2);
- Boolean F2 = D1.isbefore (D2);
- Boolean f3 = D1.isequal (D2);
10. Formatted output
Java code
- datetime datetime = new DateTime ();
- String S1 = datetime.tostring ("Yyyy/mm/dd hh:mm:ss. SSSa ");
- String s2 = datetime.tostring ("Yyyy-mm-dd HH:mm:ss");
- String s3 = datetime.tostring ("eeee dd MMMM, yyyy HH:mm:ssa");
- String S4 = datetime.tostring ("Yyyy/mm/dd hh:mm ZZZZ");
- 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