about the date class in the JDK
java.util.Date
(Date represents a specific moment, accurate to milliseconds)
Before JDK1.1, date allows you to interpret dates as year, month, day, hour, minute, and second values, as well as format and parse date strings.
Starting with JDK1.1, however, you should use the Calendar class to convert between date and Time fields, and use DateFormat to format and parse date strings.
java.sql.Date
(Inherits java.util.Date a thin wrapper with a millisecond value that allows JDBC to identify the millisecond value as a SQL Date value)
From the hierarchy of the above class, the java.util.Date into the java.sql.Date time, the date of the seconds will be removed, the accuracy of the data has changed. This is a very unreasonable problem between the two superior and subordinate classes. In JDBC, when defining interfaces, we use Java.sql.Date, and the Date we use often is java.util.Date, which often leads to errors in the conversion process.
So the Java.sql.Timestamp class was born, and it kept the original precision of date data. can be implemented and java.util.Date lossless conversion. But timestamp this class often goes wrong in some predefined SQL, with an error.
use Joda time instead of Calendar
Compared with Clendar, Joda-time makes time and date values easier to manage, manipulate, and understand.
2.1 JDK and Joda time interoperability
Key date and time characteristics of 2.2 Joda times
Joda-time Calendar Usages comparison Create an instance object, initialize the date/time of the initialization package
You can build a DateTime object in several ways:
Use system time.
Use multiple fields to specify an instantaneous moment (or a local time fragment) to achieve the most fine-grained precision that this particular implementation can support. Specifies an instantaneous moment (or local time fragment), in milliseconds.
Use another object (for example, Java.util.Date, or another Joda object). DateTime
This is one of the most commonly used classes. It encapsulates an instantaneous moment in time at a millisecond-level precision. DateTime is always associated with datetimezone, and if it is not specified, it will be set by default to the time zone of the machine where the code is running.
This constructor uses the system time:
/**
* Generally speaking, you should avoid using the system clock to initialize the actual application, but instead tend to externalized to set the system time used by the application code.
* This makes it easier to test my code with different date/time: I don't need to modify the code to run different date scenarios in the application because the time is set within the <code>SystemClock</code> implementation,
* Rather than inside the application. (Two internal difference is what.) Test time can use mock)
/datetime datetime = new DateTime ();
A DateTime object was built with some field values:
datetime datetime = new DateTime (
watts,//year
1, //month
1, //day
0, //hour (midnight is Zero)
0, //minute
0, //second
0 //milliseconds
);
Specifies the number of milliseconds to elapse from epoch (1970-01-01 00:00:00 UTC) to a certain point in time
Creates a DateTime object based on the millisecond value of the JDK date object, whose time precision is expressed in milliseconds because the epoch is the same as the Joda:
Java.util.Date jdkdate = Obtaindatesomehow ();
Long Timeinmillis = Jdkdate.gettime ();
datetime datetime = new DateTime (Timeinmillis);
Create a DateTime object using Java.util.Date
Java.util.Date jdkdate = Obtaindatesomehow ();
datetime = new DateTime (jdkdate);
Create a DateTime object directly with different objects
Use a calendar
Java.util.Calendar calendar = obtaincalendarsomehow ();
datetime = new DateTime (calendar);
Use another Joda datetime
datetime anotherdatetime = Obtaindatetimesomehow ();
datetime = new DateTime (anotherdatetime);
Use a String (must is formatted properly)
/Note that if you are ready to <code>String</code> (must be parsed), you must format it precisely. The code that parses the string part ...
String timestring = "2006-01-26t13:30:00-06:00";
datetime = new DateTime (timestring);
timestring = "2006-01-26";
datetime = new DateTime (timestring);
date Formatting
to format a Joda object, call its toString () method
datetime datetime = new DateTime ();
DateTime.ToString ("mm/dd/yyyy hh:mm:ss.") Sssa ");
DateTime.ToString ("dd-mm-yyyy HH:mm:ss");
DateTime.ToString ("eeee dd MMMM, yyyy HH:mm:ssa");
DateTime.ToString ("mm/dd/yyyy hh:mm ZZZZ");
DateTime.ToString ("mm/dd/yyyy hh:mm Z");
Use the DateTimeFormatter class to format
DateTimeFormatter format = DateTimeFormat. Forpattern ("Yyyy-mm-dd HH:mm:ss");
Time parsing
DateTime dateTime2 = DateTime.Parse ("2012-12-21 23:22:45", format);
Date Calculation calculate the last day of the one months
Localdate now = new Localdate ();
Localdate Lastdayofpreviousmonth =\
calculates the first Tuesday after the one week of November
Localdate now = Systemfactory.getclock (). Getlocaldate ();
Localdate electiondate = Now.monthofyear ()
. Setcopy (one)/ /November
. DayOfMonth () //Access Day of Month Property
. Withminimumvalue ()//Get It minimum value
. Plusdays (6) //Add 6 days
. DayOfWeek () //Access Day's Week property
. Setcopy ("Monday") //Set to Monday (it'll round down)
. Plusdays (1); c13/>//gives us Tuesday
time span
Duration: This class represents an absolute exact span, using milliseconds. This class provides methods that can be used to convert a time span to a standard unit (such as seconds, minutes, and hours) by means of a standard mathematical transformation (such as 1 minutes = 60 seconds, 1 days = 24 hours).
You only use an instance of Duration in the following situations: you want to convert a time span, but you don't care when the time span occurs, or it's convenient to use millisecond processing time spans. Period: This class represents the same concept as Duration, but it is expressed in a more familiar unit, such as year, month, and week.
You can use Period in the following situations: You are not concerned about when this period must occur, or you are more concerned with the ability to retrieve individual fields that describe the time span that is encapsulated by Period. Interval: This class represents a specific time span, and will use a clear moment to define the span of this period of time. The Interval is a half-open interval, which means that the time span encapsulated by the Interval includes the starting time for this period, but does not include an end time.
You can use Interval when you want to represent a period of time that begins and ends at a specific point in a continuous interval of time