【JAVA时间工具类大全(珍藏)】

在开发中经常会遇到操作时间类的业务,虽然代码部分并不算难,但为了今后查找和使用方便,现将常用的几种操作时间的工具类代码予以罗列,这篇文章主要为大家详细介绍了Java中一些常用时间工具类的使用示例代码,文中的代码简洁易懂,对我们学习Java有一定帮助,需要的可以参考一下

常用变量

相关方法

 

java.time 

import java.time.LocalDate;  
import java.time.LocalTime;  
import java.time.LocalDateTime;  
import java.time.ZonedDateTime;  
  
// 获取当前日期和时间  
LocalDateTime now = LocalDateTime.now();  
  
// 格式化日期和时间  
String formattedDateTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));  
  
// 计算日期和时间之间的差异  
Duration duration = Duration.between(start, end);  
long days = duration.toDays();

java.util.Date

import java.util.Date;

// 获取当前日期和时间

Date now = new Date();

// 将日期和时间转换为字符串

String dateTimeString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);

// 将字符串转换为日期和时间

Date dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTimeString);

  java.text.SimpleDateFormat

import java.text.SimpleDateFormat;  
  
// 格式化日期和时间  
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
String formattedDateTime = sdf.format(new Date());  
  
// 将字符串转换为日期和时间  
Date dateTime = sdf.parse("2023-03-17 12:34:56");
 

java.util.Calendar类

import java.util.Calendar;  
  
// 获取当前日期和时间  
Calendar calendar = Calendar.getInstance();  
int year = calendar.get(Calendar.YEAR);  
int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,需要加1  
int day = calendar.get(Calendar.DAY_OF_MONTH);  
int hour = calendar.get(Calendar.HOUR_OF_DAY);  
int minute = calendar.get(Calendar.MINUTE);  
int second = calendar.get(Calendar.SECOND);

下列工具引用jar

 import java.text.SimpleDateFormat;

import java.time.LocalDate;

import java.time.format.DateTimeFormatter;

import java.util.*;

当天的开始时间

public static long startOfTodDay() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    Date date = calendar.getTime();
    return date.getTime() / 1000;
}

当天的结束时间

 

public static long endOfTodDay() {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    calendar.set(Calendar.MILLISECOND, 999);
    Date date = calendar.getTime();
    return date.getTime() / 1000;
}

获取当月第一天开始时间

public static long startOfMonth() {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MONTH, 0); //获取当前月第一天
    c.set(Calendar.DAY_OF_MONTH, 1); //设置为1号,当前日期既为本月第一天
    c.set(Calendar.HOUR_OF_DAY, 0); //将小时至0
    c.set(Calendar.MINUTE, 0); //将分钟至0
    c.set(Calendar.SECOND,0); //将秒至0
    c.set(Calendar.MILLISECOND, 0); //将毫秒至0
    Date date = c.getTime();
    return date.getTime() / 1000;
}

 获取当月最后一天结束时间

 

public static long endOfMonth() {
    Calendar c2 = Calendar.getInstance();
    c2.set(Calendar.DAY_OF_MONTH, c2.getActualMaximum(Calendar.DAY_OF_MONTH)); //获取当前月最后一天
    c2.set(Calendar.HOUR_OF_DAY, 23); //将小时至23
    c2.set(Calendar.MINUTE, 59); //将分钟至59
    c2.set(Calendar.SECOND,59); //将秒至59
    c2.set(Calendar.MILLISECOND, 999); //将毫秒至999
    Date date = c2.getTime();
    return date.getTime() / 1000;
}
某天的年月日距今多少天以前
public static Map<String, Object> getYearMonthAndDay(int dayUntilNow) {

    Map<String, Object> map = new HashMap<String, Object>(3);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.add(Calendar.DATE, -dayUntilNow);
    map.put("year", calendar.get(Calendar.YEAR));
    map.put("month", calendar.get(Calendar.MONTH) + 1);
    map.put("day", calendar.get(Calendar.DAY_OF_MONTH));
    return map;
}

获取上个月的开始结束时间

public static Long[] getLastMonth() {
    // 取得系统当前时间
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;

    // 取得系统当前时间所在月第一天时间对象
    cal.set(Calendar.DAY_OF_MONTH, 1);

    // 日期减一,取得上月最后一天时间对象
    cal.add(Calendar.DAY_OF_MONTH, -1);

    // 输出上月最后一天日期
    int day = cal.get(Calendar.DAY_OF_MONTH);

    String months = "";
    String days = "";

    if (month > 1) {
        month--;
    } else {
        year--;
        month = 12;
    }
    if (!(String.valueOf(month).length() > 1)) {
        months = "0" + month;
    } else {
        months = String.valueOf(month);
    }
    if (!(String.valueOf(day).length() > 1)) {
        days = "0" + day;
    } else {
        days = String.valueOf(day);
    }
    String firstDay = "" + year + "-" + months + "-01";
    String lastDay = "" + year + "-" + months + "-" + days + " 23:59:59";

    Long[] lastMonth = new Long[2];
    lastMonth[0] = DateUtil.getDateline(firstDay);
    lastMonth[1] = DateUtil.getDateline(lastDay, "yyyy-MM-dd HH:mm:ss");

    return lastMonth;
}

判断当前时间是否在某个时间范围

 

* @param start 开始时间,以秒为单位的时间戳
* @param end   结束时间,以秒为单位的时间戳
public static boolean inRangeOf(long start, long end) {
    long now = getDateline();
    return start <= now && end >= now;
}

获取一个时间的第二天

public static Date getNextDay(Date date)
{
    //1天24小时,1小时60分钟,1分钟60秒,1秒1000毫秒
    long addTime = 1 * 24 * 60 * 60 * 1000;
    Date nextDate = new  Date(date.getTime() + addTime);
    return nextDate;
}

获取日期是周几

/**
 * 获取日期是周几(int)
 * @param dateStr
 * @return
 */
public static Integer getDayOfWeek(String dateStr){
    LocalDate ld  = LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    return ld.getDayOfWeek().getValue();
}

/**
 * 获取日期是周几(String)
 * @param dateStr
 * @return
 */
public static String getDayOfWeekName(String dateStr){
    String[] weekDays = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
    int week = getDayOfWeek(dateStr) - 1;
    if (week < 0){
        week = 0;
    }
    return weekDays[week];
}
package com.zyq.util.date;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
/**
 * @author msl
 * @since 2023/5/17
 */
public class DateUtils {
 
    /** 一星期的天数 */
    public static final int WEEK_DAYS = 7;
    /** 一年的月份数 */
    public static final int YEAR_MONTHS = 12;
    /** 一天的小时数 */
    public static final int DAY_HOURS = 24;
    /** 一小时分钟数 */
    public static final int HOUR_MINUTES = 60;
    /** 一天分钟数 (24 * 60) */
    public static final int DAY_MINUTES = 1440;
    /** 一分钟的秒数 */
    public static final int MINUTE_SECONDS = 60;
    /** 一个小时的秒数 (60 * 60) */
    public static final int HOUR_SECONDS = 3600;
    /** 一天的秒数 (24 * 60 * 60) */
    public static final int DAY_SECONDS = 86400;
    /** 一秒的毫秒数 */
    public static final long SECOND_MILLISECONDS = 1000L;
    /** 一分钟的毫秒数(60 * 1000) */
    public static final long MINUTE_MILLISECONDS = 60000L;
    /** 一小时的毫秒数(60 * 60 * 1000) */
    public static final long HOUR_MILLISECONDS = 3600000L;
    /** 一天的毫秒数(24 * 60* 60* 1000) */
    public static final long DAY_MILLISECONDS = 86400000L;
    /** 星期一 */
    public static final int WEEK_1_MONDAY = 1;
    /** 星期二 */
    public static final int WEEK_2_TUESDAY = 2;
    /** 星期三 */
    public static final int WEEK_3_WEDNESDAY = 3;
    /** 星期四 */
    public static final int WEEK_4_THURSDAY = 4;
    /** 星期五 */
    public static final int WEEK_5_FRIDAY = 5;
    /** 星期六 */
    public static final int WEEK_6_SATURDAY = 6;
    /** 星期天 */
    public static final int WEEK_7_SUNDAY = 7;
    /** 一月 */
    public static final int MONTH_1_JANUARY = 1;
    /** 二月 */
    public static final int MONTH_2_FEBRUARY = 2;
    /** 三月 */
    public static final int MONTH_3_MARCH = 3;
    /** 四月 */
    public static final int MONTH_4_APRIL= 4;
    /** 五月 */
    public static final int MONTH_5_MAY = 5;
    /** 六月 */
    public static final int MONTH_6_JUNE = 6;
    /** 七月 */
    public static final int MONTH_7_JULY = 7;
    /** 八月 */
    public static final int MONTH_8_AUGUST = 8;
    /** 九月 */
    public static final int MONTH_9_SEPTEMBER = 9;
    /** 十月 */
    public static final int MONTH_10_OCTOBER = 10;
    /** 十一月 */
    public static final int MONTH_11_NOVEMBER = 11;
    /** 十二月 */
    public static final int MONTH_12_DECEMBER= 12;
    /** 显示到日期 */
    public static final String FORMAT_DATE = "yyyy-MM-dd";
    /** 显示到小时 */
    public static final String FORMAT_HOUR = "yyyy-MM-dd HH";
    /** 显示到分 */
    public static final String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";
    /** 显示到秒 */
    public static final String FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
    /** 显示到毫秒 */
    public static final String FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss:SSS";
    /** 显示到日期(数字格式) */
    public static final String FORMAT_NO_DATE = "yyyyMMdd";
    /** 显示到小时(数字格式) */
    public static final String FORMAT_NO_HOUR = "yyyyMMddHH";
    /** 显示到分(数字格式) */
    public static final String FORMAT_NO_MINUTE = "yyyyMMddHHmm";
    /** 显示到秒(数字格式) */
    public static final String FORMAT_NO_SECOND = "yyyyMMddHHmmss";
    /** 显示到毫秒(数字格式) */
    public static final String FORMAT_NO_MILLISECOND = "yyyyMMddHHmmssSSS";
    /** 时间格式化器集合 */
    private static final Map<String, SimpleDateFormat> simpleDateFormatMap = new HashMap<String, SimpleDateFormat>();
    static {
        simpleDateFormatMap.put(FORMAT_DATE, new SimpleDateFormat(FORMAT_DATE));
        simpleDateFormatMap.put(FORMAT_HOUR, new SimpleDateFormat(FORMAT_HOUR));
        simpleDateFormatMap.put(FORMAT_MINUTE, new SimpleDateFormat(FORMAT_MINUTE));
        simpleDateFormatMap.put(FORMAT_SECOND, new SimpleDateFormat(FORMAT_SECOND));
        simpleDateFormatMap.put(FORMAT_MILLISECOND, new SimpleDateFormat(FORMAT_MILLISECOND));
        simpleDateFormatMap.put(FORMAT_NO_DATE, new SimpleDateFormat(FORMAT_NO_DATE));
        simpleDateFormatMap.put(FORMAT_NO_HOUR, new SimpleDateFormat(FORMAT_NO_HOUR));
        simpleDateFormatMap.put(FORMAT_NO_MINUTE, new SimpleDateFormat(FORMAT_NO_MINUTE));
        simpleDateFormatMap.put(FORMAT_NO_SECOND, new SimpleDateFormat(FORMAT_NO_SECOND));
        simpleDateFormatMap.put(FORMAT_NO_MILLISECOND, new SimpleDateFormat(FORMAT_NO_MILLISECOND));
    }
 
    /**
     * 获取指定时间格式化器
     *
     * @param formatStyle 时间格式
     * @return 时间格式化器
     */
    private static SimpleDateFormat getSimpleDateFormat(String formatStyle) {
        SimpleDateFormat dateFormat = simpleDateFormatMap.get(formatStyle);
        if (Objects.nonNull(dateFormat)) {
            return dateFormat;
        }
        return new SimpleDateFormat(formatStyle);
    }
 
    /**
     * 将 Date 格式时间转化为指定格式时间
     *
     * @param date        Date 格式时间
     * @param formatStyle 转化指定格式(如: yyyy-MM-dd HH:mm:ss)
     * @return 转化格式时间
     */
    public static String format(Date date, String formatStyle) {
        if (Objects.isNull(date)) {
            return "";
        }
        return getSimpleDateFormat(formatStyle).format(date);
    }
 
    /**
     * 将 Date 格式时间转化为 yyyy-MM-dd 格式时间
     *
     * @param date Date 格式时间
     * @return yyyy-MM-dd 格式时间(如:2022-06-17)
     */
    public static String formatDate(Date date) {
        return format(date, FORMAT_DATE);
    }
 
    /**
     * 将 Date 格式时间转化为 yyyy-MM-dd HH:mm:ss 格式时间
     *
     * @param date Date 格式时间
     * @return yyyy-MM-dd HH:mm:ss 格式时间(如:2022-06-17 16:06:17)
     */
    public static String formatDateTime(Date date) {
        return format(date, FORMAT_SECOND);
    }
 
    /**
     * 将 Date 格式时间转化为 yyyy-MM-dd HH:mm:ss:SSS 格式时间
     *
     * @param date Date 格式时间
     * @return yyyy-MM-dd HH:mm:ss:SSS 格式时间(如:2022-06-17 16:06:17:325)
     */
    public static String formatDateTimeStamp(Date date) {
        return format(date, FORMAT_MILLISECOND);
    }
 
    /**
     * 将 yyyy-MM-dd 格式时间转化为 Date 格式时间
     * 
     * @param dateString yyyy-MM-dd 格式时间(如:2022-06-17)
     * @return Date 格式时间
     */
    public static Date parseDate(String dateString) {
        return parse(dateString, FORMAT_DATE);
    }
 
    /**
     * 将 yyyy-MM-dd HH:mm:ss 格式时间转化为 Date 格式时间
     * 
     * @param dateTimeStr yyyy-MM-dd HH:mm:ss 格式时间(如:2022-06-17 16:06:17)
     * @return Date 格式时间
     */
    public static Date parseDateTime(String dateTimeStr) {
        return parse(dateTimeStr, FORMAT_SECOND);
    }
 
    /**
     * 将 yyyy-MM-dd HH:mm:ss:SSS 格式时间转化为 Date 格式时间
     * 
     * @param dateTimeStr yyyy-MM-dd HH:mm:ss:SSS 格式时间(如:2022-06-17 16:06:17)
     * @return Date 格式时间
     */
    public static Date parseDateTimeStamp(String dateTimeStampStr) {
        return parse(dateTimeStampStr, FORMAT_MILLISECOND);
    }
 
    /**
     * 将字符串格式时间转化为 Date 格式时间
     * 
     * @param dateString 字符串时间(如:2022-06-17 16:06:17)
     * @return formatStyle 格式内容
     * @return Date 格式时间
     */
    public static Date parse(String dateString, String formatStyle) {
        String s = getString(dateString);
        if (s.isEmpty()) {
            return null;
        }
        try {
            return getSimpleDateFormat(formatStyle).parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
 
    /**
     * 获取字符串有效内容
     * 
     * @param s 字符串
     * @return 有效内容
     */
    private static String getString(String s) {
        return Objects.isNull(s) ? "" : s.trim();
    }
 
    /**
     * 获取一天的开始时间(即:0 点 0 分 0 秒 0 毫秒)
     * 
     * @param date 指定时间
     * @return 当天的开始时间
     */
    public static Date getDateStart(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        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();
    }
 
    /**
     * 获取一天的截止时间(即:23 点 59 分 59 秒 999 毫秒)
     * 
     * @param date 指定时间
     * @return 当天的开始时间
     */
    public static Date getDateEnd(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
 
    /**
     * 获取日期数字
     * 
     * @param date 日期
     * @return 日期数字
     */
    public static int getDateNo(Date date) {
        if (Objects.isNull(date)) {
            return 0;
        }
        return Integer.valueOf(format(date, FORMAT_NO_DATE));
    }
 
    /**
     * 获取日期时间数字(到秒)
     * 
     * @param date 日期
     * @return 日期数字
     */
    public static long getDateTimeNo(Date date) {
        if (Objects.isNull(date)) {
            return 0L;
        }
        return Long.valueOf(format(date, FORMAT_NO_SECOND));
    }
 
    /**
     * 获取日期时间数字(到毫秒)
     * 
     * @param date 日期
     * @return 日期数字
     */
    public static long getDateTimeStampNo(Date date) {
        if (Objects.isNull(date)) {
            return 0L;
        }
        return Long.valueOf(format(date, FORMAT_NO_MILLISECOND));
    }
 
    /**
     * 获取星期几
     * 
     * @param date 时间
     * @return 0(时间为空), 1(周一), 2(周二),3(周三),4(周四),5(周五),6(周六),7(周日)
     */
    public static int getWeek(Date date) {
        if (Objects.isNull(date)) {
            return 0;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getWeek(calendar);
    }
 
    /**
     * 获取星期几
     * 
     * @param date 时间
     * @return 0(时间为空), 1(周一), 2(周二),3(周三),4(周四),5(周五),6(周六),7(周日)
     */
    private static int getWeek(Calendar calendar) {
        switch (calendar.get(Calendar.DAY_OF_WEEK)) {
        case Calendar.MONDAY:
            return 1;
        case Calendar.TUESDAY:
            return 2;
        case Calendar.WEDNESDAY:
            return 3;
        case Calendar.THURSDAY:
            return 4;
        case Calendar.FRIDAY:
            return 5;
        case Calendar.SATURDAY:
            return 6;
        case Calendar.SUNDAY:
            return 7;
        default:
            return 0;
        }
    }
 
    /**
     * 获取该日期是今年的第几周(以本年的周一为第1周,详见下面说明)<br>
     * 
     * 【说明】<br>
     * 比如 2022-01-01(周六)和 2022-01-02(周日)虽然在 2022 年里,但他们两天则属于 2021 年最后一周,<br>
     * 那么这两天不会算在 2022 年第 1 周里,此时会返回 0 ;而 2022 年第 1 周将从 2022-01-03(周一) 开始计算。<br>
     * 
     * @param date 时间
     * @return -1(时间为空), 0(为上个年的最后一周),其他数字(今年的第几周)
     */
    public static int getWeekOfYear(Date date) {
        if (Objects.isNull(date)) {
            return -1;
        }
        int weeks = getWeekOfYearIgnoreLastYear(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int week = getWeek(calendar);
        if (week == 1) {
            return weeks;
        }
        return weeks - 1;
    }
 
    /**
     * 获取今年的第几周(以本年的1月1日为第1周第1天)<br>
     * 
     * @param date 时间
     * @return -1(时间为空),其他数字(今年的第几周)
     */
    public static int getWeekOfYearIgnoreLastYear(Date date) {
        if (Objects.isNull(date)) {
            return -1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int days = calendar.get(Calendar.DAY_OF_YEAR);
        int weeks = days / 7;
        // 如果是 7 的倍数,则表示恰好是多少周
        if (days % 7 == 0) {
            return weeks;
        }
        // 如果有余数,则需要再加 1
        return weeks + 1;
    }
 
    /**
     * 获取时间节点对象
     * 
     * @param date 时间对象
     * @return DateNode
     */
    public static DateNode getDateNode(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        DateNode node = new DateNode();
        node.setTime(format(date, FORMAT_MILLISECOND));
        node.setYear(calendar.get(Calendar.YEAR));
        node.setMonth(calendar.get(Calendar.MONTH) + 1);
        node.setDay(calendar.get(Calendar.DAY_OF_MONTH));
        node.setHour(calendar.get(Calendar.HOUR_OF_DAY));
        node.setMinute(calendar.get(Calendar.MINUTE));
        node.setSecond(calendar.get(Calendar.SECOND));
        node.setMillisecond(calendar.get(Calendar.MILLISECOND));
        node.setWeek(getWeek(calendar));
        node.setDayOfYear(calendar.get(Calendar.DAY_OF_YEAR));
        node.setWeekOfYear(getWeekOfYear(date));
        node.setWeekOfYearIgnoreLastYear(getWeekOfYearIgnoreLastYear(date));
        node.setMillisecondStamp(date.getTime());
        node.setSecondStamp(node.getMillisecondStamp() / 1000);
        return node;
    }
 
    /**
     * 日期变更
     * 
     * @param date   指定日期
     * @param field  变更属性(如变更年份,则该值为 Calendar.DAY_OF_YEAR)
     * @param amount 变更大小(大于 0 时增加,小于 0 时减少)
     * @return 变更后的日期时间
     */
    public static Date add(Date date, int field, int amount) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, amount);
        return calendar.getTime();
    }
 
    /**
     * 指定日期加减年份
     * 
     * @param date 指定日期
     * @param year 变更年份(大于 0 时增加,小于 0 时减少)
     * @return 变更年份后的日期
     */
    public static Date addYear(Date date, int year) {
        return add(date, Calendar.YEAR, year);
    }
 
    /**
     * 指定日期加减月份
     * 
     * @param date  指定日期
     * @param month 变更月份(大于 0 时增加,小于 0 时减少)
     * @return 变更月份后的日期
     */
    public static Date addMonth(Date date, int month) {
        return add(date, Calendar.MONTH, month);
    }
 
    /**
     * 指定日期加减天数
     * 
     * @param date 指定日期
     * @param day  变更天数(大于 0 时增加,小于 0 时减少)
     * @return 变更天数后的日期
     */
    public static Date addDay(Date date, int day) {
        return add(date, Calendar.DAY_OF_YEAR, day);
    }
 
    /**
     * 指定日期加减星期
     * 
     * @param date 指定日期
     * @param week 变更星期数(大于 0 时增加,小于 0 时减少)
     * @return 变更星期数后的日期
     */
    public static Date addWeek(Date date, int week) {
        return add(date, Calendar.WEEK_OF_YEAR, week);
    }
 
    /**
     * 指定日期加减小时
     * 
     * @param date 指定日期时间
     * @param hour 变更小时数(大于 0 时增加,小于 0 时减少)
     * @return 变更小时数后的日期时间
     */
    public static Date addHour(Date date, int hour) {
        return add(date, Calendar.HOUR_OF_DAY, hour);
    }
 
    /**
     * 指定日期加减分钟
     * 
     * @param date   指定日期时间
     * @param minute 变更分钟数(大于 0 时增加,小于 0 时减少)
     * @return 变更分钟数后的日期时间
     */
    public static Date addMinute(Date date, int minute) {
        return add(date, Calendar.MINUTE, minute);
    }
 
    /**
     * 指定日期加减秒
     * 
     * @param date   指定日期时间
     * @param second 变更秒数(大于 0 时增加,小于 0 时减少)
     * @return 变更秒数后的日期时间
     */
    public static Date addSecond(Date date, int second) {
        return add(date, Calendar.SECOND, second);
    }
 
    /**
     * 指定日期加减秒
     * 
     * @param date   指定日期时间
     * @param minute 变更毫秒数(大于 0 时增加,小于 0 时减少)
     * @return 变更毫秒数后的日期时间
     */
    public static Date addMillisecond(Date date, int millisecond) {
        return add(date, Calendar.MILLISECOND, millisecond);
    }
 
    /**
     * 获取该日期所在周指定星期的日期
     * 
     * @param date 日期所在时间
     * @return index 指定星期(1 - 7 分别对应星期一到星期天)
     */
    public static Date getWeekDate(Date date, int index) {
        if (index < WEEK_1_MONDAY || index > WEEK_7_SUNDAY) {
            return null;
        }
        int week = getWeek(date);
        return addDay(date, index - week);
    }
 
    /**
     * 获取该日期所在周开始日期
     * 
     * @param date 日期所在时间
     * @return 所在周开始日期
     */
    public static Date getWeekDateStart(Date date) {
        return getDateStart(getWeekDate(date, WEEK_1_MONDAY));
    }
 
    /**
     * 获取该日期所在周开始日期
     * 
     * @param date 日期所在时间
     * @return 所在周开始日期
     */
    public static Date getWeekDateEnd(Date date) {
        return getWeekDateEnd(getWeekDate(date, WEEK_7_SUNDAY));
    }
 
    /**
     * 获取该日期所在周的所有日期(周一到周日)
     * 
     * @param Date 日期
     * @return 该日照所在周的所有日期
     */
    public static List<Date> getWeekDateList(Date date) {
        if (Objects.isNull(date)) {
            return Collections.emptyList();
        }
        // 获取本周开始时间
        Date weekFromDate = getWeekDateStart(date);
        // 获取本周截止时间
        Date weekeEndDate = getWeekDateEnd(date);
        return getBetweenDateList(weekFromDate, weekeEndDate, true);
    }
 
    /**
     * 获取该日期所在周的所有日期(周一到周日)
     * 
     * @param dateString
     * @return 该日照所在周的所有日期
     */
    public static List<String> getWeekDateList(String dateString) {
        Date date = parseDate(dateString);
        if (Objects.isNull(date)) {
            return Collections.emptyList();
        }
        return getDateStrList(getWeekDateList(date));
    }
 
    /**
     * 获取该日期所在月的所有日期
     * 
     * @param dateString
     * @return 该日照所月的所有日期
     */
    public static List<Date> getMonthDateList(Date date) {
        if (Objects.isNull(date)) {
            return Collections.emptyList();
        }
        Date monthDateStart = getMonthDateStart(date);
        Date monthDateEnd = getMonthDateEnd(date);
        return getBetweenDateList(monthDateStart, monthDateEnd, true);
    }
 
    /**
     * 获取该日期所在月的所有日期
     * 
     * @param dateString
     * @return 该日照所月的所有日期
     */
    public static List<String> getMonthDateList(String dateString) {
        Date date = parseDate(dateString);
        if (Objects.isNull(date)) {
            return Collections.emptyList();
        }
        return getDateStrList(getMonthDateList(date));
    }
    
    /**
     * 获取本日期所在月第一天
     * 
     * @param date 日期
     * @return 本日期所在月第一天
     */
    public static Date getMonthDateStart(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return getDateStart(calendar.getTime());
    }
 
    /**
     * 获取本日期所在月最后一天
     * 
     * @param date 日期
     * @return 本日期所在月最后一天
     */
    public static Date getMonthDateEnd(Date date) {
        if (Objects.isNull(date)) {
            return null;
        }
        Date monthDateStart = getMonthDateStart(date);
        Date nextMonthDateStart = getMonthDateStart(addMonth(monthDateStart, 1));
        return getDateEnd(addDay(nextMonthDateStart, -1));
    }
 
    /**
     * 获取两个日期相差的天数(以日期为单位计算,不以24小时制计算,详见下面说明)<br>
     * 
     * 【说明】比如 2022-06-17 23:00:00 和 2022-06-17 01:00:00,两者虽然只相差 2 个小时,但也算相差 1 天 <br>
     * 
     * @param date1 日期1
     * @param date2 日期2
     * @return 相差天数(若返回 -1,则至少有一个日期存在为空,此时不能进行比较)
     */
    public static int countBetweenDays(Date date1, Date date2) {
        if (Objects.isNull(date1) || Objects.isNull(date2)) {
            return -1;
        }
        // 获取两个日期 0 点 0 时 0 分 0 秒 0 毫秒时的时间戳(毫秒级)
        long t1 = getDateStart(date1).getTime();
        long t2 = getDateStart(date2).getTime();
        // 相差天数 = 相差的毫秒数 / 一天的毫秒数
        return (int) (Math.abs(t1 - t2) / DAY_MILLISECONDS);
    }
 
    /**
     * 获取两个日期之间的所有日期
     * 
     * @param date1 日期1
     * @param date2 日期2
     * @return 两个日期之间的所有日期的开始时间
     */
    public static List<Date> getBetweenDateList(Date date1, Date date2) {
        return getBetweenDateList(date1, date2, false);
    }
 
    /**
     * 获取两个日期之间的所有日期
     * 
     * @param date1 日期1
     * @param date2 日期2
     * @return 两个日期之间的所有日期的开始时间
     */
    public static List<Date> getBetweenDateList(Date date1, Date date2, boolean isContainParams) {
        if (Objects.isNull(date1) || Objects.isNull(date2)) {
            return Collections.emptyList();
        }
        // 确定前后日期
        Date fromDate = date1;
        Date toDate = date2;
        if (date2.before(date1)) {
            fromDate = date2;
            toDate = date1;
        }
        // 获取两个日期每天的开始时间
        Date from = getDateStart(fromDate);
        Date to = getDateStart(toDate);
        // 获取日期,开始循环
        List<Date> dates = new ArrayList<Date>();
        if (isContainParams) {
            dates.add(from);
        }
        Date date = from;
        boolean isBefore = true;
        while (isBefore) {
            date = addDay(date, 1);
            isBefore = date.before(to);
            if (isBefore) {
                dates.add(getDateStart(date));
            }
        }
        if (isContainParams) {
            dates.add(to);
        }
        return dates;
    }
 
    /**
     * 获取两个日期之间的所有日期
     * 
     * @param dateString1 日期1(如:2022-06-20)
     * @param dateString2 日期2(如:2022-07-15)
     * @return 两个日期之间的所有日期(不包含参数日期)
     */
    public static List<String> getBetweenDateList(String dateString1, String dateString2) {
        return getBetweenDateList(dateString1, dateString2, false);
    }
 
    /**
     * 获取两个日期之间的所有日期
     * 
     * @param dateString1     日期1(如:2022-06-20)
     * @param dateString2     日期2(如:2022-07-15)
     * @param isContainParams 是否包含参数的两个日期
     * @return 两个日期之间的所有日期的开始时间
     */
    public static List<String> getBetweenDateList(String dateString1, String dateString2, boolean isContainParams) {
        Date date1 = parseDate(dateString1);
        Date date2 = parseDate(dateString2);
        List<Date> dates = getBetweenDateList(date1, date2, isContainParams);
        return getDateStrList(dates);
    }
 
    /**
     * List<Date> 转 List<String>
     * 
     * @param dates 日期集合
     * @return 日期字符串集合
     */
    public static List<String> getDateStrList(List<Date> dates) {
        if (dates.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> dateList = new ArrayList<String>();
        for (Date date : dates) {
            dateList.add(formatDate(date));
        }
        return dateList;
    }
 
    static class DateNode {
        /** 年 */
        private int year;
        /** 月 */
        private int month;
        /** 日 */
        private int day;
        /** 时 */
        private int hour;
        /** 分 */
        private int minute;
        /** 秒 */
        private int second;
        /** 毫秒 */
        private int millisecond;
        /** 星期几( 1 - 7 对应周一到周日) */
        private int week;
        /** 当年第几天 */
        private int dayOfYear;
        /** 当年第几周(本年周 1 为第 1 周,0 则表示属于去年最后一周) */
        private int weekOfYear;
        /** 当年第几周(本年周 1 为第 1 周,0 则表示属于去年最后一周) */
        private int weekOfYearIgnoreLastYear;
        /** 时间戳(秒级) */
        private long secondStamp;
        /** 时间戳(毫秒级) */
        private long millisecondStamp;
        /** 显示时间 */
        private String time;
 
        public int getYear() {
            return year;
        }
 
        public void setYear(int year) {
            this.year = year;
        }
 
        public int getMonth() {
            return month;
        }
 
        public void setMonth(int month) {
            this.month = month;
        }
 
        public int getDay() {
            return day;
        }
 
        public void setDay(int day) {
            this.day = day;
        }
 
        public int getHour() {
            return hour;
        }
 
        public void setHour(int hour) {
            this.hour = hour;
        }
 
        public int getMinute() {
            return minute;
        }
 
        public void setMinute(int minute) {
            this.minute = minute;
        }
 
        public int getSecond() {
            return second;
        }
 
        public void setSecond(int second) {
            this.second = second;
        }
 
        public int getMillisecond() {
            return millisecond;
        }
 
        public void setMillisecond(int millisecond) {
            this.millisecond = millisecond;
        }
 
        public int getWeek() {
            return week;
        }
 
        public void setWeek(int week) {
            this.week = week;
        }
 
        public int getDayOfYear() {
            return dayOfYear;
        }
 
        public void setDayOfYear(int dayOfYear) {
            this.dayOfYear = dayOfYear;
        }
 
        public int getWeekOfYear() {
            return weekOfYear;
        }
 
        public void setWeekOfYear(int weekOfYear) {
            this.weekOfYear = weekOfYear;
        }
 
        public int getWeekOfYearIgnoreLastYear() {
            return weekOfYearIgnoreLastYear;
        }
 
        public void setWeekOfYearIgnoreLastYear(int weekOfYearIgnoreLastYear) {
            this.weekOfYearIgnoreLastYear = weekOfYearIgnoreLastYear;
        }
 
        public long getSecondStamp() {
            return secondStamp;
        }
 
        public void setSecondStamp(long secondStamp) {
            this.secondStamp = secondStamp;
        }
 
        public long getMillisecondStamp() {
            return millisecondStamp;
        }
 
        public void setMillisecondStamp(long millisecondStamp) {
            this.millisecondStamp = millisecondStamp;
        }
 
        public String getTime() {
            return time;
        }
 
        public void setTime(String time) {
            this.time = time;
        }
 
    }
}

 

public class DateUtil {

public static final String FULL_TIME_PATTERN = "yyyyMMddHHmmss";

public static final String FULL_TIME_SPLIT_PATTERN = "yyyy-MM-dd HH:mm:ss";

public static final String MONTH_TIME_SPLIT_PATTERN = "yyyy-MM";

public static final String CST_TIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy";

public static String formatFullTime(LocalDateTime localDateTime) {
    return formatFullTime(localDateTime, FULL_TIME_PATTERN);
}

/**
 * 常用的时间格式.
 */
private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy/MM/dd",
        "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"};

public static String formatFullTime(LocalDateTime localDateTime, String pattern) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
    return localDateTime.format(dateTimeFormatter);
}

public static String getDateFormat(Date date, String dateFormatType) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormatType, Locale.CHINA);
    return simpleDateFormat.format(date);
}

public static String formatCSTTime(String date, String format) throws ParseException {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(CST_TIME_PATTERN, Locale.US);
    Date usDate = simpleDateFormat.parse(date);
    return DateUtil.getDateFormat(usDate, format);
}

public static String formatInstant(Instant instant, String format) {
    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    return localDateTime.format(DateTimeFormatter.ofPattern(format));
}

/**
 * 获得当前时间
 *
 * @return yyyy-MM-dd HH:mm:ss
 */
public static String getCurrentTime() {
    return getDateFormat(new Date(), FULL_TIME_SPLIT_PATTERN);
}

/**
 * 获得当前月计划
 */
public static String getCurrentMonth() {
    return getDateFormat(new Date(), MONTH_TIME_SPLIT_PATTERN);
}

private static String pattern = "yyyy-MM-dd";

/**
 * 根据一个日期,返回是星期几的字符串
 *
 * @param sdate
 * @return
 */
public static String getWeek(String sdate) {
    // 再转换为时间
    Date date = DateUtil.strToDate(sdate);
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    return new SimpleDateFormat("EEEE").format(c.getTime());
}

/**
 * 将日期型字符串转换为日期格式.
 * 支持的日期字符串格式包括"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
 * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm"
 *
 * @param str
 * @return Date
 * @since 1.0
 */
public static Date parseDate(Object str) {
    if (str == null) {
        return null;
    }
    try {
        return org.apache.commons.lang3.time.DateUtils.parseDate(str.toString(), parsePatterns);
    } catch (ParseException e) {
        return null;
    }
}

/**
 * @param date   时间
 * @param format 时间格式
 * @return String
 * @Description: 时间转string
 */
public static String dateToString(Date date, String format) {

    SimpleDateFormat formatter = new SimpleDateFormat(format);
    return formatter.format(date);
}

/**
 * 使用 "yyyy-MM-dd HH:mm:ss.SSS" 格式化
 */
public static String format(Date date) {
    return dateToString(date, "yyyy-MM-dd HH:mm:ss.SSS");
}

/**
 * @param
 * @return String
 * @Description: 默认采用 yyyy-MM-dd格式
 */
public static String dateToString(Date date) {

    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    return formatter.format(date);
}

/**
 * 得到当前日期和时间字符串.
 *
 * @return String 日期和时间字符串,例如 2015-08-11 09:51:53
 * @since 1.0
 */
public static String getDateTime() {
    return formatDate(new Date(), DateUtil.FULL_TIME_SPLIT_PATTERN);
}

/**
 * @param
 * @return String
 * @Description: 默认采用 yyyy-MM-dd格式
 */
public static String dateToStringByFormat(Date date, String formart) {

    SimpleDateFormat formatter = new SimpleDateFormat(formart);
    return formatter.format(date);
}

/**
 * 将短时间格式字符串转换为时间 yyyy-MM-dd
 *
 * @param strDate
 * @return
 */
public static Date strToDate(String strDate) {
    return strToDate(strDate, pattern);
}

/**
 * 给定字符串和字符串格式转换为时间
 *
 * @param strDate
 * @param format
 * @return
 */
public static Date strToDate(String strDate, String format) {
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    ParsePosition pos = new ParsePosition(0);
    Date strtodate = formatter.parse(strDate, pos);
    return strtodate;
}

/**
 * 两个时间之间的天数
 *
 * @param lastDate
 * @param firstDate
 * @return
 */
public static long getDays(String lastDate, String firstDate) {
    if (lastDate == null || lastDate.equals(""))
        return 0;
    if (firstDate == null || firstDate.equals(""))
        return 0;
    // 转换为标准时间
    SimpleDateFormat myFormatter = new SimpleDateFormat(pattern);
    Date date = null;
    Date mydate = null;
    try {
        date = myFormatter.parse(lastDate);
        mydate = myFormatter.parse(firstDate);
    } catch (Exception e) {
    }
    long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
    return day;
}

/**
 * 计算指定时间距离现在多少天
 *
 * @param date
 * @return
 */
public static long getDaysToCurrentDate(Date date) {
    String dateStr = dateToString(date);
    String currentDateStr = dateToString(getCurrentDate());
    return getDays(dateStr, currentDateStr);
}

/**
 * 两个时间之间的分钟数
 *
 * @param @param  lastDate
 * @param @param  firstDate
 * @param @return
 * @return long
 * @throws
 */
public static long getMinutes(String lastDate, String firstDate) {
    if (lastDate == null || lastDate.equals(""))
        return 0;
    if (firstDate == null || firstDate.equals(""))
        return 0;
    // 转换为标准时间
    SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
    Date date = null;
    Date mydate = null;
    try {
        date = myFormatter.parse(lastDate);
        mydate = myFormatter.parse(firstDate);
    } catch (Exception e) {
    }
    long day = (date.getTime() - mydate.getTime()) / (60 * 1000);
    return day;
}

// 计算当月最后一天,返回字符串
public static Date getDefaultDay() {

    Calendar lastDate = Calendar.getInstance();
    lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
    lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
    lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天

    return lastDate.getTime();
}

// 上月第一天
public static Date getPreviousMonthFirst() {

    Calendar lastDate = Calendar.getInstance();
    lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
    lastDate.add(Calendar.MONTH, -1);// 减一个月,变为下月的1号

    return lastDate.getTime();
}

// 获取当月第一天
public static Date getFirstDayOfMonth() {

    Calendar lastDate = Calendar.getInstance();
    lastDate.set(Calendar.DATE, 1);// 设为当前月的1号

    return lastDate.getTime();
}

// 获得本周星期日的日期
public static Date getCurrentWeekday() {
    int mondayPlus = getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
    Date monday = currentDate.getTime();
    return monday;
}

// 获取当天时间
public static String getNowTime(String dateformat) {
    Date now = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);// 可以方便地修改日期格式
    String hehe = dateFormat.format(now);
    return hehe;
}

// 获得当前日期与本周日相差的天数
public static int getMondayPlus() {
    Calendar cd = Calendar.getInstance();
    // 获得今天是一周的第几天,星期日是第一天,星期二是第二天......
    int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
    if (dayOfWeek == 1) {
        return 0;
    } else {
        return 1 - dayOfWeek;
    }
}

// 获得本周一的日期
public static Date getMondayOFWeek() {
    int mondayPlus = getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus);
    return currentDate.getTime();
}

// 取得指定日期所在周的第一天
public static Date getFirstDayOfWeek(Date date) {
    Calendar c = new GregorianCalendar();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
    return c.getTime();
}

// 取得指定日期所在周的最后一天
public static Date getLastDayOfWeek(Date date) {
    Calendar c = new GregorianCalendar();
    c.setFirstDayOfWeek(Calendar.MONDAY);
    c.setTime(date);
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
    return c.getTime();
}

// 获得相应周的周六的日期
public static Date getSaturday() {
    int weeks = 0;
    int mondayPlus = getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks + 6);
    return currentDate.getTime();
}

// 获得上周星期日的日期
public static Date getPreviousWeekSunday() {
    int weeks = 0;
    weeks--;
    int mondayPlus = getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus + weeks);
    return currentDate.getTime();
}

// 获得上周星期一的日期
public static Date getPreviousWeekday() {
    int weeks = 0;
    weeks--;
    int mondayPlus = getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 * weeks);
    return currentDate.getTime();
}

// 获得下周星期一的日期
public static Date getNextMonday() {
    int mondayPlus = getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
    return currentDate.getTime();
}

// 获得下周星期日的日期
public static Date getNextSunday() {
    int mondayPlus = getMondayPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, mondayPlus + 7 + 6);
    return currentDate.getTime();
}

public static int getMonthPlus() {
    Calendar cd = Calendar.getInstance();
    int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);
    cd.set(Calendar.DATE, 1);// 把日期设置为当月第一天
    cd.roll(Calendar.DATE, -1);// 日期回滚一天,也就是最后一天
    int MaxDate = cd.get(Calendar.DATE);
    if (monthOfNumber == 1) {
        return -MaxDate;
    } else {
        return 1 - monthOfNumber;
    }
}

// 获得上月最后一天的日期
public static Date getPreviousMonthEnd() {
    Calendar lastDate = Calendar.getInstance();
    lastDate.add(Calendar.MONTH, -1);// 减一个月
    lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
    lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天
    return lastDate.getTime();
}

// 判断当前日期是星期几

public static int dayForWeek(String pTime) throws Exception {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(format.parse(pTime));
    int dayForWeek = 0;
    if (c.get(Calendar.DAY_OF_WEEK) == 1) {
        dayForWeek = 7;
    } else {
        dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
    }
    return dayForWeek;
}

// 获得下个月第一天的日期
public static Date getNextMonthFirst() {
    Calendar lastDate = Calendar.getInstance();
    lastDate.add(Calendar.MONTH, 1);// 减一个月
    lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
    return lastDate.getTime();
}

// 获得下个月最后一天的日期
public static Date getNextMonthEnd() {

    Calendar lastDate = Calendar.getInstance();
    lastDate.add(Calendar.MONTH, 1);// 加一个月
    lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
    lastDate.roll(Calendar.DATE, -1);// 日期回滚一天,也就是本月最后一天

    return lastDate.getTime();
}

// 获得明年第一天的日期
public static Date getNextYearFirst() {
    Calendar lastDate = Calendar.getInstance();
    lastDate.add(Calendar.YEAR, 1);// 加一年
    lastDate.set(Calendar.MONTH, 0);// 把日期设置为当年第一月
    lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天

    return lastDate.getTime();
}

/**
 * 得到某年某月的第一天
 *
 * @param year
 * @param month
 * @return
 */
public static String getLastOfMonth(int year, int month) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
    return new SimpleDateFormat("yy.MM.dd").format(cal.getTime());
}

/**
 * 得到某一年的所有月份
 *
 * @param year
 * @return
 */
public static List<String> getMonthByYear(int year) {
    List<String> months = new ArrayList<String>();
    for (int i = 1; i <= 12; i++) {
        String str = "";
        if (i < 10) {
            str = "0" + i;
        } else {
            str = i + "";
        }
        months.add(year + "-" + str);
    }
    return months;
}

/**
 * 得到当前日期的年份
 *
 * @return
 */
public static int getYear() {
    Calendar cal = Calendar.getInstance();
    return cal.get(Calendar.YEAR);
}

/**
 * 得到某年某月的第一天
 *
 * @param year
 * @param month
 * @return
 */
public static String getFirstDayOfMonth(int year, int month) {
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.YEAR, year);
    cal.set(Calendar.MONTH, month);
    cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
    return new SimpleDateFormat("yy.MM.dd").format(cal.getTime());
}

public static int getYearPlus() {
    Calendar cd = Calendar.getInstance();
    int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);// 获得当天是一年中的第几天
    cd.set(Calendar.DAY_OF_YEAR, 1);// 把日期设为当年第一天
    cd.roll(Calendar.DAY_OF_YEAR, -1);// 把日期回滚一天。
    int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
    if (yearOfNumber == 1) {
        return -MaxYear;
    } else {
        return 1 - yearOfNumber;
    }
}

// 获得本年第一天的日期
public static Date getCurrentYearFirst() {
    int yearPlus = getYearPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, yearPlus);
    return currentDate.getTime();
}

// 获得本年最后一天的日期 *
public static String getCurrentYearEnd() {
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
    String years = dateFormat.format(date);
    return years + "-12-31";
}

// 获得上年第一天的日期 *
public static String getPreviousYearFirst() {
    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
    String years = dateFormat.format(date);
    int years_value = Integer.parseInt(years);
    years_value--;
    return years_value + "-1-1";
}

// 获得上年最后一天的日期
public static String getPreviousYearEnd() {
    int weeks = 0, MaxYear = 0;
    weeks--;
    int yearPlus = getYearPlus();
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.add(GregorianCalendar.DATE, yearPlus + MaxYear * weeks
            + (MaxYear - 1));
    Date yearDay = currentDate.getTime();
    DateFormat df = DateFormat.getDateInstance();
    String preYearDay = df.format(yearDay);
    getThisSeasonTime(11);
    return preYearDay;
}

// 获得本季度
public static String getThisSeasonTime(int month) {
    int array[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
    int season = 1;
    if (month >= 1 && month <= 3) {
        season = 1;
    }
    if (month >= 4 && month <= 6) {
        season = 2;
    }
    if (month >= 7 && month <= 9) {
        season = 3;
    }
    if (month >= 10 && month <= 12) {
        season = 4;
    }
    int start_month = array[season - 1][0];
    int end_month = array[season - 1][2];

    Date date = new Date();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");// 可以方便地修改日期格式
    String years = dateFormat.format(date);
    int years_value = Integer.parseInt(years);

    int start_days = 1;// years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
    int end_days = getLastDayOfMonth(years_value, end_month);
    String seasonDate = years_value + "-" + start_month + "-" + start_days
            + ";" + years_value + "-" + end_month + "-" + end_days;
    return seasonDate;

}

/**
 * 获取某年某月的最后一天
 *
 * @param year  年
 * @param month 月
 * @return 最后一天
 */
public static int getLastDayOfMonth(int year, int month) {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
            || month == 10 || month == 12) {
        return 31;
    }
    if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
    }
    if (month == 2) {
        if (isLeapYear(year)) {
            return 29;
        } else {
            return 28;
        }
    }
    return 0;
}

/**
 * 是否闰年
 *
 * @param year 年
 * @return
 */
public static boolean isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

/**
 * @param
 * @return String
 * @Description: 得到昨天日期
 */
public static Date lastDay(Date today) {
    return nextNDay(today, -1);
}

/**
 * @param
 * @return String
 * @Description: 得到明天日期
 */
public static Date nextDay(Date today) {
    return nextNDay(today, 1);
}

/**
 * @param period (n 天)
 * @return String
 * @Description: 得到n天后日期
 */
public static Date nextNDay(Date today, int period) {

    long millSecond = 3600000 * 24;
    long lastDayLong = today.getTime() + period * millSecond;
    Date nextDay = new Date(lastDayLong);

    SimpleDateFormat formatter = new SimpleDateFormat(pattern);
    ParsePosition pos = new ParsePosition(0);
    String str = formatter.format(nextDay);
    return formatter.parse(str, pos);
}

public static Date stringToDate(String timeStr, String pattern) {
    if (timeStr == null || timeStr.equals("")) {
        return null;
    }
    Date date = new Date();
    SimpleDateFormat apf = new SimpleDateFormat(pattern);
    try {
        date = apf.parse(timeStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;

}

public static boolean isToday(String today, String dateformat) {
    if (today == null)
        today = "";
    Date date = new Date();
    String str = (new SimpleDateFormat(dateformat)).format(date);
    if (today.equals(str)) {
        return true;
    }
    return false;
}

/**
 * @param day 天数
 * @return Date
 * @Description: 得到几天前的时间
 */
public static Date getDateBefore(int day) {
    Calendar now = Calendar.getInstance();
    now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
    return now.getTime();
}

/**
 * @param day 天数
 * @return Date
 * @Description: 得到几天后的时间
 */
public static Date getDateAfter(int day) {
    Calendar now = Calendar.getInstance();
    now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
    return now.getTime();
}

public static Date getCurrentDate() {
    Calendar cal = Calendar.getInstance();
    Date currDate = cal.getTime();
    return currDate;
}

/**
 * java.util.Date 类型转换为 XMLGregorianCalendar
 *
 * @param date
 * @return XMLGregorianCalendar
 * @throws DatatypeConfigurationException
 */
public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date)
        throws DatatypeConfigurationException {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    XMLGregorianCalendar gc = null;
    try {
        gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
    } catch (DatatypeConfigurationException e) {
        throw e;
    }
    return gc;
}

/**
 * XMLGregorianCalendar 类型转换为 java.util.Date
 *
 * @return Date
 */
public static Date convertToDate(XMLGregorianCalendar xmlCalendar) {

    if (null != xmlCalendar) {
        Calendar c = xmlCalendar.toGregorianCalendar();
        Date d = c.getTime();
        return d;
    } else {
        return null;
    }

}

/**
 * 日期得到年,月
 *
 * @return
 */
public static String getDateMonth(int month) {
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MONTH, month);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
    String preMonth = dateFormat.format(c.getTime());
    return preMonth;
}

/**
 * 得到某个月所有的星期一
 *
 * @return
 */
/*public static List<Date> getMonthWeek(String month) {
    List<Date> list = new ArrayList<Date>();
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    sdf1.setLenient(false);
    for (int i = 1; i < 32; i++) {
        try {
            Date date = sdf1.parse(month + "-" + i);
            int s = DateUtil.dayForWeek(DateUtil.format(date));
            if (s == 1) {
                list.add(date);
                // System.out.println(DateUtil.format(date) + ":" + s);
            }
        } catch (Exception e) {
            // do nothing
        }
    }
    return list;
}
*/

/**
 * 得到某个月所有的星期五
 *
 * @return
 */
public static List<Date> getMonthWeekFiveDay(String month) {
    List<Date> list = new ArrayList<Date>();
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    sdf1.setLenient(false);
    for (int i = 1; i < 32; i++) {
        try {
            Date date = sdf1.parse(month + "-" + i);
            int s = DateUtil.dayForWeek(DateUtil.format(date));
            if (s == 5) {
                list.add(date);
                // System.out.println(DateUtil.format(date) + ":" + s);
            }
        } catch (Exception e) {
            // do nothing
        }
    }
    return list;
}

/**
 * 得到从当日到某年某月某日的所有符合条件(比如:都是周一和周二)的日期
 */
public static List<String> getAllDateToDay(List<String> weekList, String start, String end) {
    List<String> dayList = new ArrayList<String>();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.setTime(strToDate(start));
    Calendar endC = Calendar.getInstance();
    endC.setTime(strToDate(end));
    while (!c.after(endC)) {
        int dayForWeek = 0;
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            dayForWeek = 7;
        } else {
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }
        if (weekList.contains(String.valueOf(dayForWeek))) {
            dayList.add(format.format(c.getTime()));
        }
        c.add(Calendar.DATE, 1);
    }
    return dayList;
}


/**
 * 传入月份,找到这个月的第一个星期五的日期,返回周一的日期
 *
 * @return
 */
public static String getMonthWeekMonday(String month) {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    sdf1.setLenient(false);
    Date fiveDay = null;
    for (int i = 1; i < 32; i++) {
        try {
            Date date = sdf1.parse(month + "-" + i);
            int s = DateUtil.dayForWeek(DateUtil.format(date));
            if (s == 5) {
                fiveDay = date;
                break;
            }
        } catch (Exception e) {
            // do nothing
        }
    }
    return DateUtil.dateToString(DateUtil.nextNDay(fiveDay, -4));
}

/**
 * 传入月份,找到这个月的第一个星期五的日期,返回周天的日期
 *
 * @return
 */
public static String getMonthWeekLastSunday(String month) {
    List<Date> list = new ArrayList<Date>();
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    sdf1.setLenient(false);
    for (int i = 1; i < 32; i++) {
        try {
            Date date = sdf1.parse(month + "-" + i);
            int s = DateUtil.dayForWeek(DateUtil.format(date));
            if (s == 5) {
                list.add(date);
                // System.out.println(DateUtil.format(date) + ":" + s);
            }
        } catch (Exception e) {
            // do nothing
        }
    }
    return DateUtil.dateToString(DateUtil.nextNDay(list.get(list.size() - 1), 2));
}

/**
 * 通过月份得到这个月每周的开始日期及结束日期 (用于人员工作量统计)
 *
 * @param month
 * @return
 */
public static List<String> formart(String month) {
    List<Date> oneWeekList = DateUtil.getMonthWeekFiveDay(month);
    List<String> list = new ArrayList<String>();
    String start = "";   //当月开始时间
    String end = "";    //当月结束时间
    for (int i = 0; i < oneWeekList.size(); i++) {
        String str = DateUtil.dateToString(DateUtil.getFirstDayOfWeek(oneWeekList.get(i)), "yy.MM.dd") + "-" + DateUtil.dateToString(DateUtil.getLastDayOfWeek(oneWeekList.get(i)), "yy.MM.dd");
        if (i == 0) {
            start = DateUtil.dateToString(DateUtil.getFirstDayOfWeek(oneWeekList.get(i)), "yy.MM.dd");
        }
        if (i == oneWeekList.size() - 1) {
            end = DateUtil.dateToString(DateUtil.getLastDayOfWeek(oneWeekList.get(i)), "yy.MM.dd");
        }
        list.add(str);
    }
    if (oneWeekList.size() == 4) {
        list.add("");
    }
    String s = start + "-" + end;
    list.add(s);
    return list;
}


/**
 * 通过月份得到这个月每周的开始日期及结束日期 (用于人员工作量统计)
 * 不包含当月的开始和结束时间
 *
 * @param month
 * @return
 */
public static List<Map<String, Object>> formartTwo(String month) {
    List<Date> oneWeekList = DateUtil.getMonthWeekFiveDay(month);
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < oneWeekList.size(); i++) {
        Map<String, Object> map = new HashMap<String, Object>();
        String str = DateUtil.dateToString(DateUtil.getFirstDayOfWeek(oneWeekList.get(i)), "MM.dd") + "-" + DateUtil.dateToString(DateUtil.getLastDayOfWeek(oneWeekList.get(i)), "MM.dd");
        map.put("name", str);
        map.put("value", DateUtil.dateToString(DateUtil.getLastDayOfWeek(oneWeekList.get(i))));
        list.add(map);
    }
    return list;
}

// 将日期的时分秒转换为 23:59:00
public static Date getCustomizationTime(Date date) {
    SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd 23:59:00");
    SimpleDateFormat dd2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String date1 = dd.format(getLastDayOfWeek(date));
    Date date2 = null;
    try {
        date2 = dd2.parse(date1);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date2;
}


/**
 * 得到某个月第一个星期五
 *
 * @param month 月份
 * @return
 */
public static Date getFirstFiveDay(String month) {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    sdf1.setLenient(false);
    Calendar lastDate = Calendar.getInstance();
    try {
        lastDate.setTime(sdf1.parse(month + "-01"));
    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
    lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天
    for (int i = 1; i <= lastDate.get(Calendar.DATE); i++) {
        try {
            Date date = sdf1.parse(month + "-" + i);
            int s = DateUtil.dayForWeek(DateUtil.format(date));
            if (s == 5) {
                return date;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return new Date();
}

/**
 * 得到某个月最后一个星期五
 *
 * @param month 月份
 * @return
 */
public static Date getLastFiveDay(String month) {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    sdf1.setLenient(false);
    Calendar lastDate = Calendar.getInstance();
    try {
        lastDate.setTime(sdf1.parse(month + "-01"));
    } catch (ParseException e1) {
        e1.printStackTrace();
    }
    lastDate.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
    lastDate.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天
    for (int i = lastDate.get(Calendar.DATE); i > 0; i--) {
        try {
            Date date = sdf1.parse(month + "-" + i);
            int s = DateUtil.dayForWeek(DateUtil.format(date));
            if (s == 5) {
                return date;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return new Date();
}


/**
 * 查询指定日期与当前日期之间星期天的个数
 */
public static int getWeekCount(String startDate) {
    int sunDaySum = 0;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar start = Calendar.getInstance();
    Calendar end = Calendar.getInstance();
    start.setTime(strToDate(startDate));
    end.setTime(getPreviousWeekSunday());
    while (start.compareTo(end) <= 0) {
        int w = start.get(Calendar.DAY_OF_WEEK);
        if (w == Calendar.SUNDAY) {
            sunDaySum++;
            // System.out.println(format.format(start.getTime()));//打印每天
        }
        start.set(Calendar.DATE, start.get(Calendar.DATE) + 1);//循环,每次天数加1
    }
    //System.out.println("星期天总数为:" + sunDaySum);
    return sunDaySum;
}

public static List<Date> getMonthWeekSunday(String month) {
    List<Date> list = new ArrayList<Date>();
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    sdf1.setLenient(false);
    for (int i = 1; i < 32; i++) {
        try {
            Date date = sdf1.parse(month + "-" + i);
            int s = DateUtil.dayForWeek(DateUtil.format(date));
            if (s == 7) {
                list.add(date);
                // System.out.println(DateUtil.format(date) + ":" + s);
            }
        } catch (Exception e) {
            // do nothing
        }
    }
    return list;
}

/*根据月获取周,第一周按1日开始计算
 * 第一周:04.01-04.05    #星期三至星期日
 * 第二周:04.06-04.12    #星期一至星期日
 * .......
 * 第五周:04.27-04.28    #星期1至星期二
 */
public static List<Map<String, Object>> formatWeekDateForMonth(String month) {
    List<Date> oneWeekList = DateUtil.getMonthWeekSunday(month);
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    String tyear = month.split("-")[0];
    String tmonth = month.split("-")[1];
    for (int i = 0; i <= oneWeekList.size(); i++) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (i == 0) {
            String firstday = tmonth + "." + "01";
            String endday = DateUtil.dateToString(oneWeekList.get(i), "MM.dd");
            map.put("name", firstday + "-" + endday);
            map.put("value", DateUtil.dateToString(oneWeekList.get(i)));
            list.add(map);
        } else if (i == oneWeekList.size()) {
            int iyear = Integer.parseInt(tyear);
            int imonth = Integer.parseInt(tmonth);
            int endday = DateUtil.getLastDayOfMonth(iyear, imonth);
            Date enddate = DateUtil.strToDate(month + "-" + endday);
            if (oneWeekList.get(i - 1).compareTo(enddate) < 0) {
                String start = DateUtil.dateToString(DateUtil.nextNDay(oneWeekList.get(i - 1), 1), "MM.dd");
                String end = DateUtil.dateToString(enddate, "MM.dd");
                map.put("name", start + "-" + end);
                map.put("value", DateUtil.dateToString(enddate));
                list.add(map);
            }
        } else {
            String str = DateUtil.dateToString(DateUtil.getFirstDayOfWeek(oneWeekList.get(i)), "MM.dd") + "-" + DateUtil.dateToString(DateUtil.getLastDayOfWeek(oneWeekList.get(i)), "MM.dd");
            map.put("name", str);
            map.put("value", DateUtil.dateToString(DateUtil.getLastDayOfWeek(oneWeekList.get(i))));
            list.add(map);
        }
    }
    return list;
}

/**
 * 获得某月某周第一天时间(第一周第一天和最后一周第一天是关键点)
 * 某月1日为 第一周起点,
 * 2017.03.01-03.05 第一周  (星期三到星期日)
 */
public static String getFirstDayForWeek(String submitTime) {
    String startDate = "";
    Date temp = DateUtil.strToDate(submitTime);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    String month = sdf.format(temp);
    List<Map<String, Object>> list = DateUtil.formatWeekDateForMonth(month);
    String firstWeek = (String) list.get(0).get("value");
    String lastWeek = (String) list.get(list.size() - 1).get("value");
    if (submitTime.equals(firstWeek)) {
        //第一周第一天处理
        startDate = month + "-01";
    } else if (submitTime.compareTo(lastWeek) > 0) {
        //最后一周第一天处理
        Date temp1 = DateUtil.nextNDay(DateUtil.stringToDate(lastWeek, "yyyy-MM-dd"), 1);
        startDate = DateUtil.dateToString(temp1);
    } else {
        //其它完整周处理
        Date temp2 = DateUtil.getFirstDayOfWeek(DateUtil.stringToDate(submitTime, "yyyy-MM-dd"));
        startDate = DateUtil.dateToString(temp2);
    }
    return startDate;
}

//获得一月最后一天
public static String getLastDayForMonth(String month) {
    String tyear = month.split("-")[0];
    String tmonth = month.split("-")[1];
    String lastday = month + "-" + DateUtil.getLastDayOfMonth(Integer.parseInt(tyear), Integer.parseInt(tmonth));
    return lastday;
}


/**
 * 查询指定的两个日期之间星期天的个数   add 2017-06-20 iceHuang
 */
public static int getWeekCountBetweenTwoDate(String startDate, String endDate) {
    int sunDaySum = 0;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Calendar start = Calendar.getInstance();
    Calendar end = Calendar.getInstance();
    start.setTime(strToDate(startDate));
    end.setTime(strToDate(endDate));
    while (start.compareTo(end) <= 0) {
        int w = start.get(Calendar.DAY_OF_WEEK);
        if (w == Calendar.SUNDAY) {
            sunDaySum++;
            // System.out.println(format.format(start.getTime()));//打印每天
        }
        start.set(Calendar.DATE, start.get(Calendar.DATE) + 1);//循环,每次天数加1
    }
    //System.out.println("星期天总数为:" + sunDaySum);
    return sunDaySum;
}

/**
 * 生成16位不重复的随机数,含数字+大小写
 *
 * @return
 */
public static String getGUID() {
    StringBuilder uid = new StringBuilder();
    //产生16位的强随机数
    Random rd = new SecureRandom();
    for (int i = 0; i < 16; i++) {
        //产生0-2的3位随机数
        int type = rd.nextInt(3);
        switch (type) {
            case 0:
                //0-9的随机数
                uid.append(rd.nextInt(10));
                break;
            case 1:
                //ASCII在65-90之间为大写,获取大写随机
                uid.append((char) (rd.nextInt(25) + 65));
                break;
            case 2:
                //ASCII在97-122之间为小写,获取小写随机
                uid.append((char) (rd.nextInt(25) + 97));
                break;
            default:
                break;
        }
    }
    return uid.toString();
}
}

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

群峦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值