Java Date Computing, Formatting Tool Class

Java Date Computing, Formatting Tool Class

Preface

Use Date calculations when writing your project, such as getting data for the last month, to calculate the date 30 days ago from the current date, then place it in SQL for queries. Although I have calculated before, I always forget that I need to search every time I use it, so I want to do a tool class instead of searching all the time, and I can also deepen this image, so I have this blog.

Date calculation

I'm calculating with time stamps. The Date class has a getTime() method, which is to get a time stamp. The time stamp you get here is in milliseconds. For example, to calculate a date 30 days ago, we just need to subtract the current time stamp from the 30 days converted in milliseconds (1000*60*24*30 ms). The code is as follows:

public static Date getDateMonthAgo(Date date) {
	return new Date(date.getTime() - 1000L*60*60*24*30);
}

Note: When calculating a timestamp, it is important to have a value of long type. Otherwise, the default subtraction result is an int type value, which may cause overflow problems and then become negative. You will find that the time calculated is not 30 days ago, but 30 days later.

So one day is 1000*60*60*24, one week is 1000*60*60*24*7, and so on. Just count the number of days before you change the minus sign to the plus sign.
Or, like me, you can define all these ghosts as a constant:

/* 1 second */
private static final long SECOND = 1000L;

/* 1 branch */
private static final long MINUTE = 1000*60L;

/* 1 hour */
private static final long HOUR = 1000*60*60L;

/* 1 day */
private static final long DAY = 1000*60*60*24L;

/* 1 week */
private static final long WEEK = 1000*60*60*24*7L;

/* 1 Months */
private static final long MONTH = 1000*60*60*24*30L;

/* 1 year */
private static final long YEAR = 1000*60*60*24*365L;

This is a better way to write it, you should:

/**
 * Get the date after days
 * @param date Incoming Date
 * @param days Days
 * @return days Date after day
 */
public static Date getDateDaysAfter(Date date, int days) {
	return new Date(date.getTime() + DAY * days);
}

Date Formatting

There's a class coming on at this point, SimpleDateFormat
We can use this class to define the format of the date and time, and use the methods inside to convert Date to the defined format

	public static String getDateFormatTest(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy year MM month dd day HH time mm branch ss second");
		return sdf.format(date);
	}

Codedescribe
yyyyParticular year
yyTwo after the year
MMMonth
MMMChinese capitalized month
ddWhat number
HH24-hour system
hh12-hour system
mmMinute
sssecond
msMillisecond

Of course, these formats can also be defined as constant calls that are done:

/**
 * Date format
 * You can continue to add custom formats with 24-hour hh and 12-hour hh
 */
private static final String DATE_FORMAT_CHINESE = "yyyy year MM month dd day";

private static final String DATETIME_FORMAT_CHINESE = "yyyy year MM month dd day HH time mm branch ss second";

private static final String DATE_FORMAT_WITH_LINE = "yyyy-MM-dd";

private static final String DATETIME_FORMAT_WITH_LINE = "yyyy-MM-dd HH:mm:ss";

private static final String DATE_FORMAT_WITH_SLASH  = "yyyy/MM/dd";

private static final String DATETIME_FORMAT_WITH_SLASH = "yyyy/MM/dd HH:mm:ss";

private static final String DATE_FORMAT  = "yyyyMMdd";

private static final String DATETIME_FORMAT = "yyyyMMdd HHmmss";

Ending

When I was about to finish this tool class, I suddenly wondered why Date's getDays() method was not recommended, so I saw new Java8 features: LocalDate, LocalTime, LocalDateTime, DateFormatter...
Originally, the SimpleDateFormat thread was not safe. Maybe Date did nothing wrong during the conversion, so we got several new classes out, and those classes were very useful. With their own calculation methods, I split them on the spot. However, there are still some Dates for the project, so you can still survive. If you have a new project, use these new categories directly. DateFormatter is thread-safe

This tool class code

package icu.sanjin.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Date Computing and Formatting Tool Classes
 * @author Sanjin
 * @since 2021-12-28 14:40
 * <p>Personal blog: https://blog.csdn.net/qq_16186465 </p>
 * Note: Date and SimpleDateFormat are obsolete classes because SimpleDateFormat threads are insecure, baking, and learning something new
 * JAVA8 New features include classes for processing time such as LocalDate and DateFormatter
 * Now the parties are very sorry, but they have written it all and can't waste it. Hey hey
 */
public class DateUtil {

	/* 1 second */
	private static final long SECOND = 1000L;

	/* 1 branch */
	private static final long MINUTE = 1000*60L;

	/* 1 hour */
	private static final long HOUR = 1000*60*60L;

	/* 1 day */
	private static final long DAY = 1000*60*60*24L;

	/* 1 week */
	private static final long WEEK = 1000*60*60*24*7L;

	/* 1 Months */
	private static final long MONTH = 1000*60*60*24*30L;

	/* 1 year */
	private static final long YEAR = 1000*60*60*24*365L;

	/**
	 * Date format
	 * You can continue to add custom formats with 24-hour hh and 12-hour hh
	 */
	private static final String DATE_FORMAT_CHINESE = "yyyy year MM month dd day";

	private static final String DATETIME_FORMAT_CHINESE = "yyyy year MM month dd day HH time mm branch ss second";

	private static final String DATE_FORMAT_WITH_LINE = "yyyy-MM-dd";

	private static final String DATETIME_FORMAT_WITH_LINE = "yyyy-MM-dd HH:mm:ss";

	private static final String DATE_FORMAT_WITH_SLASH  = "yyyy/MM/dd";

	private static final String DATETIME_FORMAT_WITH_SLASH = "yyyy/MM/dd HH:mm:ss";

	private static final String DATE_FORMAT  = "yyyyMMdd";

	private static final String DATETIME_FORMAT = "yyyyMMdd HHmmss";

	/**
	 * Get tomorrow's date
	 * @param date Incoming Date
	 * @return Tomorrow's date
	 */
	public static Date getDateTomorrow(Date date) {
		return new Date(date.getTime() + DAY);
	}

	/**
	 * Get Yesterday's Date
	 * @param date Incoming Time
	 * @return Date of Yesterday
	 */
	public static Date getDateYesterday(Date date) {
		return new Date(date.getTime() - DAY);
	}

	/**
	 * Get the date day before
	 * @param date Incoming Date
	 * @param days Days
	 * @return days Date days ago
	 */
	public static Date getDateDaysAgo(Date date, int days) {
		return new Date(date.getTime() - DAY * days);
	}

	/**
	 * Get the date after days
	 * @param date Incoming Date
	 * @param days Days
	 * @return days Date after day
	 */
	public static Date getDateDaysAfter(Date date, int days) {
		return new Date(date.getTime() + DAY * days);
	}

	/**
	 * Get the date before weeks
	 * @param date Incoming Date
	 * @param weeks Weeks
	 * @return weeks Date before week
	 */
	public static Date getDateWeeksAgo(Date date, int weeks) {
		return new Date(date.getTime() - WEEK * weeks);
	}

	/**
	 * Get the date after weeks week
	 * @param date Incoming Date
	 * @param weeks Weeks
	 * @return weeks Date after week
	 */
	public static Date getDateWeeksAfter(Date date, int weeks) {
		return new Date(date.getTime() + WEEK * weeks);
	}

	/**
	 * Get the date before the month
	 * @param date Incoming Date
	 * @param months Number of months
	 * @return months Date before month
	 */
	public static Date getDateMonthsAgo(Date date, int months) {
		return new Date(date.getTime() - MONTH * months);
	}

	/**
	 * Get the date after the month
	 * @param date Incoming Date
	 * @param months Number of months
	 * @return months Date after month
	 */
	public static Date getDateMonthsAfter(Date date, int months) {
		return new Date(date.getTime() + MONTH * months);
	}

	/**
	 * Get the date before years
	 * @param date Incoming Date
	 * @param years number of years
	 * @return years Date before year
	 */
	public static Date getDateAYearsAgo(Date date, int years) {
		return new Date(date.getTime() - YEAR * years);
	}

	/**
	 * Get the date after years year
	 * @param date Incoming Date
	 * @param years number of years
	 * @return years Date after year
	 */
	public static Date getDateAYearsAfter(Date date, int years) {
		return new Date(date.getTime() + YEAR * years);
	}

	/**
	 * Date to get Chinese format
	 * @param date Incoming Date
	 * @return Date in Chinese format
	 */
	public static String getDateFormatChinese(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_CHINESE);
		return sdf.format(date);
	}

	/**
	 * Get the date and time in Chinese format
	 * @param date Incoming Date Time
	 * @return Date and time in Chinese format
	 */
	public static String getDatetimeFormatChinese(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT_CHINESE);
		return sdf.format(date);
	}

	/**
	 * Get Use-Separated Dates
	 * @param date Incoming Date
	 * @return Separated Dates
	 */
	public static String getDateFormatWithLine(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_WITH_LINE);
		return sdf.format(date);
	}

	/**
	 * Get date and time separated by
	 * @param date Incoming Date Time
	 * @return Separated Date Time
	 */
	public static String getDatetimeFormatWithLine(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT_WITH_LINE);
		return sdf.format(date);
	}

	/**
	 * Get Used/Separated Dates
	 * @param date Incoming Date
	 * @return Dates Separated/Separated
	 */
	public static String getDateFormatWithSlash(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_WITH_SLASH);
		return sdf.format(date);
	}

	/**
	 * Get date and time separated
	 * @param date Incoming Date Time
	 * @return Date Time Separated
	 */
	public static String getDatetimeFormatWithSlash(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT_WITH_SLASH);
		return sdf.format(date);
	}

	/**
	 * Get the date of the year, month, day in a pool
	 * @param date Incoming Date
	 * @return The date of the first month of the year
	 */
	public static String getDateFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		return sdf.format(date);
	}

	/**
	 * Gets the date time of the year, month, day, hour, second at a time
	 * @param date Incoming Date Time
	 * @return Date time of year, month, day, hour, second in a pool
	 */
	public static String getDatetimeFormat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT);
		return sdf.format(date);
	}

	/**
	 * Customize Date Time Format
	 * @param date Incoming Date Time
	 * @param dateFormat Customize Date Time Format
	 * @return Custom Date Time
	 */
	public static String getDateFormatYourself(Date date, String dateFormat) {
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		return sdf.format(date);
	}

	/**
	 * Divide time, get all parts directly
	 * @param date Incoming Time
	 * @return All parts of time
	 */
	public static Map<String, String> splitDate(Date date) {
		Map<String, String> map = new HashMap<>();
		map.put("second", getDateFormatYourself(date, "ss"));
		map.put("minute", getDateFormatYourself(date, "mm"));
		map.put("hour", getDateFormatYourself(date, "HH"));
		map.put("day", getDateFormatYourself(date, "dd"));
		map.put("month", getDateFormatYourself(date, "MM"));
		map.put("year", getDateFormatYourself(date, "yyyy"));
		return map;
	}

	/**
	 * Get seconds
	 * @param date Incoming Time
	 * @return Seconds
	 */
	public static String getSecond(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("ss");
		return sdf.format(date);
	}

	/**
	 * Get Minutes
	 * @param date Incoming Time
	 * @return Minute
	 */
	public static String getMinute(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("mm");
		return sdf.format(date);
	}

	/**
	 * Get Hours
	 * @param date Incoming Time
	 * @return hour
	 */
	public static String getHour(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("hh");
		return sdf.format(date);
	}

	/**
	 * Get Number of Numbers
	 * @param date Incoming Time
	 * @return Number of numbers
	 */
	public static String getDay(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		return sdf.format(date);
	}

	/**
	 * Get Months
	 * @param date Incoming Time
	 * @return Month
	 */
	public static String getMonth(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		return sdf.format(date);
	}

	/**
	 * Get Year
	 * @param date Incoming Time
	 * @return Particular year
	 */
	public static String getYear(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		return sdf.format(date);
	}
}

Keywords: Java Back-end

Added by onicsoft on Sat, 01 Jan 2022 22:40:12 +0200