There is often a need to process time in the project. The following are some commonly used operations for easy use in the future and review.
1. Convert string to date
/** * Convert string to date* @param dateStr The date that needs to be converted* @param dateFormat Date format yyyy-MM-dd/yyyy-MM-dd HH:mm:ss */ public static Date toDate(String dateStr, SimpleDateFormat dateFormat) throws ParseException{ Date date = null; try { date = dateFormat.parse(dateStr); } catch (ParseException e) { logger.debug("Fail to convert String to Date, {}", dateStr); } return date; }2. Time stamp to date
/** * Timestamp to date* @param date * @return */ public static String dateToTime(long time, SimpleDateFormat dateFormat) throws ParseException{ String data = null; try { dateFormat.format(new Date(time*1000)); } catch (Exception e) { logger.debug("Fail to convert long to Date, {}", time); } return data; }3. Format date into string
/** * Date is formatted into a string* @param date * @param dateFormat * @return * @throws ParseException */ public static String toString(Date date, SimpleDateFormat dateFormat) throws ParseException{ return dateFormat.format(date); }4. Get the date before or after the specified date, ten seconds are 00:00:00
/** * Get the date before or after the specified date* @param date * @param num The positive number is after, and the negative number is before* @return yyyy-MM-dd 00:00:00 */ public static Date getSpecificDate(Date date, int num){ Calendar todayCal = Calendar.getInstance(); todayCal.setTime(date); Calendar c = Calendar.getInstance(); c.set(todayCal.get(Calendar.YEAR), todayCal.get(Calendar.MONTH), todayCal.get(Calendar.DAY_OF_MONTH) + num, 0, 0, 0); return c.getTime(); }5. Get the date before or after the specified date, the hour, minute and second are the current ones
/** * Get the date before or after the specified date* @param date * @param num The positive number is before and the negative number is after * @return yyyy-MM-dd + current time, minute and second*/ public static Date getSpecificDateAndHhMmSs(Date date,int num){ Calendar c = Calendar.getInstance(); c.setTime(date); int day=c.get(Calendar.DATE); c.set(Calendar.DATE,day - num); return c.getTime(); }6. Convert time strings of time type to time and minutes
/** * Convert time strings of time type to time and minutes* HH-mm-ss -->> HH-mm * @param time * @return */ public static String timeToHHMM(String time){ return time.substring(0, time.length() - 3); }7. Obtain the time and minutes of a certain date
/** * Get the time and minutes of a date* @param date * @return HH-mm */ public static String getHM(Date date){ Calendar ca = Calendar.getInstance(); ca.setTime(date); Integer hour = ca.get(Calendar.HOUR_OF_DAY);//hour Integer minute = ca.get(Calendar.MINUTE);//minute String rs_hour = hour.toString(); String rs_minute = minute.toString(); if (rs_hour.length() == 1){ rs_hour = "0" + hour; } if(rs_minute.length() == 1){ rs_minute = "0" + minute; } return rs_hour + ":" + rs_minute; }8. Time string of type time -->> Number of seconds starting from zero
/** * Time string of time type -->> Number of seconds at the beginning of zero* @param time HH-mm / HH-mm-ss * @return */ public static Integer timeToSeconds(String time){ String[] timeSplit = null; int hours = 0,minutes = 0,seconds = 0; try { timeSplit = time.split(":"); if (timeSplit.length == 2) { hours = Integer.valueOf(timeSplit[0])*60*60; minutes = Integer.valueOf(timeSplit[1])*60; }else if(timeSplit.length == 3){ hours = Integer.valueOf(timeSplit[0])*60*60; minutes = Integer.valueOf(timeSplit[1])*60; seconds = Integer.valueOf(timeSplit[2]); }else{ logger.debug("Fail to convert the time, {}", time); } } catch (Exception e) { logger.debug("Fail to convert the time, {}", time); throw e; } return hours + minutes + seconds; }9. The number of seconds at the beginning of zero point-->> HH-mm-ss
/** * The number of seconds at the beginning of zero point-->> HH-mm-ss * @param durationSeconds * @return */ public static String getDuration(int durationSeconds){ int hours = durationSeconds /(60*60); int leftSeconds = durationSeconds % (60*60); int minutes = leftSeconds / 60; int seconds = leftSeconds % 60; StringBuffer sBuffer = new StringBuffer(); sBuffer.append(addZeroPrefix(hours)); sBuffer.append(":"); sBuffer.append(addZeroPrefix(minutes)); sBuffer.append(":"); sBuffer.append(addZeroPrefix(seconds)); return sBuffer.toString(); } public static String addZeroPrefix(int number){ if(number < 10) return "0"+number; else return ""+number; }10. Comparison of the number of seconds between two dates
/** * Comparison of the number of seconds between the two dates* @param startDate * @param endDate * @return */ public static int getTimeSeconds(Date startDate,Date endDate) { long a = endDate.getTime(); long b = startDate.getTime(); return (int)((a - b) / 1000); }11. Determine whether there is an intersection between two time periods
/** * Comparison of the number of seconds between the two dates* @param startDate * @param endDate * @return */ public static int getTimeSeconds(Date startDate,Date endDate) { long a = endDate.getTime(); long b = startDate.getTime(); return (int)((a - b) / 1000); }12. Get the specified date of the week (1-7 represents Monday to Sunday respectively)
/** * Get the specified date is the day of the week (1-7 represents Monday to Sunday respectively) * @return */ public static int DayOfWeek(Date date){ Calendar now = Calendar.getInstance(); now.setTime(date); boolean isFirstDay = (now.getFirstDayOfWeek() == Calendar.SUNDAY); int weekday = now.get(Calendar.DAY_OF_WEEK); if(isFirstDay){ weekday = weekday - 1; if(weekday == 0){ weekday = 7; } } return weekday; } The above is all the content of this article. I hope that the content of this article will be of some help to everyone’s study or work. I also hope to support Wulin.com more!