Summarize the commonly used time dependent transformations in Java

  • 2021-10-15 10:28:50
  • OfStack

Time-dependent conversions commonly used in Java

Basic explanation of 1 variables in the following code
datePattern: Time-corresponding string format
date: Time
dateStr: Time in String Format
Several constants specified:


public static final long DAYTIMESTAMP = 24 * 60 * 60 * 1000L;
public static final  String SHORTDATEFORMATER = "yyyy-MM-dd";
public static final  String LONGDATEFORMATER = "yyyy-MM-dd HH:mm:ss";

1. Convert time to a string in the specified format


public static final String convertDateToString(String datePattern, Date date) {
		String returnValue = null;
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(datePattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}

2. String transition time in the specified format


public static final Date convertStringToDate(String datePattern,String dateStr) {
		if( StringUtils.isBlank(dateStr) ){
			return null;
		}
		SimpleDateFormat df = null;
		Date date = null;
		df = new SimpleDateFormat(datePattern);
		try {
			date = df.parse(dateStr);
		} catch (ParseException pe) {
			log.error(" Anomaly ![{}]",pe);
			return null;
		}
		return (date);
	}

3. Determine if the date has not expired


public static final boolean isNonExpired(Date date){
		Calendar calendarNow = Calendar.getInstance();
		calendarNow.setTime(calendarNow.getTime());
		Calendar calendarGiven = Calendar.getInstance();
		calendarGiven.setTime(date);
		return calendarNow.before(calendarGiven);
	}

4. Determine whether the date is overdue


public static final boolean isExpired(Date date){
		Calendar calendarNow = Calendar.getInstance();
		calendarNow.setTime(calendarNow.getTime());
		Calendar calendarGiven = Calendar.getInstance();
		calendarGiven.setTime(date);
		return calendarNow.after(calendarGiven);
	}

5. Determine the size of two dates


public static final int compare( Date firstDate,Date secondDate ){
		return firstDate.compareTo(secondDate);
	}

Note: If the first date parameter is greater than the second date, it will return 1; Returns 0 if two dates are equal; Returns-1 if the first date is less than the second date

6. Get n months before the specified time


public static Date DateMinus(Date date,int month){
		  Calendar calendar = Calendar.getInstance();
		  calendar.setTime(date);
		  calendar.add(Calendar.MONTH, -month);
		return calendar.getTime();
	}

7. Gets the specified day before the specified date, including the 1 day passed in


public static String getDaysBefore(Date date, int days) {
		Date td = new Date(date.getTime() - DAYTIMESTAMP * days);
		return DateUtils.convertDateToString(SHORTDATEFORMATER, td);
	}

8. Gets an array of the specified days before the specified date, containing the 1 day passed in


public static List<String> getDaysBeforeArray(Date date, int days){
		List<String> resultList = new ArrayList<>();
		for (int i = days-1; i >= 0; i--) {
			resultList.add(getDaysBefore(date, i));
		}
		return resultList;
	}

Remarks: Used in conjunction with Article 7

9. Gets the 0 point at the specified time


public static Date getDayStartTimeByDate(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

10. Gets the last 1 second of the specified date


public static final String convertDateToString(String datePattern, Date date) {
		String returnValue = null;
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(datePattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}
0

11. Get the time one hour before the current time


public static final String convertDateToString(String datePattern, Date date) {
		String returnValue = null;
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(datePattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}
1

12. Get the number of minutes between two times


public static final String convertDateToString(String datePattern, Date date) {
		String returnValue = null;
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(datePattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}
2

Remarks: endDate is relatively large in time; nowDate relatively small time; It can be judged at the time of entering the parameter, or it can be optimized in the method, that is, it can be operated according to the return value by calling the fifth operation.

13. Get the number of days between two times


public static int differentDaysByMillisecond(Date date1,Date date2){
		return (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
	}

14. Get a collection of dates between two times


public static final String convertDateToString(String datePattern, Date date) {
		String returnValue = null;
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(datePattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}
4

15. Get the first day at the beginning of the month


public static final String convertDateToString(String datePattern, Date date) {
		String returnValue = null;
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(datePattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}
5

16. Timestamp Formatting


public static final String convertDateToString(String datePattern, Date date) {
		String returnValue = null;
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(datePattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}
6

17. Obtain that today is the n week of the current year


public static final String convertDateToString(String datePattern, Date date) {
		String returnValue = null;
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(datePattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}
7

Remarks: startCalendar refers to the beginning period of the week from the day of the week. For example, if the week of 5 is the beginning of the week, the value of startCalendar is Calendar. FRIDAY

Summary: At present, the commonly used time-related operations are probably these, and those that are not covered can be adjusted through the above related operations. If there are any omissions, please add them in the comments, and I will adjust and increase them in time.


Related articles: