Foreword
When writing the background program, the timestamp of the current server is often required. It is also convenient to use the timestamp. The client and the server can make their own conversion according to their own needs
In PHP, obtain the current timestamp and use the Time () function, and the format output can pass through the Date () function. It is relatively complicated in Java. Here we briefly introduce
Calendar, Dateformat in the Java language form a basic but important part of the Java standard. Date is a key part of the business logic calculation. All developers should be able to calculate the future date, the customized date display format, and analyze the text data into the date object
Get unix timestamp
In JDK1.0, the Date class is the only class that represents time, but because the Date class is not convenient to achieve internationalization, starting from the JDK 1.1 version, it is recommended to use the Calendar class for time and date. Here we briefly introduce how to get the current timestamp with the Date class
The current date and time of the system create a date object and return a long integer. This time is usually called the system time of the Java virtual machine (JVM) host environment. The unit is milliseconds.
Import Java.util.date; Public Class Timetest {Public Static Void Main (String ARGS []) {Date time = New Date (); System.out.println (Time.gettim () / 1000); // 1387258105 system. out.println (time.tostring ()); // Tue DEC 17 13:28:25 cst 2013}}
Formatting date
The format of the date () function customization date data can be used in php. The SimpleDateFormat class needs to be called in Java, such as formatting the current time format output:
Import Java.Text.simpleDateFormat; Import Java.util.date; Public Class Timetest {Public Static Void Main (String ARGS []) {Date Time = New Date (); em.out.println (time.gettime () / 1000 ) ;/ 1387260201 SimpleDateFormat SDF = New SimpleDateFormat ("Yyyy-MM-DD HH: SS: SS"); // 2013-03-17 14:03:21 string str = sdf.Format (time); System.out .println (str);}}
Perky text into date objects
Given the formatting time string, such as "2013-12-17 14:05:59", which needs to be converted to Date object to facilitate time stamps to obtain other formatting operations. You can continue to call the SimpleDateFormat class
Import Java.Text.parsexception; Import Java.text.SimpleDateFormat; Import Java.util.date; Public Class TimeTest {PUBLIC Static Void Main (String AR gs []) {string text = "2013-12-17 14:05:59 "; SimpleDateformat SDF = New SimpleDateFormat (" Yyyy-MM-DD HH: MM: SS "); Try {date time = sdf.parse (text); System.out.println (time.gettime () / 100 0);} Catch (PAREEEXCEPTION E) {System.out.println (e.getMessage ());}}}
The specific part of the acquisition date
Through the two classes: DateDateFormat, we can already achieve the current timestamp, the date format output, the formatted date string to the Date object function, now there is a new demand, how to obtain the specific part of the date, such as the current current current Hours, the current number of days, etc., this requires the Calendar class
Import java.util.calendar; Import Java.util.date; Import Java.util.gregorianCalendar; Public Class Timetest {Public Static Void Main (String ARGS []) {Date date = new date (); gregorianCalendar gcalendar = new gregoriancalendar ( );; int Year = gcalendar.get (calendar.year); int Month = gcalendar.month); int days = gcalendar.DET (CALENDAR.DAY _OF_MONTH); int Hour = gcalendar. Get (CALEENDAR.HOUR_OF_DAY); int Minute = gcalendar.get (calendar.minute); int second = gcalendar.Get (Calendar.minute); System.out.println (year + "- " + Month +"-" + Day + "" + Hour + ":" + Minute + ":" + Second);}}
Calculate the number of days between the two dates
For example, if calculation differences between April 1, 2010 and March 11, 2009, you can use time and date processing for calculation.
The principle of the implementation of this program is: first represents two specific time points. Here is the object of the Calendar to represent the representative, and then convert the two time points to the corresponding relative time, find the difference between the relative time point in two points, and then divide the division You can get the corresponding number of days with 1 day's milliseconds (24 hours x60 minutes x60 seconds x1000 milliseconds). The complete code of this example is as follows:
Import java.util.*;/*** Calculate the number of days between the two dates*/public class datexample1 {Public Static Void Main (String [] ARGS) {// Set two Date: 2009 3rd: 2009 3rd Calendar C1 = Calendar.getInstance (); C1.Set (2009, 3-1, 11); // Date: Calendar C2 = Calendar.getInstance (); C2.Set (2010, 2010, 4-1, 1); // Convert to relative time long t1 = c1.gettimeinmillis (); long t2 = c2.gettimeinmillis (); // Calculate the number of days loong days = (t2 -t1)/(24 * 60 * 60 * 1000); system.out.println (days);}}
Output the monthly calendar of the current month
The function of this example is the calendar of the month of the current system time. For example, the current system time is March 10, 2009, and the calendar in March 2009 was output.
The principle of the procedure is: first to get the week of the month 1, and then the number of days of the month, and finally use the process control to implement the output in the format of the calendar. That is, if No. 1 is Monday, the space of a unit is printed. If the No. 1 is Tuesday, the two units of the two units are printed and pushed in order. After printing the date of Saturday, change the bank. The complete code of this example is as follows:
Import java.util.*;/*** Output the current month's calendar*/Public Class datexample2 {Public Static Void Main (String [] ARGS) {// Get the current time Calendar.getInstance (); // Settings The date of the representative is C.SET (Calendar.date, 1); // Obtain the number 1 is int Start = c.Get (Calendar.Day_of_week); // Get the maximum number of current month Int maxday = C. .getActualMaximum (Calendar.date); // Outlet Title System.out.println System.out.print ("");} // Output all the date for (int i = 1; i <= maxday; i ++) {// output date systerm.print (""+i ); // Output separate spaces System.out.print (""); if (i <10) {system.out.print ('');} // Determine whether to change if ((Start + I - 1) % 7 == 0) {system.out.println ();}} // change the system.out.println ();}}