Java most time class and usage, java time class usage
For the time class, this article mainly describes how to set values in various real situations, how to set values, and how to get the desired time parameters. In java, the time classes mainly include Date and Calendar. Currently, only java is introduced. util. * Time class, for java. SQL. * I will not introduce the following content first, but I will add it in another article later.
First of all, we need to understand the introduction to the API so that we can understand why this is written during use? Instead of simply copying and pasting jobs.
For Date,Many methods have been migrated to the Calendar class out of date, but this is the first time class. The constructor mainly introduces two methods:
Date date = new Date (); // allocate the object and start the statement
Date date = newDate(Long date) // allocateDateObject and initialize this object to indicate the specified number of milliseconds since the standard reference time (called epoch), that is, January 1, 1970 00:00:00 GMT.
It can be seen that Date is based on calendar elements, and you can simply verify your conjecture. The value of new Date (). getYear () is 117, but this year is 2017.
For CalendarDifferent from Date, It offsets the time element of Date. If it looks a bit dizzy in the brackets, don't look at it. API (CalendarA class is an abstract class that corresponds to a groupYEAR,MONTH,DAY_OF_MONTH,HOURAnd so onCalendar FieldProvides some methods for converting between fields, and provides some methods for manipulating calendar fields (for example, obtaining the date of the next week. It can be expressed in milliseconds instantly.Yuan(That is, the offset of 00:00:00. 000, gregali calendar, January 1, January 1, 1970, Greenwich Mean Time ).
CalendarI have to say that it is a class method.GetInstance,CalendarOfgetInstanceMethod returnsCalendarThe calendar field of the object has been initialized by the current date and time:
Calendar rightNow = Calendar. getInstance ();
You must know this method. Otherwise, what do you do with Calendar?
Well, next we will introduce various practical things that need to be handled. You must be looking for them on your blog.
X // how to obtain the current time Parameter,Run yyyy-MM-dd hh: mm: ss, yyyy-MM-dd, yyyy-MM-dd hh: mm, yyyyMMdd, and so on.
public static String getCurrentTime() {Date NowDate = new Date();SimpleDateFormat formatter =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");CurrentTime = formatter.format(NowDate);return CurrentTime;}
//How to obtain the first and last days of a month,The first day is certainly easy, and the last day is actually very easy. It mainly uses the Calendar and SimpleDateFormat classes. It should be noted that the month is 12 months in total by 0---11, not from 1, however, the day of a month starts from day 1. The following is the encapsulation of the two methods. The test results are shown in the figure below.
public static String getLastDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DATE)); return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime()); } public static String getFirstDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE)); return new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime()); }
//How do I obtain the month, year, and current date of the current month?It is relatively simple without testing.
/* Obtain the date of the current day */
Public static String getCurrentDay () {java. util. date NowDate = new java. util. date (); SimpleDateFormat formatter = new SimpleDateFormat ("dd"); return formatter. format (NowDate);}/* Get month */public static String getCurrentMonth () {java. util. date NowDate = new java. util. date (); SimpleDateFormat formatter = new SimpleDateFormat ("MM"); return formatter. format (NowDate);}/* Get the current year */public static String getCurrentYear () {java. util. date NowDate = new java. util. date (); SimpleDateFormat formatter = new SimpleDateFormat ("yyyy"); return formatter. format (NowDate );}
I would like to continue to summarize it. I will come here today and work overtime.