io.github.nichetoolkit.rest.util.DateUtils Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.util;
import io.github.nichetoolkit.rest.constant.DateConstants;
import io.github.nichetoolkit.rest.error.natives.ParseErrorException;
import io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException;
import io.github.nichetoolkit.rest.helper.DateHelper;
import lombok.extern.slf4j.Slf4j;
import java.util.Calendar;
import java.util.Date;
/**
* DateUtils
* The date utils class.
* @author Cyan ([email protected])
* @see lombok.extern.slf4j.Slf4j
* @see java.lang.SuppressWarnings
* @since Jdk1.8
*/
@Slf4j
@SuppressWarnings("unused")
public class DateUtils {
/**
* formatDate
* The format date method.
* @param date {@link java.lang.Long} The date parameter is Long
type.
* @return {@link java.lang.String} The format date return object is String
type.
* @see java.lang.Long
* @see java.lang.String
*/
public static String formatDate(Long date) {
try {
return DateHelper.formatDate(date);
} catch (ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date format date has error!date: {},format: {},error: {}", date.toString(), DateConstants.DATE_FORMAT_10, exception.getMessage());
return null;
}
}
/**
* formatTime
* The format time method.
* @param time {@link java.lang.Long} The time parameter is Long
type.
* @return {@link java.lang.String} The format time return object is String
type.
* @see java.lang.Long
* @see java.lang.String
*/
public static String formatTime(Long time) {
try {
return DateHelper.formatTime(time);
} catch (ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date format time has error!time: {},format: {},error: {}", time.toString(), DateConstants.DATE_FORMAT_19, exception.getMessage());
return null;
}
}
/**
* formatDate
* The format date method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @return {@link java.lang.String} The format date return object is String
type.
* @see java.util.Date
* @see java.lang.String
*/
public static String formatDate(Date date) {
try {
return DateHelper.formatDate(date);
} catch (ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date format date has error! date: {},format: {},error: {}", date.toString(), DateConstants.DATE_FORMAT_10, exception.getMessage());
return null;
}
}
/**
* formatTime
* The format time method.
* @param time {@link java.util.Date} The time parameter is Date
type.
* @return {@link java.lang.String} The format time return object is String
type.
* @see java.util.Date
* @see java.lang.String
*/
public static String formatTime(Date time) {
try {
return DateHelper.formatTime(time);
} catch (ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date format time has error! time: {},format: {},error: {}", time.toString(), DateConstants.DATE_FORMAT_19, exception.getMessage());
return null;
}
}
/**
* format
* The format method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param format {@link java.lang.String} The format parameter is String
type.
* @return {@link java.lang.String} The format return object is String
type.
* @see java.util.Date
* @see java.lang.String
*/
public static String format(Date date, String format) {
try {
return DateHelper.format(date, format);
} catch (ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date format has error!date: {},format: {},error: {}", date.toString(), format, exception.getMessage());
return null;
}
}
/**
* parseDate
* The parse date method.
* @param date {@link java.lang.String} The date parameter is String
type.
* @return {@link java.util.Date} The parse date return object is Date
type.
* @see java.lang.String
* @see java.util.Date
*/
public static Date parseDate(String date) {
try {
return DateHelper.parseDate(date);
} catch (ParseErrorException | ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date parse date has error!date: {},format: {},error: {}", date, DateConstants.DATE_FORMAT_10, exception.getMessage());
return null;
}
}
/**
* parseDateTime
* The parse date time method.
* @param datetime {@link java.lang.String} The datetime parameter is String
type.
* @return {@link java.util.Date} The parse date time return object is Date
type.
* @see java.lang.String
* @see java.util.Date
* @see java.lang.Deprecated
* @deprecated The parse date time method has be deprecated.
*/
@Deprecated
public static Date parseDateTime(String datetime) {
try {
return DateHelper.parseDateTime(datetime);
} catch (ParseErrorException | ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date parse date time has error!time: {},format: {},error: {}", datetime, DateConstants.DATE_FORMAT_19, exception.getMessage());
return null;
}
}
/**
* parseTime
* The parse time method.
* @param time {@link java.lang.String} The time parameter is String
type.
* @return {@link java.util.Date} The parse time return object is Date
type.
* @see java.lang.String
* @see java.util.Date
*/
public static Date parseTime(String time) {
try {
return DateHelper.parseTime(time);
} catch (ParseErrorException | ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date parse time has error!time: {},format: {},error: {}", time, DateConstants.DATE_FORMAT_19, exception.getMessage());
return null;
}
}
/**
* parse
* The parse method.
* @param datetime {@link java.lang.String} The datetime parameter is String
type.
* @param format {@link java.lang.String} The format parameter is String
type.
* @return {@link java.util.Date} The parse return object is Date
type.
* @see java.lang.String
* @see java.util.Date
*/
public static Date parse(String datetime, String format) {
try {
return DateHelper.parse(datetime, format);
} catch (ParseErrorException | ResourceNotFoundException exception) {
GeneralUtils.printStackTrace(exception);
log.error("date parse has error!date time: {},format: {},error: {}", datetime, format, exception.getMessage());
return null;
}
}
/**
* today
* The today method.
* @return {@link java.util.Date} The today return object is Date
type.
* @see java.util.Date
*/
public static Date today() {
Calendar calendar = Calendar.getInstance();
clear(calendar);
return calendar.getTime();
}
/**
* getDay
* The get day getter method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @return {@link java.util.Date} The get day return object is Date
type.
* @see java.util.Date
*/
public static Date getDay(final Date date) {
if (date == null) {
return null;
}
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
clear(calendar);
return calendar.getTime();
}
/**
* getMonth
* The get month getter method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @return {@link java.util.Date} The get month return object is Date
type.
* @see java.util.Date
*/
public static Date getMonth(final Date date) {
if (date == null) {
return null;
}
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
clear(calendar);
return calendar.getTime();
}
/**
* getYear
* The get year getter method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @return {@link java.util.Date} The get year return object is Date
type.
* @see java.util.Date
*/
public static Date getYear(final Date date) {
if (date == null) {
return null;
}
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);
clear(calendar);
return calendar.getTime();
}
/**
* getDateInt
* The get date int getter method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param field int The field parameter is int
type.
* @return {@link java.lang.Integer} The get date int return object is Integer
type.
* @see java.util.Date
* @see java.lang.Integer
*/
public static Integer getDateInt(final Date date, int field) {
if (date == null) {
return null;
}
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if (field == Calendar.MONTH) {
return calendar.get(field) + 1;
} else {
return calendar.get(field);
}
}
/**
* addYears
* The add years method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param amount int The amount parameter is int
type.
* @return {@link java.util.Date} The add years return object is Date
type.
* @see java.util.Date
*/
public static Date addYears(final Date date, final int amount) {
return add(date, Calendar.YEAR, amount);
}
/**
* addMonths
* The add months method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param amount int The amount parameter is int
type.
* @return {@link java.util.Date} The add months return object is Date
type.
* @see java.util.Date
*/
public static Date addMonths(final Date date, final int amount) {
return add(date, Calendar.MONTH, amount);
}
/**
* addDays
* The add days method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param amount int The amount parameter is int
type.
* @return {@link java.util.Date} The add days return object is Date
type.
* @see java.util.Date
*/
public static Date addDays(final Date date, final int amount) {
return add(date, Calendar.DAY_OF_MONTH, amount);
}
/**
* addHours
* The add hours method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param amount int The amount parameter is int
type.
* @return {@link java.util.Date} The add hours return object is Date
type.
* @see java.util.Date
*/
public static Date addHours(final Date date, final int amount) {
return add(date, Calendar.HOUR_OF_DAY, amount);
}
/**
* addMinutes
* The add minutes method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param amount int The amount parameter is int
type.
* @return {@link java.util.Date} The add minutes return object is Date
type.
* @see java.util.Date
*/
public static Date addMinutes(final Date date, final int amount) {
return add(date, Calendar.MINUTE, amount);
}
/**
* addSeconds
* The add seconds method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param amount int The amount parameter is int
type.
* @return {@link java.util.Date} The add seconds return object is Date
type.
* @see java.util.Date
*/
public static Date addSeconds(final Date date, final int amount) {
return add(date, Calendar.SECOND, amount);
}
/**
* clear
* The clear method.
* @param calendar {@link java.util.Calendar} The calendar parameter is Calendar
type.
* @see java.util.Calendar
*/
private static void clear(Calendar calendar) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
/**
* add
* The add method.
* @param date {@link java.util.Date} The date parameter is Date
type.
* @param field int The field parameter is int
type.
* @param amount int The amount parameter is int
type.
* @return {@link java.util.Date} The add return object is Date
type.
* @see java.util.Date
*/
private static Date add(final Date date, final int field, final int amount) {
if (date == null) {
return null;
}
final Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(field, amount);
return calendar.getTime();
}
}