Page Search Box Date Search Criteria Data to Background Type Conversion (Timestamp)

The date type defined by the background database is Long, which is stored as a time stamp; the date type of the search box on the page is String.The String and Long types of dates are not equal on time stamps!

I show you how to convert date types in my code, and you can understand the difference between debug and timestamp converters.

 

 

1. First in the page search box (search box only, other displays and add-delete change checks are defined as Long), in order to distinguish, the Long type date is set to beginTime, endTime by default, and the String type date is set to beginTimeString, endTimeString

 

2. Background database and Mapper.xml Date type of query criteria

 

 

 

3. InQueryReq.javaFour data types are defined in the class and converted (the class accepts the parameters passed by the search box)

 

Customize the date conversion method (partial code, see the first five methods available here):

  1 /**
  2      * String Format Time Conversion My UnixTime, e.g. 2014-8-12 Conversion results do not include milliseconds
  3      */
  4     public static long convertDateToTimestamp(String datetime, String format)
  5             throws ParseException {
  6         return getFormatter(format.trim()).parse(datetime.trim()).getTime() / 1000;
  7     }
  8 
  9     /**
 10      * String Format Time Conversion My UnixTime starts at 00:00:00
 11      */
 12     public static long convertStartDateToTimestamp(String datetime,
 13             String format) throws ParseException {
 14         datetime = datetime + " 00:00:00";
 15         return getFormatter(format.trim()).parse(datetime.trim()).getTime() / 1000;
 16     }
 17 
 18     /**
 19      * String Format Time Conversion My UnixTime starts at 00:00:00
 20      */
 21     public static Long convertStartDateToTimestamp(String datetime) {
 22         datetime = datetime + " 00:00:00";
 23         try {
 24             return getFormatter("yyyy-MM-dd HH:mm:ss").parse(datetime.trim()).getTime() / 1000;
 25         } catch (ParseException e) {
 26             logger.error("ERROR:", e);
 27             return null;
 28         }
 29 
 30     }
 31 
 32     /**
 33      * String Format Time Conversion My UnixTime ends at 23:59:59
 34      */
 35     public static long convertEndDateToTimestamp(String datetime, String format)
 36             throws ParseException {
 37         datetime = datetime + " 23:59:59";
 38         return getFormatter(format.trim()).parse(datetime.trim()).getTime() / 1000;
 39     }
 40 
 41     /**
 42      * String Format Time Conversion My UnixTime ends at 23:59:59
 43      */
 44     public static Long convertEndDateToTimestamp(String datetime) {
 45 
 46         datetime = datetime + " 23:59:59";
 47         try {
 48             return getFormatter("yyyy-MM-dd HH:mm:ss").parse(datetime.trim()).getTime() / 1000;
 49         } catch (ParseException e) {
 50             logger.error("ERROR:", e);
 51             return null;
 52         }
 53     }
 54 
 55     /**
 56      * Date Turn timestamp conversion results excluding milliseconds
 57      */
 58     public static long convertDateToTimestamp(Date datetime)
 59             throws ParseException {
 60         // SimpleDateFormat formater=getFormatter(format.trim());
 61         // return formater.parse(formater.format(datetime)).getTime();
 62         return datetime.getTime() / 1000;
 63     }
 64 
 65     /*
 66      * String Format Time Conversion I UnixTime, e.g. 8-12, 2014 This method is not used yet
 67      */
 68     @SuppressWarnings("unused")
 69     private static long convertDateToLong(String datetime, String format)
 70             throws ParseException {
 71         return getFormatter(format.trim()).parse(datetime.trim()).getTime();
 72     }
 73 
 74     /*
 75      * Date Trans-timestamp This method is not used at this time
 76      */
 77     @SuppressWarnings("unused")
 78     private static long convertDateToLong(Date datetime) throws ParseException {
 79         // SimpleDateFormat formater=getFormatter(format.trim());
 80         // return formater.parse(formater.format(datetime)).getTime();
 81         return datetime.getTime();
 82     }
 83 
 84     /*
 85      * UnixTime Convert to String
 86      */
 87     public static String convertTimestampToDate(long time, String format) {
 88         return getFormatter(format.trim()).format(new Date(time * 1000)).trim();
 89     }
 90 
 91     /*
 92      * UnixTime Convert to Date
 93      */
 94     public static Date convertTimestampToDate(long time) {
 95         // SimpleDateFormat formater=getFormatter(format.trim());
 96         // Date date=null;
 97         // try {
 98         // date= formater.parse(formater.format(time));
 99         // } catch (ParseException e) {
100         // e.printStackTrace();
101         // }
102         return new Date(time * 1000);
103     }
104 
105     /*
106      * UnixTime Convert to String
107      */
108     public static String convertLongToDate(long time, String format) {
109         return getFormatter(format.trim()).format(new Date(time)).trim();
110     }
111 
112     /*
113      * UnixTime Convert to String
114      */
115     public static String convertDateToDateString(Date date, String format) {
116         return getFormatter(format.trim()).format(date).trim();
117     }
118 
119     /*
120      * UnixTime Convert to Date
121      */
122     @SuppressWarnings("unused")
123     private static Date convertLongToDate(long time) {
124         // SimpleDateFormat formater=getFormatter(format.trim());
125         // Date date=null;
126         // try {
127         // date= formater.parse(formater.format(time));
128         // } catch (ParseException e) {
129         // e.printStackTrace();
130         // }
131         return new Date(time);
132     }
133 
134     /*
135      * String Convert Date
136      */
137     public static Date ConvertDate(String time, String format) {
138         try {
139             SimpleDateFormat formater = getFormatter(format.trim());
140             return formater.parse(time);
141         } catch (ParseException e) {
142             logger.error("ERROR:", e);
143             return null;
144         }
145     }
146 
147     /**
148      * Get the current time stamp without milliseconds
149      *
150      * @return time stamp
151      */
152     public static long currentTimestamp() {
153         return System.currentTimeMillis() / 1000;
154     }
155 
156     public static long getTodayDateOfTimestamp() {
157         long t = System.currentTimeMillis() / 1000 / 60 / 60 / 24;
158         return t * 24 * 3600;
159 
160     }
161 
162     // public static long getNextWeekDateOfLong() {
163     // calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
164     // calendar.add(Calendar.WEEK_OF_YEAR, 1);
165     // Date d = calendar.getTime();
166     // return calendar.getTimeInMillis() / 1000;
167     //
168     // }
169 
170     public static long getNextMonthDateOfTimestamp(long thisMonth) {
171         Calendar calendar = Calendar.getInstance();
172         Date thisMonthDate = new Date(thisMonth * 1000);
173         calendar.setTime(thisMonthDate);
174         calendar.add(Calendar.MONTH, 1);
175         // Date d = calendar.getTime();
176         return calendar.getTimeInMillis() / 1000;
177 
178     }
179 
180     public static long getNextIntervalTimeOfTimestamp(long thisDay, int days) {
181         Calendar calendar = Calendar.getInstance();
182         Date thisDayDate = new Date(thisDay * 1000);
183         calendar.setTime(thisDayDate);
184         calendar.add(Calendar.DATE, days);
185         calendar.set(Calendar.HOUR_OF_DAY, 24);
186         calendar.set(Calendar.SECOND, 0);
187         calendar.set(Calendar.MINUTE, 0);
188         calendar.set(Calendar.MILLISECOND, 0);
189         return calendar.getTimeInMillis() / 1000;
190 
191     }
192 
193     public static long getSpecifiedMonthDateOfTimestamp(long thisMonth,
194             int amount) {
195         Calendar calendar = Calendar.getInstance();
196         Date thisMonthDate = new Date(thisMonth * 1000);
197         calendar.setTime(thisMonthDate);
198         calendar.add(Calendar.MONTH, amount);
199         // Date d = calendar.getTime();
200         return calendar.getTimeInMillis() / 1000;
201 
202     }
203 
204     public static long getLastDayOfThisYear() {
205         Calendar calendar = Calendar.getInstance();
206         Date d = new Date();
207         calendar.setTime(d);
208 
209         calendar.set(calendar.get(Calendar.YEAR), 12, 31, 23, 59, 59);
210         return calendar.getTimeInMillis() / 1000;
211     }
212 
213     public static int getCurrentMonth() {
214         Calendar calendar = Calendar.getInstance();
215         return calendar.get(Calendar.MONTH);
216     }

 

 

4. After that, get the data in the service implementation class to implement the query:

 

5. Page display results

 

 

Keywords: Java Database xml

Added by zahadum on Mon, 08 Jun 2020 19:15:00 +0300