Today in the spring official demo to see the Joda-time, so go to Joda-time official website to understand a bit, instantly by its powerful function and ease of use of the admiration. Joda-time Introduction
joda-time-Alternative selection of date/Time libraries for Java applications, Joda-time makes time and date values easier to manage, manipulate, and understand. In fact, easy to use is the main design goal of Joda. Other goals include scalability, a complete set of features, and support for multiple calendar systems. And Joda is completely interoperable with JDK, so you don't have to replace all the Java code, just the part of the code that performs date/time calculations. Joda-time Use
1. Create a casual moment of time--for example, December 21, 2015 0:0
2. Format Time Output
datetime datetime = new DateTime (2015, 0, 0, 0, 333);
System.out.println (DateTime.ToString ("Yyyy/mm/dd HH:mm:ss EE"));
3. Parse Text Format time
DateTimeFormatter format = DateTimeFormat. Forpattern ("Yyyy-mm-dd HH:mm:ss");
datetime datetime = DateTime.Parse ("2015-12-21 23:22:45", format);
System.out.println (DateTime.ToString ("Yyyy/mm/dd HH:mm:ss EE"));
4. Add 90 days to a date and output the result
datetime datetime = new DateTime (2016, 1, 1, 0, 0, 0, 0);
System.out.println (datetime.plusdays). toString ("E mm/dd/yyyy HH:mm:ss. SSS ");
5. How many more days till the New Year's Day
Public days Daystonewyear (Localdate fromdate) {
localdate newyear = fromdate.plusyears (1). Withdayofyear (1);
Return Days.daysbetween (FromDate, newyear);
}
6. Conversion to JDK date objects
DateTime dt = new DateTime ();
Convert to Java.util.Date object
Date D1 = new Date (Dt.getmillis ());
7. Time Zone
The default setting is Japanese time
datetimezone.setdefault (Datetimezone.forid ("Asia/tokyo"));
DateTime DT1 = new DateTime ();
System.out.println (dt1.tostring ("Yyyy-mm-dd HH:mm:ss"));
London time
datetime DT2 = new DateTime (Datetimezone.forid ("Europe/london"));
System.out.println (dt2.tostring ("Yyyy-mm-dd HH:mm:ss"));
8. Calculation interval and interval
DateTime begin = New DateTime ("2015-02-01");
DateTime end = new DateTime ("2016-05-01");
Calculates the interval millisecond
Duration d = new Duration (begin, end);
Long Millis = 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 interval
Interval Interval = new Interval (begin, end);
9. Date Comparison
datetime d1 = new DateTime ("2015-10-01");
DateTime d2 = new DateTime ("2016-02-01");
and system time than
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);
The following is a simple demo to combat, enter a date (birthday, format: yyyy-mm-dd Or yyyy-mm-dd HH:mm:ss), to calculate today is your life number of days/hours/minutes/sec, the code is as follows:
Package Com.ricky.spring.springdemo;
Import Org.joda.time.DateTime;
Import Org.joda.time.Days;
Import org.joda.time.Hours;
Import org.joda.time.Minutes;
Import Org.joda.time.Seconds;
Import Org.joda.time.format.DateTimeFormat;
Import Org.joda.time.format.DateTimeFormatter;
Import Java.util.Scanner;
public class Dayoflife {public static void main (string[] args) {new Dayoflife (). Caldaynumber ();
public void Caldaynumber () {Scanner Scanner = new Scanner (system.in); try{while (true) {System.out.println ("********* Please enter a birthday (yyyy-mm-dd Or yyyy-mm-dd HH:mm:ss) *******
**");
String line = Scanner.nextline ();
if ("Exit". Equalsignorecase (line)) {break;
} datetimeformatter format = null;
if (Line.length () ==10) {format = DateTimeFormat. Forpattern ("Yyyy-mm-dd"); }else{format = DAtetimeformat. Forpattern ("Yyyy-mm-dd HH:mm:ss");
DateTime startdatetime = null;;
try {startdatetime = DateTime.Parse (line, format);
catch (Exception e) {System.err.println ("Input format error, please re-enter birthday!");
Continue
} caldays (StartDateTime, New DateTime ());
}}finally{Scanner.close (); } private void Caldays (DateTime startdatetime,datetime enddatetime) {days = Days.daysbetween (STA
Rtdatetime, EndDateTime);
System.out.println ("Today is the first of your Life" +days.getdays () + "Day");
Hours Hours = Hours.hoursbetween (StartDateTime, enddatetime);
System.out.println ("Today is the first of your Life" +hours.gethours () + "Hour");
Minutes Minutes = Minutes.minutesbetween (StartDateTime, enddatetime);
System.out.println ("Today is the first of your Life" +minutes.getminutes () + "Minutes"); Seconds Seconds = Seconds.secoNdsbetween (StartDateTime, enddatetime);
System.out.println ("Today is the first of your Life" +seconds.getseconds () + "seconds");
}
}
Reference Link:
http://www.joda.org/joda-time/quickstart.html
http://www.ibm.com/developerworks/cn/java/ j-jodatime.html