Today, when reading the time in SOLR, I find that there is a lot of time format to turn around, and now summarize the time conversion that was encountered in the previous project, here is the main Java class to summarize:
I. Timestamp and Date (java.util.Date)
Date is the parent class of Timestamp!
1.1 Timestamp->date:
// date对象指向的实体是一个timestamp,这是父类引用指向子类对象。publicTimestampToDate(Timestamp timestamp) { new Date(); try { date = timestamp; return date; }catch(Exception e) { e.printStackTrace(); }}
1.2 Date, Timestamp:
//父类不能直接向子类转化,其实很简单;new Timestamp(date.getTime());
two. Timestamp and string Transfer
2.1 Timestamp, String:
One is to direct timestamp.tostring ();
or using DateFormat;
publictimestampToString(Timestamp timestamp) { String tsStr = “”; new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”); try { //方法一 tsStr = timestamp.toString(); //方法二 tsStr = sdf.format(timestamp); return tsStr; catch (Exception e) { e.printStackTrace(); return tsStr; } }
2.2 string-> Timestamp:
Timestamp timestamp = Timestamp.valueOf(tsStr);
Note: The type of string must be in the form of: Yyyy-mm-dd hh:mm:ss[.f ...] format.
three. Date (java.util.Date) and String cross-transfer
3.1 Date, String:
publicdateToString(Date date, String format) { //format: “yyyy/MM/dd HH:mm:ss” String dateStr=""; new SimpleDateFormat(format); try { dateStr = sdf.format(date); return dateStr; catch (Exception e) { e.printStackTrace(); returnnull; } }
3.2 string->date:
publicstringToDate(String dateStr,String format) { new Date(); //注意format的格式要与日期String的格式相匹配 //format1: Locale.US //format: “yyyy/MM/dd HH:mm:ss” //DateFormat sdf1 = new SimpleDateFormat(format1,Locale.US); new SimpleDateFormat(format); try { date = sdf.parse(dateStr); return date; catch (Exception e) { e.printStackTrace(); returnnull; } }
four. Calendar related function Module
4.1 dateutil about part of the calendar:
/** * Get the first day of a one month * @param Month * @param Timestamp * @return */ Public StaticTimestampGetfirstdayofmonth(Calendar calendar) {Calendar.set (calendar.date,1);//Set the first day of the current monthCalendar.set (Calendar.hour_of_day,0); Calendar.set (Calendar.minute,0); Calendar.set (Calendar.second,0);return NewTimestamp (Calendar.gettimeinmillis ()); }/** * Get a start time * @param Month * @param Timestamp * @return */ Public StaticTimestampGetfirsttimeofday(Calendar calendar) {Calendar.set (Calendar.hour_of_day,0); Calendar.set (Calendar.minute,0); Calendar.set (Calendar.second,0);return NewTimestamp (Calendar.gettimeinmillis ()); }/** * Get the last time of day * @param Month * @param Timestamp * @return */ Public StaticTimestampGetlasttimeofday(Calendar calendar) {Calendar.set (Calendar.hour_of_day, at); Calendar.set (Calendar.minute, -); Calendar.set (Calendar.second, -);return NewTimestamp (Calendar.gettimeinmillis ()); }/** * Get the last day of a one month * @param Month * @param Timestamp * @return * * Public StaticTimestampGetlastdayofmonth(Calendar calendar) {Calendar.set (calendar.date,1);//Set number 1th for the current monthCalendar.set (Calendar.hour_of_day, at); Calendar.set (Calendar.minute, -); Calendar.set (Calendar.second, -); Calendar.add (Calendar.month,1);//Add one months to the next month's number 1thCalendar.add (Calendar.date,-1);//Minus one day, becoming the last day of the current month return NewTimestamp (Calendar.gettimeinmillis ()); }/** * Calculates the number of days between two dates * @param costtime * @param receivabletime * @return * / Public Static int getDays(Date end,date start) {Calendar Acalendar = calendar.getinstance (); Calendar Bcalendar = Calendar.getinstance (); Acalendar.settime (end); Bcalendar.settime (start);intDays =0; while(Acalendar.before (Bcalendar)) {days++; Acalendar.add (Calendar.day_of_year,1); }if(days==0) {acalendar.settime (start); Bcalendar.settime (end); while(Acalendar.before (Bcalendar)) {days++; Acalendar.add (Calendar.day_of_year,1); } }returnDays }/** * Calculates the number of months that differ between two months * @param costtime * @param receivabletime * @return * / Public Static int getmonths(Date end,date start) {Calendar Acalendar = calendar.getinstance (); Calendar Bcalendar = Calendar.getinstance (); Acalendar.settime (end); Bcalendar.settime (start);intmonths =0; while(Acalendar.before (Bcalendar)) {months++; Acalendar.add (Calendar.month,1); }if(months==0) {acalendar.settime (start); Bcalendar.settime (end); while(Acalendar.before (Bcalendar)) {months++; Acalendar.add (Calendar.month,1); } }returnMonths }/** * Get the last day of a one month * @param Month * @param Timestamp * @return * * Public StaticTimestampGetlastmonthtime(Calendar calendar) {Calendar.set (Calendar.hour_of_day,0); Calendar.set (Calendar.minute,0); Calendar.set (Calendar.second,0); Calendar.add (Calendar.month,-1);//Minus one months to the beginning of last month return NewTimestamp (Calendar.gettimeinmillis ()); }
4.2 Fixed a point of time every day to start the program:
In addition to the following code, of course, can also be implemented in the spring configuration file, more simple.
Calendar Calendar=Calendar.getinstance (); calendar.Set(Calendar.Hour_of_day,7);//7 in the morning.Calendar.Set(Calendar.MINUTE,0); calendar.Set(Calendar.SECOND,0);Date Date =Calendar.GetTime ();//Time of the first execution of a scheduled task//If the time to perform the first scheduled task is less than the current time//At this point, the time of the first execution of the scheduled task is added one day so that the task executes at the next point in time. If one day is not added, the task executes immediately. if(Date.Before (New Date()) {Calendar Startdt=Calendar.GetInstance (); Startdt.SetTime (Date); Startdt.Add (Calendar.Day_of_month,1);Date =Startdt.GetTime ();} Timer Timer= NewTimer (); Taskmain Task= NewTaskmain ();//taskmain is my task thread.//Schedule The specified task to begin repeating fixed deferred execution at a specified time. Timer.Schedule (Task,Date, Period_day);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java time-dependent classes convert to each other