io.github.nichetoolkit.rest.helper.DateHelper Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.helper;
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.util.GeneralUtils;
import lombok.extern.slf4j.Slf4j;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* DateHelper
* The date helper class.
* @author Cyan ([email protected])
* @see lombok.extern.slf4j.Slf4j
* @see java.lang.SuppressWarnings
* @since Jdk1.8
*/
@Slf4j
@SuppressWarnings("unused")
public class DateHelper {
/**
* 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.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.Long
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
public static String formatDate(Long date) throws ResourceNotFoundException {
return formatDate(new Date(date));
}
/**
* 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.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.Long
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
public static String formatTime(Long time) throws ResourceNotFoundException {
return formatTime(new Date(time));
}
/**
* 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.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.util.Date
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
public static String formatDate(Date date) throws ResourceNotFoundException {
return format(date, DateConstants.DATE_FORMAT_10);
}
/**
* 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.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.util.Date
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
public static String formatTime(Date time) throws ResourceNotFoundException {
return format(time, DateConstants.DATE_FORMAT_19);
}
/**
* 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.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.util.Date
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
public static String format(Date date, String format) throws ResourceNotFoundException {
return cacheDateFormat(format).format(date);
}
/**
* 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.
* @throws ParseErrorException {@link io.github.nichetoolkit.rest.error.natives.ParseErrorException} The parse error exception is ParseErrorException
type.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.String
* @see java.util.Date
* @see io.github.nichetoolkit.rest.error.natives.ParseErrorException
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
public static Date parseDate(String date) throws ParseErrorException, ResourceNotFoundException {
return parseFormat(date, DateConstants.DATE_FORMAT_10);
}
/**
* 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.
* @throws ParseErrorException {@link io.github.nichetoolkit.rest.error.natives.ParseErrorException} The parse error exception is ParseErrorException
type.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.String
* @see java.util.Date
* @see java.lang.Deprecated
* @see io.github.nichetoolkit.rest.error.natives.ParseErrorException
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
* @deprecated The parse date time method has be deprecated.
*/
@Deprecated
public static Date parseDateTime(String datetime) throws ParseErrorException, ResourceNotFoundException {
return parseFormat(datetime, DateConstants.DATE_FORMAT_19);
}
/**
* 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.
* @throws ParseErrorException {@link io.github.nichetoolkit.rest.error.natives.ParseErrorException} The parse error exception is ParseErrorException
type.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.String
* @see java.util.Date
* @see io.github.nichetoolkit.rest.error.natives.ParseErrorException
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
public static Date parseTime(String time) throws ParseErrorException, ResourceNotFoundException {
return parseFormat(time, DateConstants.DATE_FORMAT_19);
}
/**
* 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.
* @throws ParseErrorException {@link io.github.nichetoolkit.rest.error.natives.ParseErrorException} The parse error exception is ParseErrorException
type.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.String
* @see java.util.Date
* @see io.github.nichetoolkit.rest.error.natives.ParseErrorException
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
public static Date parse(String datetime, String format) throws ParseErrorException, ResourceNotFoundException {
return parseFormat(datetime, format);
}
/**
* parseFormat
* The parse format 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 format return object is Date
type.
* @throws ParseErrorException {@link io.github.nichetoolkit.rest.error.natives.ParseErrorException} The parse error exception is ParseErrorException
type.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.String
* @see java.util.Date
* @see io.github.nichetoolkit.rest.error.natives.ParseErrorException
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
private static Date parseFormat(String datetime, String format) throws ParseErrorException, ResourceNotFoundException {
if (GeneralUtils.isEmpty(format)) {
format = switchDateFormat(datetime);
}
try {
return cacheDateFormat(format).parse(datetime);
} catch (Exception exception) {
throw new ParseErrorException(exception.getMessage());
}
}
/**
* switchDateFormat
* The switch date format method.
* @param datetime {@link java.lang.String} The datetime parameter is String
type.
* @return {@link java.lang.String} The switch date format return object is String
type.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.String
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
private static String switchDateFormat(String datetime) throws ResourceNotFoundException {
int length = datetime.length();
switch (length) {
case 10:
return DateConstants.DATE_FORMAT_10;
case 14:
return DateConstants.DATE_FORMAT_14;
case 17:
return DateConstants.DATE_FORMAT_17;
case 19:
return datetime.contains(DateConstants.MINUS) ? DateConstants.DATE_FORMAT_19 : DateConstants.DATE_FORMAT_19_FORWARD_SLASH;
case 23:
return datetime.contains(DateConstants.MINUS) ? DateConstants.DATE_FORMAT_23 : DateConstants.DATE_FORMAT_23_FORWARD_SLASH;
case 11:
case 12:
case 13:
case 15:
case 16:
case 18:
return DateConstants.DATE_FORMAT_18;
default:
throw new ResourceNotFoundException(datetime, "can not find date format pattern!");
}
}
/**
* cacheDateFormat
* The cache date format method.
* @param format {@link java.lang.String} The format parameter is String
type.
* @return {@link java.text.DateFormat} The cache date format return object is DateFormat
type.
* @throws ResourceNotFoundException {@link io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException} The resource not found exception is ResourceNotFoundException
type.
* @see java.lang.String
* @see java.text.DateFormat
* @see io.github.nichetoolkit.rest.error.supply.ResourceNotFoundException
*/
private static DateFormat cacheDateFormat(String format) throws ResourceNotFoundException {
if (GeneralUtils.isEmpty(format)) {
throw new ResourceNotFoundException("format", "format is not null!");
}
Map formatMap = DateConstants.DATE_FORMAT_THREAD_LOCAL.get();
SimpleDateFormat formatCached;
if (formatMap == null) {
formatMap = new HashMap<>();
DateConstants.DATE_FORMAT_THREAD_LOCAL.set(formatMap);
} else {
formatCached = (formatMap).get(format);
if (formatCached != null) {
return formatCached;
}
}
formatCached = new SimpleDateFormat(format);
formatMap.put(format, formatCached);
return formatCached;
}
}