package com.vci.starter.web.util;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import java.sql.Timestamp;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.time.LocalDateTime;
|
import java.time.LocalTime;
|
import java.time.format.DateTimeFormatter;
|
import java.util.*;
|
|
|
/**
|
* 时间工具类
|
* 暂时不用JDK8的日期格式
|
* @author weidy
|
*/
|
public class VciDateUtil {
|
|
/**
|
* 默认日期格式
|
*/
|
private static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
|
|
/**
|
* 日期时间
|
*/
|
public static final String DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
/**
|
* 日期时间-不含符号
|
*/
|
public static final String DateTimeFormatStr = "yyyyMMddHHmmss";
|
|
/**
|
* 日期时间含毫秒,不含符号
|
*/
|
public static final String DateTimeMillFormatStr = "yyyyMMddHHmmssSSS";
|
|
/**
|
* 日期时间含毫秒
|
*/
|
public static final String DateTimeMillFormat = "yyyy-MM-dd HH:mm:ss.SSS";
|
|
/**
|
* 日期格式
|
*/
|
public static final String DateFormat = "yyyy-MM-dd";
|
|
/**
|
* 时间格式
|
*/
|
public static final String TimeFormat = "HH:mm:ss";
|
|
/**
|
* 默认构造函数
|
*/
|
private VciDateUtil() {
|
}
|
|
/**
|
* 字符串转换成日期 如果转换格式为空,则利用默认格式进行转换操作
|
*
|
* @param str
|
* 字符串
|
* @param format
|
* 日期格式
|
* @return 日期
|
* @throws ParseException
|
*/
|
public static Date str2Date(String str, String format) throws Exception {
|
if (null == str || "".equals(str) || str.equals("null")) {
|
return null;
|
}
|
// 如果没有指定字符串转换的格式,则用默认格式进行转换
|
if (null == format || "".equals(format) || format.equals("null")) {
|
format = DEFAULT_FORMAT;
|
}
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
Date date = null;
|
try {
|
date = sdf.parse(str);
|
return date;
|
} catch (ParseException e) {
|
throw new Exception(e);
|
}
|
}
|
|
/**
|
* 获取当前周的周一
|
* @return 周一的日期
|
*/
|
public static Date getCurrentMonday(){
|
return getCurrentWeekDay(2);
|
}
|
|
/**
|
* 获取当前周的某一天
|
* @param dayOfWeek 从周日开始为1,周一为2,一次推算
|
* @return 日期
|
*/
|
public static Date getCurrentWeekDay(int dayOfWeek){
|
if(dayOfWeek>7){
|
dayOfWeek = 7;
|
}
|
if(dayOfWeek<1){
|
dayOfWeek = 1;
|
}
|
Date date = new Date();
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTime(date);
|
calendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
return calendar.getTime();
|
}
|
|
/**
|
* 获取当前周的周五
|
* @return 日期
|
*/
|
public static Date getCurrentFriday(){
|
return getCurrentWeekDay(6);
|
}
|
|
/**
|
* 日期转换为字符串
|
*
|
* @param date
|
* 日期
|
* @param format
|
* 日期格式
|
* @return 字符串
|
*/
|
public static String date2Str(Date date, String format) {
|
if (null == date) {
|
return null;
|
}
|
if (format == null || format.trim().length() == 0) {
|
format = DEFAULT_FORMAT;
|
}
|
SimpleDateFormat sdf = new SimpleDateFormat(format);
|
return sdf.format(date);
|
}
|
|
/**
|
* 时间戳转换为字符串
|
*
|
* @param time 时间戳
|
* @return 字符串
|
*/
|
public static String timestamp2Str(Timestamp time) {
|
Date date = null;
|
if (null == time) {
|
return null;
|
}
|
if (null != time) {
|
date = new Date(time.getTime());
|
}
|
return date2Str(date, DEFAULT_FORMAT);
|
}
|
|
/**
|
* 字符串转换时间戳
|
*
|
* @param str 字符串
|
* @return 时间戳
|
* @throws Exception 字符串的格式出现错误的时候抛出异常
|
*/
|
public static Timestamp str2Timestamp(String str) throws Exception {
|
if (str == null || str.trim().length() == 0) {
|
return null;
|
}
|
Date date = str2Date(str, DEFAULT_FORMAT);
|
return new Timestamp(date.getTime());
|
}
|
|
|
|
/**
|
* 对比两个日期的大小
|
*
|
* @param date 时间对象1
|
* @param date1 时间对象2
|
* @return
|
*/
|
public static String compareDate(String date, String date1)
|
throws Exception {
|
if (date == null || date.trim().length() == 0 || date1 == null
|
|| date1.trim().length() == 0) {
|
throw new Exception("传入compareDate的参数为空");
|
}
|
try {
|
long time = str2Date(date, DateFormat).getTime();
|
long time1 = str2Date(date1, DateFormat).getTime();
|
if (time == time1) {
|
return "=";
|
}
|
if (time < time1) {
|
return "<";
|
}
|
if (time > time1) {
|
return ">";
|
}
|
else {
|
return "";
|
}
|
} catch (Exception e) {
|
throw e;
|
}
|
}
|
|
/**
|
* 时间对比
|
* @param date 日期对象1
|
* @param date1 日期对象2
|
* @return 对比的结果
|
*/
|
public static String compareDate(Date date, Date date1) {
|
if (date == null || date1 == null) {
|
return "";
|
}
|
long time = date.getTime();
|
long time1 = date1.getTime();
|
if (time == time1) {
|
return "=";
|
}
|
if (time < time1) {
|
return "<";
|
}
|
if (time > time1) {
|
return ">";
|
}
|
else {
|
return "";
|
}
|
}
|
|
/**
|
* 在时间戳上加分钟
|
*
|
* @param date 日期字符串
|
* @param minute 分钟
|
* @return 添加后日期值
|
* @throws Exception 日期字符串的格式错误时会抛出错误
|
*/
|
public static String dateTimeAddMinutes(String date, int minute)
|
throws Exception {
|
String ret = "";
|
if (date == null || date.equals("")) {
|
date = date2Str(getNow(), DEFAULT_FORMAT);
|
}
|
if (minute == 0) {
|
ret = date;
|
}
|
else {
|
Date d = VciDateUtil.str2Date(date, DateTimeFormat);
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(d);
|
cal.add(Calendar.MINUTE,minute);
|
return date2Str(cal.getTime(), DateTimeFormat);
|
}
|
return ret;
|
}
|
|
/**
|
* 在时间上加天数
|
*
|
* @param date 日期字符串
|
* @param dayCount 天数,可以为负数
|
* @return 添加后的日期值
|
* @throws Exception 日期字符串的格式错误
|
*/
|
public static Date getDateAddDay(String date, int dayCount)
|
throws Exception {
|
if (date == null || date.equals("") || date.equals("null")) {
|
return getNow();
|
}
|
else {
|
if (dayCount == 0) {
|
return str2Date(date, DateFormat);
|
}
|
else {
|
Date d = str2Date(date, DateFormat);
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(d);
|
cal.add(Calendar.DAY_OF_MONTH,dayCount);
|
return cal.getTime();
|
}
|
}
|
}
|
|
/**
|
* 在时间上加天数
|
*
|
* @param date 日期对象
|
* @param dayCount 天数
|
* @return 添加后的日期对象
|
*/
|
public static Date getDateAddDay(Date date, int dayCount) {
|
if (dayCount == 0) {
|
return date;
|
}
|
else {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
cal.add(Calendar.DAY_OF_MONTH,dayCount);
|
return cal.getTime();
|
}
|
}
|
|
/**
|
* 日期相减
|
*
|
* @param beginDateStr 开始日期字符串
|
* @param endDateStr 结束日期字符串
|
* @return 相减后日期值
|
*/
|
public static long getDaySub(String beginDateStr, String endDateStr) {
|
if (beginDateStr == null || beginDateStr.trim().equals("")
|
|| endDateStr == null || endDateStr.trim().equals("")) {
|
return 0;
|
}
|
long day = 0;
|
SimpleDateFormat format = new SimpleDateFormat(DateFormat);
|
Date beginDate;
|
Date endDate;
|
try {
|
beginDate = format.parse(beginDateStr);
|
endDate = format.parse(endDateStr);
|
day = (endDate.getTime() - beginDate.getTime())
|
/ (24 * 60 * 60 * 1000);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return day;
|
}
|
|
/**
|
* 获取相关日期相减
|
* @param date 开始日期
|
* @param date1 结束日期
|
* @return 相减的差额
|
*/
|
public static long getDaySub(Date date, Date date1) {
|
return (date.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);
|
}
|
|
|
/**
|
* 在时间上加上某个数。不能使用getTime后然后添加数,会造成long的溢出
|
* @param d 原始日期对象
|
* @param addDayType 添加的类型,必须是Calendar.YEAR,Calendar.MOUTH,Calendar.DAY_OF_YEAR,Calendar.DAY_OF_WEEK其中一个
|
* @param addCount 要添加的数,可以是负数
|
* @return 添加获取减去的日期
|
*/
|
public static Date addOrSubDate(Date d, int addDayType, int addCount ) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(d);
|
cal.add(addDayType,addCount);
|
return cal.getTime();
|
}
|
|
/**
|
* 获取当期时间
|
*
|
* @return 当前日期
|
*/
|
public static Date getNow() {
|
return new Date();
|
}
|
|
/**
|
* 获取当前时间的字符串格式
|
*
|
* @return 当前日期的字符串形式
|
*/
|
public static String getNowString() {
|
return getNowString(DEFAULT_FORMAT);
|
}
|
|
/**
|
* 获取当前时间的字符串格式(可自定义时间格式)
|
*
|
* @param simpleDateFormat
|
* 时间的格式
|
* @return 指定格式的当前日期字符串
|
*/
|
public static String getNowString(String simpleDateFormat) {
|
Date currentTime = new Date();
|
SimpleDateFormat formatter = new SimpleDateFormat(simpleDateFormat);
|
return formatter.format(currentTime);
|
}
|
|
/**
|
* 获取当前时间(可自定义时间格式)
|
*
|
* @param simpleDateFormat
|
* 时间格式
|
* @return 根据指定格式的当前日期对象
|
* @throws Exception 日期格式字符串的内容错误时抛出异常
|
*/
|
public static Date getNow(String simpleDateFormat) throws Exception {
|
return str2Date(getNowString(simpleDateFormat), simpleDateFormat);
|
}
|
|
/**
|
* 获取剩下的时间
|
*
|
* @param oldtime 原始时间
|
* @param newTime 结束时间
|
* @return 时间余额,负数表示已经超时了
|
*/
|
public static String getCountdown(String oldtime, String newTime) {
|
if (oldtime == null || oldtime.trim().equals("") || newTime == null
|
|| newTime.equals("")) {
|
return "";
|
} else {
|
try {
|
Date date1 = new SimpleDateFormat(DateTimeFormat)
|
.parse(oldtime);
|
Date date2 = new SimpleDateFormat(DateTimeFormat)
|
.parse(newTime);
|
long l = date1.getTime() - date2.getTime() > 0 ? date1
|
.getTime() - date2.getTime() : date2.getTime()
|
- date1.getTime();
|
long d = 0;
|
long yushu = l;
|
long h = 0;
|
long m = 0;
|
if (l > (24 * 60 * 60 * 1000)) {
|
yushu = l % (24 * 60 * 60 * 1000);
|
d = (l - yushu) / (24 * 60 * 60 * 1000);
|
}
|
if (yushu > (60 * 60 * 1000)) {
|
h = (yushu - yushu % (60 * 60 * 1000)) / (60 * 60 * 1000);
|
yushu = yushu % (60 * 60 * 1000);
|
}
|
if (yushu > (60 * 1000)) {
|
m = (yushu - yushu % (60 * 1000)) / (60 * 1000);
|
}
|
if (date1.getTime() - date2.getTime() < 0) {
|
return "已经超期" + d + "天" + h + "小时" + m + "分";
|
}
|
else {
|
return "还剩下" + d + "天" + h + "小时" + m + "分";
|
}
|
} catch (Exception te) {
|
return "";
|
}
|
}
|
}
|
|
/**
|
* 两个时间相减
|
*
|
* @param oldTime 原时间
|
* @param newTime 结束时间
|
* @return 相减后的毫秒数
|
*/
|
public static long getDateDiffer(String oldTime, String newTime) {
|
if (oldTime == null || oldTime.trim().equals("") || newTime == null
|
|| newTime.equals("")) {
|
return 0;
|
} else {
|
try {
|
Date date1 = new SimpleDateFormat(DateTimeFormat)
|
.parse(oldTime);
|
Date date2 = new SimpleDateFormat(DateTimeFormat)
|
.parse(newTime);
|
return date1.getTime() - date2.getTime();
|
} catch (Exception e) {
|
return 0;
|
}
|
}
|
}
|
|
/**
|
* 获取某年一共有多少周
|
*
|
* @param year 年份
|
* @return 某年的周数
|
*/
|
public static int getWeeks(int year) {
|
if (year == 0) {
|
return year;
|
}
|
int week = 0;
|
int days = 365;
|
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
|
days = 366;
|
}
|
week = days / 7;
|
return week;
|
}
|
|
/**
|
* 传入的时间在这一年是第几周
|
*
|
* @param date 日期对象
|
* @return 日期所在的周数
|
*/
|
public static int getWeekOnDate(Date date) {
|
GregorianCalendar g = new GregorianCalendar();
|
g.setTime(date);
|
if (isSunday(date)) {
|
return g.get(Calendar.WEEK_OF_YEAR) - 1;
|
}
|
return g.get(Calendar.WEEK_OF_YEAR);// 获得周数
|
}
|
|
/**
|
* 获取某年中某周的日历对象
|
*
|
* @param year 年份
|
* @param week 周数
|
* @return 日历对象
|
* @throws Exception 在年份的字符串格式错误时抛出错误
|
*/
|
public static Calendar getCalendarFromWeek(String year, int week)
|
throws Exception {
|
Date newDate;
|
newDate = str2Date(year + "-01-01", DateFormat);
|
Calendar caleNew = Calendar.getInstance();
|
caleNew.setTime(newDate);
|
caleNew.add(Calendar.WEEK_OF_YEAR, week - 1);
|
return caleNew;
|
}
|
|
/**
|
* 获取某年中某周的日期数组
|
*
|
* @param year 年份
|
* @param week 周数
|
* @return 某一周的整个日期
|
*/
|
public static String[] getDaysInWeek(int year, int week) {
|
String[] thisWeek = new String[7];
|
try {
|
GregorianCalendar gc = (GregorianCalendar) getCalendarFromWeek(
|
String.valueOf(year) + "-01-01", week);
|
for (int i = 0; i < 7; i++) {
|
Calendar myCale = Calendar.getInstance();
|
myCale.setTime(gc.getTime());
|
myCale.set(Calendar.DATE,
|
gc.get(Calendar.DATE) - gc.get(Calendar.DAY_OF_WEEK)
|
+ i + 2);
|
thisWeek[i] = date2Str(myCale.getTime(), DateFormat);
|
}
|
} catch (Exception e) {
|
System.out.println(e.getMessage());
|
}
|
return thisWeek;
|
}
|
|
/**
|
* 判断今天是不是周日
|
*
|
* @return true表示为周日,false表示为不是周日
|
*/
|
public static boolean isSunday() {
|
return isSunday(new Date());
|
}
|
|
/**
|
* 判断某天是不是周日
|
*
|
* @param date 日期对象
|
* @return true表示为周日,false表示为不是周日
|
*/
|
public static boolean isSunday(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
if (week == 0) {
|
return true;
|
}
|
else {
|
return false;
|
}
|
}
|
|
/**
|
* 判断某天是否周末
|
*
|
* @param date 日期对象
|
* @return true表示为周末,false表示为不是周末
|
*/
|
public static boolean isWeekend(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
if (week == 0 || week == 1) {
|
return true;
|
}
|
else {
|
return false;
|
}
|
}
|
|
/**
|
* 判断某天是否为周5
|
*
|
* @param date 日期对象
|
* @return true表示为周五,false表示为不是周五
|
*/
|
public static boolean isFriday(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
if (week == 5) {
|
return true;
|
}
|
else {
|
return false;
|
}
|
}
|
|
/**
|
* 判断某天是否为月末
|
*
|
* @param date 日期
|
* @return true表示为月末,false表示为不是月末
|
*/
|
public static boolean isMouthEnd(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
|
int endMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
if (endMonth == dayOfMonth) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
|
/**
|
* 判断某天是否为季末
|
*
|
* @param date 日期对象
|
* @return true表示为季末,false表示为不是季末
|
*/
|
public static boolean isSeasonEnd(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int y = calendar.get(Calendar.MONTH) + 1;
|
int d = calendar.get(Calendar.DATE);
|
if (y == 3 && d == 31) {
|
return true;
|
}
|
if (y == 6 && d == 30) {
|
return true;
|
}
|
if (y == 9 && d == 30) {
|
return true;
|
}
|
if (y == 12 && d == 31) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 判断某天是否为年末
|
*
|
* @param date 日期对象
|
* @return true表示为年末,false表示为不是年末
|
*/
|
public static boolean isYearEnd(Date date) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(date);
|
int y = calendar.get(Calendar.MONTH) + 1;
|
int d = calendar.get(Calendar.DATE);
|
if (y == 12 && d == 31) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 获取耗时
|
*
|
* @param newDate 新日期
|
* @param startDate 开始日期
|
* @return 耗时的毫秒数
|
*/
|
public static Long getProcessedTime(Date newDate, Date startDate) {
|
try {
|
Long p = (newDate.getTime() - startDate.getTime());
|
return p;
|
} catch (Exception e) {
|
return 0L;
|
}
|
}
|
|
/**
|
* 获取耗时
|
*
|
* @param startDate 开始日期
|
* @return 到当前时间的耗时的好描述
|
*/
|
public static String getProcessedTime(Date startDate) {
|
return getProcessedTime(new Date(), startDate) + "ms";
|
}
|
|
/**
|
* 获取当前的年份
|
* @return 年份
|
*/
|
public static String getCurrentYear() {
|
Calendar c = Calendar.getInstance();
|
return String.valueOf(c.get(Calendar.YEAR) + 1900);
|
}
|
|
/**
|
* 获取当年的年初
|
* @return 日期字符串
|
*/
|
public static String getCurrentYearStart() {
|
return getCurrentYear() + "-01-01 00:00:00";
|
}
|
|
/**
|
* 获取当年的年末
|
* @return 日期字符串
|
*/
|
public static String getCurrentYearEnd() {
|
return getCurrentYear() + "-12-31 23:59:59";
|
}
|
|
/**
|
* 获取当前所在的季度
|
* @return 季度
|
*/
|
public static String getCurrentQuarter() {
|
String currentMouth = getCurrentMouth();
|
int currentMouthInt = getInt(currentMouth);
|
if (currentMouthInt > 0 && currentMouthInt <= 3) {
|
return "1";
|
}
|
else if (currentMouthInt > 3 && currentMouthInt <= 6) {
|
return "2";
|
}
|
else if (currentMouthInt > 6 && currentMouthInt <= 9) {
|
return "3";
|
}
|
else {
|
return "4";
|
}
|
}
|
|
/**
|
* 字符串转为数字
|
* @param s 字符串
|
* @return 数字
|
*/
|
private static int getInt(String s){
|
try{
|
return Integer.valueOf(s);
|
}catch (Exception e){
|
|
}
|
return 0;
|
}
|
|
/**
|
* 获取当前季度的开始日期
|
* @return 日期字符串
|
*/
|
public static String getCurrentQuarterStart() {
|
String currentQuarter = getCurrentQuarter();
|
if ("1".equalsIgnoreCase(currentQuarter)) {
|
return getCurrentYearStart();
|
}
|
else if ("2".equalsIgnoreCase(currentQuarter)) {
|
return getCurrentYear() + "-04-01 00:00:00";
|
}
|
else if ("3".equalsIgnoreCase(currentQuarter)) {
|
return getCurrentYear() + "-07-01 00:00:00";
|
}
|
else {
|
return getCurrentYear() + "-10-01 00:00:00";
|
}
|
}
|
|
/**
|
* 获取当前季度的结束日期
|
* @return 日期字符串
|
*/
|
public static String getCurrentQuarterEnd() {
|
String currentQuarter = getCurrentQuarter();
|
if ("1".equalsIgnoreCase(currentQuarter)) {
|
return getCurrentYear() + "-03-31 23:59:59";
|
}
|
else if ("2".equalsIgnoreCase(currentQuarter)) {
|
return getCurrentYear() + "-06-30 23:59:59";
|
}
|
else if ("3".equalsIgnoreCase(currentQuarter)) {
|
return getCurrentYear() + "-09-30 23:59:59";
|
}
|
else {
|
return getCurrentYear() + "-12-31 23:59:59";
|
}
|
}
|
|
/**
|
* 获取当前的月份
|
* @return 日期字符串
|
*/
|
public static String getCurrentMouth() {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(getNow());
|
int m = cal.get(Calendar.MONTH);
|
if (m < 10) {
|
return "0" + String.valueOf(m);
|
}
|
else {
|
return String.valueOf(m);
|
}
|
}
|
|
/**
|
* 获取当前月份的开始时间
|
* @return 日期格式字符串
|
*/
|
public static String getCurrentMouthStart() {
|
String currentMouth = getCurrentMouth();
|
return getCurrentYear() + "-" + currentMouth + "-01 00:00:00";
|
}
|
|
/**
|
* 获取当前月份的结束时间
|
* @return 日期格式字符串
|
*/
|
public static String getCurrentMouthEnd() {
|
String currentMouth = getCurrentMouth();
|
int currentMouthInt = getInt(currentMouth);
|
if (currentMouthInt == 1 || currentMouthInt == 3
|
|| currentMouthInt == 5 || currentMouthInt == 7
|
|| currentMouthInt == 8 || currentMouthInt == 10
|
|| currentMouthInt == 12) {
|
return getCurrentYear() + "-" + currentMouth + "-31 23:59:59";
|
}
|
else if (currentMouthInt == 2) {
|
if (isLeapYear(getInt(getCurrentYear()))) {
|
return getCurrentYear() + "-" + currentMouth + "-29 23:59:59";
|
}else {
|
return getCurrentYear() + "-" + currentMouth + "-28 23:59:59";
|
}
|
} else {
|
return getCurrentYear() + "-" + currentMouth + "-30 23:59:59";
|
}
|
}
|
|
/**
|
* 是否是闰年
|
*
|
* @param year 年份
|
* @return true为闰年,false不是闰年
|
*/
|
public static boolean isLeapYear(int year) {
|
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
|
return true;
|
}else {
|
return false;
|
}
|
}
|
|
/**
|
* 获取当前日期
|
* @return 当前日期的字符串
|
*/
|
public static String getCurrentDay() {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(getNow());
|
int day = cal.get(Calendar.DAY_OF_MONTH);
|
if (day < 10) {
|
return "0" + String.valueOf(day);
|
}else {
|
return String.valueOf(day);
|
}
|
}
|
|
/**
|
* 获取当天的开始时间
|
* @return 日期字符串
|
*/
|
public static String getCurrentDayStart() {
|
return LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MIN).format(DateTimeFormatter.ofPattern(DateTimeFormat));
|
}
|
|
/**
|
* 获取当前的结束时间
|
* @return 日期字符串
|
*/
|
public static String getCurrentDayEnd() {
|
return LocalDateTime.of(LocalDateTime.now().toLocalDate(), LocalTime.MAX).format(DateTimeFormatter.ofPattern(DateTimeFormat));
|
}
|
|
/**
|
* 获取某个时间段内的所有日期
|
* @param dBegin 开始日期
|
* @param dEnd 结束日期
|
* @return 期间的所有日期
|
*/
|
public static List<Date> getDateInRange(Date dBegin, Date dEnd) {
|
List lDate = new ArrayList();
|
lDate.add(dBegin);
|
Calendar calBegin = Calendar.getInstance();
|
// 使用给定的 Date 设置此 Calendar 的时间
|
calBegin.setTime(dBegin);
|
Calendar calEnd = Calendar.getInstance();
|
// 使用给定的 Date 设置此 Calendar 的时间
|
calEnd.setTime(dEnd);
|
// 测试此日期是否在指定日期之后
|
while (dEnd.after(calBegin.getTime())) {
|
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
|
calBegin.add(Calendar.DAY_OF_MONTH, 1);
|
lDate.add(calBegin.getTime());
|
}
|
return lDate;
|
}
|
|
/**
|
* 字符串转时间格式,主要是直接用hibernate查询SQL语句出来的内容使用
|
* @param value 值
|
* @return 移除了毫秒的后三位
|
*/
|
public static Date getDateFromStringForVci(String value){
|
Date d = null;
|
if(StringUtils.isNotBlank(value)){
|
try{
|
if(value.indexOf("-") > -1 && value.indexOf(".")> -1 &&value.indexOf(" ") > -1 && value.substring(value.lastIndexOf(".") +1).length() >= 9){
|
//2013-4-19.14.5. 45. 734000000 这种格式,这个在使用SQL语句直接查询出时间字段的时候就会显示成这样
|
String ymd = value.substring(0,value.indexOf("."));
|
value = value.substring(value.indexOf(".") + 1);
|
if(value.indexOf(".")> -1){
|
String hms = value.substring(0,value.lastIndexOf("."));
|
String nano = value.substring(value.lastIndexOf(".") + 1).trim();
|
if(nano.length()>3){
|
nano = nano.substring(0,3);
|
}
|
hms = hms.replace(".", ":").replace(" ", "");
|
Date tempDate = VciDateUtil.str2Date(ymd + " " + hms , "yyyy-M-d h:m:s");
|
if(tempDate!=null){
|
d = VciDateUtil.str2Date(VciDateUtil.date2Str(tempDate, VciDateUtil.DateTimeFormat) + "." + nano, VciDateUtil.DateTimeMillFormat);
|
}
|
}
|
} else if(VciBaseUtil.isNumber(value)){
|
d = new Date();
|
try{
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateTimeMillFormatStr);
|
}catch(Exception e){
|
if(value.length() != 14){
|
d.setTime(VciBaseUtil.getLong(value));
|
}else {
|
try {
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateTimeFormatStr);
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(d);
|
if(cal.get(Calendar.YEAR) < 1900){
|
d.setTime(VciBaseUtil.getLong(value));
|
}
|
} catch (Exception e2) {
|
//说明不是时间格式
|
d.setTime(VciBaseUtil.getLong(value));
|
}
|
}
|
}
|
}else{
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateTimeMillFormat);
|
}
|
}catch(Exception e){
|
try{
|
d = VciDateUtil.str2Date(value, VciDateUtil.DateTimeMillFormat);
|
}catch (Exception e1) {
|
|
}
|
}
|
}
|
return d;
|
}
|
|
/**
|
* 读取文本到时间格式
|
* @param text
|
* @return
|
*/
|
public static Date readText2Date(String text){
|
if(VciBaseUtil.isNullOrNullString(text)) {
|
return null;
|
}
|
SimpleDateFormat dateFormat = null;
|
int exactDateLength = 0;
|
if(text.trim().indexOf("/")>-1 && text.trim().length() ==19){
|
exactDateLength = 19;
|
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
}
|
else if(text.trim().indexOf("/")>-1 && text.trim().length() ==17){
|
exactDateLength = 17;
|
dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
|
}
|
else if(text.trim().indexOf("/")>-1 && text.trim().length() ==8){
|
exactDateLength = 8;
|
dateFormat = new SimpleDateFormat("yy/MM/dd");
|
}
|
else if(text.trim().indexOf("-")>-1 && text.trim().length() >=19){
|
if(text.trim().length() == 19){
|
exactDateLength = 19;
|
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
}else if(text.trim().length() == 23){
|
exactDateLength = 23;
|
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
}
|
}
|
else if (text.trim().indexOf("-")>-1 && text.trim().length() ==17){
|
exactDateLength = 17;
|
dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
|
}
|
else if(text.trim().indexOf("-")>-1 && text.trim().length() ==8){
|
exactDateLength = 8;
|
dateFormat = new SimpleDateFormat("yy-MM-dd");
|
}//先弄这么多,不够再添加
|
else{
|
exactDateLength = 19;
|
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
}
|
if ((text != null) && (exactDateLength >= 0)&& (text.length() != exactDateLength)) {
|
throw new IllegalArgumentException("不能初始化时间,因为内容不到" + exactDateLength + "长度");
|
}
|
try {
|
return VciDateUtil.str2Date(VciDateUtil.date2Str(dateFormat.parse(text), VciDateUtil.DateTimeFormat), VciDateUtil.DateTimeMillFormat);//无论界面是什么格式,我们都转化为"yyyy-MM-dd HH:mm:ss.SSS"这样可以统一后台的内容
|
} catch (ParseException ex) {
|
throw new IllegalArgumentException("不能格式化日期: "+ ex.getMessage(), ex);
|
} catch (Exception e) {
|
throw new IllegalArgumentException("不能格式化日期: "+ e.getMessage(), e);
|
}
|
}
|
|
/**
|
* 获取某天的农历形式
|
*
|
* @param d
|
* @return
|
* @throws Exception
|
*/
|
public static String getChinaDate(String d) throws Exception {
|
Date s = str2Date(d, DateFormat);
|
SimpleDateFormat chineseDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
|
Calendar today = Calendar.getInstance();
|
try {
|
today.setTime(chineseDateFormat.parse(date2Str(s,
|
"yyyy年MM月dd日")));
|
} catch (ParseException e) {
|
throw new Exception(e);
|
}
|
Lunar lunar = new Lunar(today);
|
return lunar.getDate();
|
}
|
|
/**
|
* 获取时间的time值
|
* @param date date
|
* @Return java.lang.Long
|
*/
|
public static Long getTime(Date date) {
|
if(date == null){
|
return null;
|
}
|
return date.getTime();
|
}
|
|
/**
|
* 获取当前时间的time值
|
* @Return java.lang.Long
|
*/
|
public static Long getNowTime() {
|
Date date = new Date();
|
return date.getTime();
|
}
|
|
/**
|
* 将timestamp转换为Date
|
* @param timestamp timestamp
|
* @Return java.util.Date
|
*/
|
public static Date long2Date(Long timestamp){
|
if(timestamp == null){
|
return null;
|
}
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTimeInMillis(timestamp);
|
return calendar.getTime();
|
}
|
}
|