Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.swak.frame.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
/**
* The type Date time utils.
*/
@Slf4j
public final class DateTimeUtils {
/**
* String 2 date.
*
* @param strtime the strtime
* @param datepattern the datepattern
* @return the date
*/
public static Date string2Date(String strtime, String datepattern) {
if (StringUtils.isEmpty(strtime)) {
return null;
}
try {
DateTimeFormatter format = DateTimeFormat.forPattern(datepattern);
DateTime dateTime = DateTime.parse(strtime, format);
return dateTime.toDate();
} catch (Exception e) {
log.error(e.getMessage());
}
return null;
}
/**
* String 2 date.
*
* @param strtime the strtime
* @return the date
*/
public static Date string2Date(String strtime) {
int index = strtime.indexOf(":");
String datepattern = null;
if (index != -1) {
datepattern = DFormatEnum.YYYY_MM_DDHH_MM_SS.pattern;
} else {
datepattern = DFormatEnum.YYYY_MM_DD.pattern;
}
return string2Date(strtime, datepattern);
}
/**
* String 2 calendar calendar.
*
* @param strtime the strtime
* @return the calendar
*/
public static Calendar string2Calendar(String strtime) {
Date d = string2Date(strtime);
if (d != null) {
Calendar c = Calendar.getInstance();
c.setTime(d);
return c;
}
return null;
}
/**
* String 2 timestamp timestamp.
*
* @param strtime the strtime
* @param datepattern the datepattern
* @return the timestamp
*/
public static Timestamp string2Timestamp(String strtime, String datepattern) {
Date date = string2Date(strtime, datepattern);
if (date != null) {
return new Timestamp(date.getTime());
}
return null;
}
/**
* Calendar 2 string string.
*
* @param time the time
* @return the string
*/
public static String calendar2String(Calendar time) {
return date2String(time.getTime());
}
/**
* Date 2 string string.
*
* @param time the time
* @param datepattern the datepattern
* @return the string
*/
public static String date2String(Date time, String datepattern) {
if (StringUtils.isEmpty(datepattern)) {
datepattern = DFormatEnum.DATA_FORMAT_DEFAULT;
}
return new DateTime(time).toString(datepattern);
}
/**
* Date 2 string string.
*
* @param time the time
* @param datepattern the datepattern
* @return the string
*/
public static String date2String(Timestamp time, String datepattern) {
if (StringUtils.isEmpty(datepattern)) {
datepattern = DFormatEnum.DATA_FORMAT_DEFAULT;
}
return new DateTime(time.getTime()).toString(datepattern);
}
/**
* Date 2 string string.
*
* @param time the time
* @return the string
*/
public static String date2String(Date time) {
return date2String(time, null);
}
/**
* Date 2 string string.
*
* @param time the time
* @return the string
*/
public static String date2String(Timestamp time) {
return date2String(time, null);
}
/**
* Sys time str string.
*
* @param datepattern the datepattern
* @return the string
*/
public static String sysTimeStr(String datepattern) {
return date2String(new Date(), datepattern);
}
/**
* Sys time str string.
*
* @return the string
*/
public static String sysTimeStr() {
return sysTimeStr(null);
}
/**
* Yyyymmdd 2 string string.
*
* @return the string
*/
public static String YYYYMMDD2String() {
return YYYYMMDD2String(new Date());
}
/**
* Yyyymmdd 2 string string.
*
* @param date the date
* @return the string
*/
public static String YYYYMMDD2String(Date date) {
return date2String(date, DFormatEnum.YYYYMMDD.pattern);
}
/**
* Full date 2 string string.
*
* @param date the date
* @return the string
*/
public static String fullDate2String(Date date) {
return date2String(date, DFormatEnum.YYYY_MM_DDHH_MM_SS.pattern);
}
/**
* Formate short date string.
*
* @param date the date
* @return the string
*/
public static String formateShortDate(Date date) {
return date2String(date, DFormatEnum.YYYY_MM_DD.pattern);
}
/**
* Gets short date str.
*
* @return the short date str
*/
public static String getShortDateStr() {
return date2String(new Date(), DFormatEnum.YYYY_MM_DD.pattern);
}
/**
* Gets last time day.
*
* @param date the date
* @return the last time day
*/
public static Date getLastTimeDay(Date date) {
String dateTemp = date2String(date, DFormatEnum.YYYY_MM_DD.pattern);
return string2Date(dateTemp + " 23:59:59");
}
/**
* Week of year int.
*
* @param date the date
* @param gap the gap
* @return the int
*/
public static int weekOfYear(Date date, Integer gap) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.WEEK_OF_YEAR, gap);
return Integer.valueOf(
String.valueOf(calendar.get(Calendar.YEAR)) + String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR)));
}
/**
* Gets current time.
*
* @return the current time
*/
public static Timestamp getCurrentTime() {
return new Timestamp(System.currentTimeMillis());
}
/**
* Reset time 2 zero date.
*
* @param date the date
* @return the date
*/
public static Date resetTime2Zero(Date date) {
if (date == null) {
return date;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
/**
* The enum D format enum.
*/
public enum DFormatEnum {
/**
* Yyyymmdd d format enum.
*/
YYYYMMDD("yyyyMMdd"),
/**
* Yyyymmddhhmmss d format enum.
*/
YYYYMMDDHHMMSS("yyyyMMddHHmmss"),
/**
* Yymmddhhmmss d format enum.
*/
YYMMDDHHMMSS("yyMMddHHmmss"),
/**
* The Yyyy mm ddhh mm ss.
*/
YYYY_MM_DDHH_MM_SS("yyyy-MM-dd HH:mm:ss"),
/**
* Yyyy mm dd d format enum.
*/
YYYY_MM_DD("yyyy-MM-dd"),
/**
* Yyyymmdd dot d format enum.
*/
YYYYMMDD_DOT("yyyy.MM.dd"),
/**
* Mmdd dot d format enum.
*/
MMDD_DOT("MM.dd"),
/**
* Yymmdd d format enum.
*/
YYMMDD("yyMMdd"),
/**
* Hhmmss d format enum.
*/
HHMMSS("HHmmss"),
/**
* Hh mm ss d format enum.
*/
HH_MM_SS("HH:mm:ss"),
/**
* Yyyymm d format enum.
*/
YYYYMM("yyyyMM"),
/**
* Yyyy mm d format enum.
*/
YYYY_MM("yyyy-MM"),
/**
* The Yyyymmdd slash full.
*/
YYYYMMDD_SLASH_FULL("yyyy/MM/dd HH:mm:ss"),
/**
* Yyyymmdd slash d format enum.
*/
YYYYMMDD_SLASH("yyyy/MM/dd"),
/**
* Yymm d format enum.
*/
YYMM("yyMM");
/**
* The Pattern.
*/
public String pattern;
DFormatEnum(String pattern) {
this.pattern = pattern;
}
private static DFormatEnum getDefaultEnum() {
return YYYY_MM_DDHH_MM_SS;
}
/**
* The constant DATA_FORMAT_DEFAULT.
*/
public static String DATA_FORMAT_DEFAULT = getDefaultEnum().pattern;
}
}