yaoq преди 2 години
родител
ревизия
4fe3a6176d
променени са 1 файла, в които са добавени 353 реда и са изтрити 242 реда
  1. +353
    -242
      smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/DateUtil.java

+ 353
- 242
smtweb-framework/core/src/main/java/cc/smtweb/framework/core/util/DateUtil.java Целия файл

@@ -6,6 +6,8 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
* 日期工具类
@@ -16,11 +18,12 @@ public class DateUtil {
private static ThreadLocal<DateFormat> stdDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
// private static ThreadLocal<DateFormat> stdLongDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));

private static ThreadLocal<DateFormat> simpleWeekFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("EEEE"));
private static ThreadLocal<DateFormat> simpleTimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("HHmmss"));
private static ThreadLocal<DateFormat> simpleDateFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));
private static ThreadLocal<DateFormat> simpleDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));
private static ThreadLocal<DateFormat> simpleLongDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmssSSS"));
private static ThreadLocal<DateFormat> simpleWeekFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("EEEE"));
private static ThreadLocal<DateFormat> simpleTimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("HHmmss"));
private static ThreadLocal<DateFormat> simpleDateFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMdd"));
private static ThreadLocal<DateFormat> simpleDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmss"));
private static ThreadLocal<DateFormat> simpleLongDatetimeFormat = ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyyMMddHHmmssSSS"));
private static TimeZone mTimeZone = TimeZone.getTimeZone("Asia/Shanghai");


private DateUtil(){}
@@ -90,242 +93,350 @@ public class DateUtil {
return simpleDateFormat.get().format(date);
}

/**
* 按yyyyMMddHHmmss格式化
* @param date
* @return
*/
public static String formatSimpleDateTime(Date date) {
return simpleDatetimeFormat.get().format(date);
}

/**
* 按yyyyMMddHHmmssSSS格式化
* @param date
* @return
*/
public static String formatSimpleDateTimeS(Date date) {
return simpleLongDatetimeFormat.get().format(date);
}

/**
* 按yyyyMMdd格式化
* @param date
* @return
*/
public static long date2Long(Date date) {
return Long.parseLong(formatSimpleDate(date));
}

/**
* 按yyyyMMddHHmmss格式化
* @param date
* @return
*/
public static long dateTime2Long(Date date) {
return Long.parseLong(formatSimpleDateTime(date));
}

public static String nowDate() {
return formatDate(now());
}

public static String nowDateTime() {
return formatDateTime(now());
}

public static long nowDateLong() {
return date2Long(now());
}

public static long nowDateTimeLong() {
return dateTime2Long(now());
}

// 获得当天0点时间
public static long getTimesmorning() {
return getTimesmorning(System.currentTimeMillis());
}

// 获得指定时间0点时间
public static long getTimesmorning(long time) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}

// 获得昨天0点时间
public static long getYesterdaymorning() {
return getTimesmorning() - DateUtils.MILLIS_PER_DAY;
}

// 获得当天近7天时间
public static long getWeekFromNow() {
return getTimesmorning() - DateUtils.MILLIS_PER_DAY * 7;
}

// 获得当天24点时间
public static long getTimesnight() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}

public static long getTimesnight(long time) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}

// 获得本周一0点时间
public static long getTimesWeekmorning() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return cal.getTimeInMillis();
}

// 获得本周日24点时间
public static long getTimesWeeknight() {
return getTimesWeekmorning() + DateUtils.MILLIS_PER_DAY * 7;
}

// 获得本月第一天0点时间
public static long getTimesMonthMorning() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
return cal.getTimeInMillis();
}

// 获得本月最后一天24点时间
public static long getTimesMontgHight() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, 24);
return cal.getTimeInMillis();
}

public static long getLastMonthStartMorning() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getTimesMonthMorning());
cal.add(Calendar.MONTH, -1);
return cal.getTimeInMillis();
}

public static long getLastMonthStartMorning(long timeInMillis) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
cal.add(Calendar.MONTH, -1);
return cal.getTimeInMillis();
}

public static Date getCurrentQuarterStartTime() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(getTimesMonthMorning());
int currentMonth = c.get(Calendar.MONTH) + 1;
SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = null;
try {
if (currentMonth <= 3) {
c.set(Calendar.MONTH, 0);
} else if (currentMonth <= 6) {
c.set(Calendar.MONTH, 3);
} else if (currentMonth <= 9) {
c.set(Calendar.MONTH, 4);
} else if (currentMonth <= 12) {
c.set(Calendar.MONTH, 9);
}

c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 当前季度的结束时间,即2012-03-31 23:59:59
*
* @return
*/
public static Date getCurrentQuarterEndTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(getCurrentQuarterStartTime());
cal.add(Calendar.MONTH, 3);
return cal.getTime();
}

public static long getCurrentYearStartTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getTimesMonthMorning());
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
return cal.getTimeInMillis();
}

public static long getCurrentYearEndTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getCurrentYearStartTime());
cal.add(Calendar.YEAR, 1);
return cal.getTimeInMillis();
}

public static long getLastYearStartTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getCurrentYearStartTime());
cal.add(Calendar.YEAR, -1);
return cal.getTimeInMillis();
}

public static long getLastYearStartTime(long timeInMillis) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
cal.add(Calendar.YEAR, -1);
return cal.getTimeInMillis();
}

public static String parseTimeTag(Date date){
if(date == null){
return "";
}
int now = date2Day(new Date());
int day = date2Day(date);
int deDay = now - day;
/**
* 按yyyyMMddHHmmss格式化
*
* @param date
* @return
*/
public static String formatSimpleDateTime(Date date) {
return simpleDatetimeFormat.get().format(date);
}

/**
* 按yyyyMMddHHmmssSSS格式化
*
* @param date
* @return
*/
public static String formatSimpleDateTimeS(Date date) {
return simpleLongDatetimeFormat.get().format(date);
}

/**
* 按yyyyMMdd格式化
*
* @param date
* @return
*/
public static long date2Long(Date date) {
return Long.parseLong(formatSimpleDate(date));
}

/**
* 按yyyyMMddHHmmss格式化
*
* @param date
* @return
*/
public static long dateTime2Long(Date date) {
return Long.parseLong(formatSimpleDateTime(date));
}

public static String nowDate() {
return formatDate(now());
}

public static String nowDateTime() {
return formatDateTime(now());
}

public static long nowDateLong() {
return date2Long(now());
}

public static long nowDateTimeLong() {
return dateTime2Long(now());
}

// 获得当天0点时间
public static long getTimesmorning() {
return getTimesmorning(System.currentTimeMillis());
}

// 获得指定时间0点时间
public static long getTimesmorning(long time) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}

// 获得昨天0点时间
public static long getYesterdaymorning() {
return getTimesmorning() - DateUtils.MILLIS_PER_DAY;
}

// 获得当天近7天时间
public static long getWeekFromNow() {
return getTimesmorning() - DateUtils.MILLIS_PER_DAY * 7;
}

// 获得当天24点时间
public static long getTimesnight() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}

public static long getTimesnight(long time) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}

// 获得本周一0点时间
public static long getTimesWeekmorning() {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return cal.getTimeInMillis();
}

// 获得本周日24点时间
public static long getTimesWeeknight() {
return getTimesWeekmorning() + DateUtils.MILLIS_PER_DAY * 7;
}

// 获得本月第一天0点时间
public static long getTimesMonthMorning() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
return cal.getTimeInMillis();
}

// 获得本月最后一天24点时间
public static long getTimesMontgHight() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
cal.set(Calendar.HOUR_OF_DAY, 24);
return cal.getTimeInMillis();
}

public static long getLastMonthStartMorning() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getTimesMonthMorning());
cal.add(Calendar.MONTH, -1);
return cal.getTimeInMillis();
}

public static long getLastMonthStartMorning(long timeInMillis) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
cal.add(Calendar.MONTH, -1);
return cal.getTimeInMillis();
}

public static Date getCurrentQuarterStartTime() {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(getTimesMonthMorning());
int currentMonth = c.get(Calendar.MONTH) + 1;
SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = null;
try {
if (currentMonth <= 3) {
c.set(Calendar.MONTH, 0);
} else if (currentMonth <= 6) {
c.set(Calendar.MONTH, 3);
} else if (currentMonth <= 9) {
c.set(Calendar.MONTH, 4);
} else if (currentMonth <= 12) {
c.set(Calendar.MONTH, 9);
}

c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 当前季度的结束时间,即2012-03-31 23:59:59
*
* @return
*/
public static Date getCurrentQuarterEndTime() {
Calendar cal = Calendar.getInstance();
cal.setTime(getCurrentQuarterStartTime());
cal.add(Calendar.MONTH, 3);
return cal.getTime();
}

public static long getCurrentYearStartTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getTimesMonthMorning());
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.YEAR));
return cal.getTimeInMillis();
}

public static long getCurrentYearEndTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getCurrentYearStartTime());
cal.add(Calendar.YEAR, 1);
return cal.getTimeInMillis();
}

public static long getLastYearStartTime() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(getCurrentYearStartTime());
cal.add(Calendar.YEAR, -1);
return cal.getTimeInMillis();
}

public static long getLastYearStartTime(long timeInMillis) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(timeInMillis);
cal.add(Calendar.YEAR, -1);
return cal.getTimeInMillis();
}

public static String parseTimeTag(Date date) {
if (date == null) {
return "";
}
int now = date2Day(new Date());
int day = date2Day(date);
int deDay = now - day;
// int deMonth = now/100 - day/100;
int deYear = now/10000 - day/10000;
if(deYear < 1){
switch(deDay){
case 0: return new SimpleDateFormat("HH:mm").format(date);
case 1: return "昨天";
default:
return new SimpleDateFormat("MM-dd").format(date);
}
}
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}

private static int date2Day(Date date){
return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(date));
}

public static void main(String[] args) {
System.out.println(getTimesmorning());
}
int deYear = now / 10000 - day / 10000;
if (deYear < 1) {
switch (deDay) {
case 0:
return new SimpleDateFormat("HH:mm").format(date);
case 1:
return "昨天";
default:
return new SimpleDateFormat("MM-dd").format(date);
}
}
return new SimpleDateFormat("yyyy-MM-dd").format(date);
}

private static int date2Day(Date date) {
return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(date));
}

/**
* 获取标准今日日期串
*
* @return 今日日期字符串 yyyy-MM-dd;
*/
public static String nowStdDateString() {
return parseTimeTag(now());
}

public static String nowYear() {
return nowStdDateString().substring(0, 4);
}

//两位的月
public static String nowMonth2() {
return nowStdDateString().substring(5, 7);
}

/**
* 取得当月第一天日期串
*
* @return String
*/
public static String getMonthFirstDay() {
return nowYear() + "-" + nowMonth2() + "-01";
}

public static String getMonthFirstDay(String ym) {
return ym.substring(0, 4) + "-" + ym.substring(4, 6) + "-01";
}

public static String getMonthLastDay(String ym) {
return getAfterDays(getAfterMonth(getMonthFirstDay(ym), 1), -1);
}


public static Long getMonthLastDayLong(String ym) {
return Long.parseLong(getMonthLastDay(ym).replaceAll("-",""));
}


/**
* 取与指定日期间隔月的日期(yyyy-mm-dd)
*
* @return String java.sql.Date.toString()
*/
public synchronized static String getAfterMonth(java.util.Date dt, int months) {
if (dt == null) return "";
if (months == 0) return dt.toString();
GregorianCalendar dy = new GregorianCalendar();
dy.setTime(dt);
dy.add(GregorianCalendar.MONTH, months);
java.sql.Date ret = new java.sql.Date(dy.getTimeInMillis());
return ret.toString();
}

/**
* 取与指定日期间隔月的日期(yyyy-mm-dd)
*
* @param dt (yyyy-mm-dd)
* @param months 间隔月
* @return java.sql.Date.toString()
*/
public static String getAfterMonth(String dt, int months) {
return getAfterMonth(java.sql.Date.valueOf(dt), months);
}

/**
* 得到date + days的日期
*
* @param dt 日期
* @param days 间隔天数,正数往前,负数往后
* @return String java.sql.Date.toString()
*/
public synchronized static String getAfterDays(java.util.Date dt, int days) {
if (dt == null) return "";
if (days == 0) return dt.toString();
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeZone(mTimeZone);
gc.setTime(dt);
gc.add(GregorianCalendar.DATE, days);
java.sql.Date ret = new java.sql.Date(gc.getTimeInMillis());
return ret.toString();
}


/**
* 得到date + days的日期
*
* @param dt 日期(yyyy-mm-dd)
* @param days 间隔天数,正数往前,负数往后
* @return String java.sql.Date.toString()
*/
public static String getAfterDays(String dt, int days) {
if (days == 0) return dt;
try {
return getAfterDays(java.sql.Date.valueOf(dt), days);
} catch (Exception e) {
return "";
}
}

public static void main(String[] args) {
System.out.println(getTimesmorning());
}
}

Зареждане…
Отказ
Запис