整理DateUtils工具类
This commit is contained in:
parent
f108949052
commit
9c6f9ebda4
|
|
@ -8,6 +8,7 @@ import java.time.LocalDateTime;
|
||||||
import java.time.LocalTime;
|
import java.time.LocalTime;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import org.apache.commons.lang3.time.DateFormatUtils;
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
||||||
|
|
||||||
|
|
@ -23,6 +24,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
||||||
public static String YYYY_MM = "yyyy-MM";
|
public static String YYYY_MM = "yyyy-MM";
|
||||||
|
|
||||||
public static String YYYY_MM_DD = "yyyy-MM-dd";
|
public static String YYYY_MM_DD = "yyyy-MM-dd";
|
||||||
|
public static String YYYY_MM_DD_PATH = "yyyy/MM/dd";
|
||||||
|
|
||||||
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
||||||
|
|
||||||
|
|
@ -34,7 +36,43 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
||||||
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
|
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前Date型日期
|
* 获取年(4位数字)
|
||||||
|
* @param date
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int getYear(Date date)
|
||||||
|
{
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
return calendar.get(Calendar.YEAR);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取月份(1 - 12)
|
||||||
|
* @param date
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int getMonth(Date date)
|
||||||
|
{
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
return calendar.get(Calendar.MONTH) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取天数(一个月内)
|
||||||
|
* @param date
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static int getDayOfMonth(Date date)
|
||||||
|
{
|
||||||
|
Calendar calendar = Calendar.getInstance();
|
||||||
|
calendar.setTime(date);
|
||||||
|
return calendar.get(Calendar.DAY_OF_MONTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前 Date型日期
|
||||||
*
|
*
|
||||||
* @return Date() 当前日期
|
* @return Date() 当前日期
|
||||||
*/
|
*/
|
||||||
|
|
@ -44,45 +82,46 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前日期, 默认格式为yyyy-MM-dd
|
* 获取当前日期, 默认格式为 yyyy-MM-dd
|
||||||
*
|
*
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
public static String getDate()
|
public static String getDateNow()
|
||||||
{
|
{
|
||||||
return dateTimeNow(YYYY_MM_DD);
|
return parseDateToStr(YYYY_MM_DD, new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final String getTime()
|
/**
|
||||||
|
* 获取当前日期时间, 默认格式为 yyyy-MM-dd HH:mm:ss
|
||||||
|
*
|
||||||
|
* @return String
|
||||||
|
*/
|
||||||
|
public static final String getDateTimeNow()
|
||||||
{
|
{
|
||||||
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
|
return parseDateToStr(YYYY_MM_DD_HH_MM_SS, new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final String dateTimeNow()
|
/**
|
||||||
{
|
* 获取当前日期时间, 按照指定格式
|
||||||
return dateTimeNow(YYYYMMDDHHMMSS);
|
*
|
||||||
}
|
* @param format 格式
|
||||||
|
* @return String
|
||||||
public static final String dateTimeNow(final String format)
|
*/
|
||||||
|
public static final String getDateTimeNow(final String format)
|
||||||
{
|
{
|
||||||
return parseDateToStr(format, new Date());
|
return parseDateToStr(format, new Date());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final String dateTime(final Date date)
|
|
||||||
{
|
|
||||||
return parseDateToStr(YYYY_MM_DD, date);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final String parseDateToStr(final String format, final Date date)
|
public static final String parseDateToStr(final String format, final Date date)
|
||||||
{
|
{
|
||||||
return new SimpleDateFormat(format).format(date);
|
return new SimpleDateFormat(format).format(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Date dateTime(final String format, final String ts)
|
public static final Date parseStrToDate(final String format, final String str)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return new SimpleDateFormat(format).parse(ts);
|
return new SimpleDateFormat(format).parse(str);
|
||||||
}
|
}
|
||||||
catch (ParseException e)
|
catch (ParseException e)
|
||||||
{
|
{
|
||||||
|
|
@ -90,24 +129,6 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 日期路径 即年/月/日 如2018/08/08
|
|
||||||
*/
|
|
||||||
public static final String datePath()
|
|
||||||
{
|
|
||||||
Date now = new Date();
|
|
||||||
return DateFormatUtils.format(now, "yyyy/MM/dd");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 日期路径 即年/月/日 如20180808
|
|
||||||
*/
|
|
||||||
public static final String dateTime()
|
|
||||||
{
|
|
||||||
Date now = new Date();
|
|
||||||
return DateFormatUtils.format(now, "yyyyMMdd");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 日期型字符串转化为日期 格式
|
* 日期型字符串转化为日期 格式
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -122,7 +122,7 @@ public class FileUploadUtils
|
||||||
*/
|
*/
|
||||||
public static final String extractFilename(MultipartFile file)
|
public static final String extractFilename(MultipartFile file)
|
||||||
{
|
{
|
||||||
return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
|
return StringUtils.format("{}/{}_{}.{}", DateUtils.getDateTimeNow(DateUtils.YYYY_MM_DD_PATH),
|
||||||
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
|
FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), getExtension(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,7 +90,7 @@ public class FileUtils
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
String extension = getFileExtendName(data);
|
String extension = getFileExtendName(data);
|
||||||
pathName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + "." + extension;
|
pathName = DateUtils.getDateTimeNow(DateUtils.YYYY_MM_DD_PATH) + "/" + IdUtils.fastUUID() + "." + extension;
|
||||||
File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName);
|
File file = FileUploadUtils.getAbsoluteFile(uploadDir, pathName);
|
||||||
fos = new FileOutputStream(file);
|
fos = new FileOutputStream(file);
|
||||||
fos.write(data);
|
fos.write(data);
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ public class Seq
|
||||||
*/
|
*/
|
||||||
public static String getId(AtomicInteger atomicInt, int length)
|
public static String getId(AtomicInteger atomicInt, int length)
|
||||||
{
|
{
|
||||||
String result = DateUtils.dateTimeNow();
|
String result = DateUtils.getDateTimeNow(DateUtils.YYYYMMDDHHMMSS);
|
||||||
result += machineCode;
|
result += machineCode;
|
||||||
result += getSeq(atomicInt, length);
|
result += getSeq(atomicInt, length);
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ public class VelocityUtils
|
||||||
velocityContext.put("basePackage", getPackagePrefix(packageName));
|
velocityContext.put("basePackage", getPackagePrefix(packageName));
|
||||||
velocityContext.put("packageName", packageName);
|
velocityContext.put("packageName", packageName);
|
||||||
velocityContext.put("author", genTable.getFunctionAuthor());
|
velocityContext.put("author", genTable.getFunctionAuthor());
|
||||||
velocityContext.put("datetime", DateUtils.getDate());
|
velocityContext.put("datetime", DateUtils.getDateNow());
|
||||||
velocityContext.put("pkColumn", genTable.getPkColumn());
|
velocityContext.put("pkColumn", genTable.getPkColumn());
|
||||||
velocityContext.put("importList", getImportList(genTable));
|
velocityContext.put("importList", getImportList(genTable));
|
||||||
velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
|
velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue