Java时间工具类

- 日理万妓 2021-10-03 02:50 530阅读 0赞

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**

  • 时间工具类
    */
    public class DateUtil {

    /**

    • 开始时间
      /
      public static final Long Start = System.currentTimeMillis();
      /
      *
    • 结束时间
      /
      public static final Long End = System.currentTimeMillis();
      /
      *
    • 缺省的日期显示格式: yyyy-MM-dd
      /
      public static final String DEFAULT_DATE_FORMAT = “yyyy-MM-dd”;
      /
      *
    • 缺省的日期显示格式: yyyy-MM
      /
      public static final String DEFAULT_YYMM_FORMAT = “yyyy-MM”;
      /
      *
    • 缺省的日期时间显示格式:yyyy-MM-dd HH:mm:ss
      /
      public static final String DEFAULT_DATETIME_FORMAT = “yyyy-MM-dd HH:mm:ss”;
      /
      *
    • 缺省的日期时间显示格式:yyyy-MM-dd HH:mm:ss:sss
      /
      public static final String YMDHMSS = “yyyy-MM-dd HH:mm:ss:sss”;
      /
      *
    • 字符串时间
      /
      public static final String YMDHMS = “yyyyMMddHHmmss”;
      /
      *
    • 当前时间
      */
      public static Date DATE_NOW = new Date();

    private static final String DEFAULT_CONVERT_PATTERN = “yyyyMMddHHmmssSSS”;

    /**

    • 获取当前时间字符串(默认格式:yyyyMMddHHmmssSSS)
    • @return
      */
      public static String getCurrentTimeStrDefault() {
      return getCurrentTimeStr(DEFAULT_CONVERT_PATTERN);
      }

    /**

    • 获取指定时间字符串(默认格式:yyyyMMddHHmmssSSS)
    • @param date
    • @return
      */
      public static String getTimeStrDefault(Date date) {
      SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_CONVERT_PATTERN);
      return dateFormat.format(date);
      }

    /**

    • 获取当前时间字符串
    • @param pattern 转换格式
    • @return
      */
      public static String getCurrentTimeStr(String pattern) {
      SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
      return dateFormat.format(new Date());
      }

    /**

    • 获取指定时间字符串
    • @param date
    • @return
      */
      public static String getTimeStr(Date date, String pattern) {
      SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
      return dateFormat.format(date);
      }

    /**

    • 判断时间字符串是否为默认格式
    • @param dateTimeStr
    • @return
      */
      public static boolean isValidDefaultFormat(String dateTimeStr) {
      SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_CONVERT_PATTERN);
      try {
      dateFormat.parse(dateTimeStr);
      return true;
      } catch (Exception e) {
      // 如果抛出异常,说明格式不正确
      return false;
      }
      }

    /**

    • 私有构造方法,禁止对该类进行实例化
      */
      private DateUtil() {
      }

    /**

    • 获取开始时间
    • @return 当前日期时间
      */
      public static Long Start() {
      return Long.valueOf(Start);
      }

    /**

    • 获取结束时间
    • @return 当前日期时间
      */
      public static Long End() {
      return Long.valueOf(End);
      }

    /**

    • 开始时间与结束时间差
    • @return 时间差
      */
      public static Long dif(Long End, Long Start) {
      return (End - Start);
      }

    /**

    • 得到系统当前日期时间
    • @return 当前日期时间
      */
      public static Date getNow() {
      return Calendar.getInstance().getTime();
      }

    /**

    • 得到用缺省方式格式化的当前日期
    • @return 当前日期
      */
      public static String getDate() {
      return getDateTime(DEFAULT_DATE_FORMAT);
      }

    /**

    • 得到用缺省方式格式化的当前日期及时间
    • @return 当前日期及时间
      */
      public static String getDateTime() {
      return getDateTime(DEFAULT_DATETIME_FORMAT);
      }

    /**

    • 得到系统当前日期及时间,并用指定的方式格式化
    • @param pattern 显示格式
    • @return 当前日期及时间
      */
      public static String getDateTime(String pattern) {
      Date datetime = Calendar.getInstance().getTime();
      return getDateTime(datetime, pattern);
      }

    /**

    • 得到用指定方式格式化的日期
    • @param date 需要进行格式化的日期
    • @param pattern 显示格式
    • @return 日期时间字符串
      */
      public static String getDateTime(Date date, String pattern) {
      if (null == pattern || “”.equals(pattern)) {
      pattern = DEFAULT_DATETIME_FORMAT;
      }
      SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
      return dateFormat.format(date);
      }

    /**

    • 得到当前年份
    • @return 当前年份
      */
      public static int getCurrentYear() {
      return Calendar.getInstance().get(Calendar.YEAR);
      }

    /**

    • 得到当前月份
    • @return 当前月份
      */
      public static int getCurrentMonth() {
      // 用get得到的月份数比实际的小1,需要加上
      return Calendar.getInstance().get(Calendar.MONTH) + 1;
      }

    /**

    • 得到当前日
    • @return 当前日
      */
      public static int getCurrentDay() {
      return Calendar.getInstance().get(Calendar.DATE);
      }

    public static Date getCurrentToDay() {
    return parse(getDate(), DEFAULT_DATE_FORMAT);
    }

    /**

    • 取得当前日期以后若干天的日期。如果要得到以前的日期,参数用负数。 例如要得到上星期同一天的日期,参数则为-7
    • @param days 增加的日期数
    • @return 增加以后的日期
      */
      public static Date addDays(int days) {
      return add(getNow(), days, Calendar.DATE);
      }

    /**

    • 取得指定日期以后若干天的日期。如果要得到以前的日期,参数用负数。
    • @param date 基准日期
    • @param days 增加的日期数
    • @return 增加以后的日期
      */
      public static Date addDays(Date date, int days) {
      return add(date, days, Calendar.DATE);
      }

    /**

    • 取得当前日期以后某月的日期。如果要得到以前月份的日期,参数用负数。
    • @param months 增加的月份数
    • @return 增加以后的日期
      */
      public static Date addMonths(int months) {
      return add(getNow(), months, Calendar.MONTH);
      }

    /**

    • 取得指定日期以后某月的日期。如果要得到以前月份的日期,参数用负数。 注意,可能不是同一日子,例如2003-1-31加上一个月是2003-2-28
    • @param date 基准日期
    • @param months 增加的月份数
    • @return 增加以后的日期
      */
      public static Date addMonths(Date date, int months) {
      return add(date, months, Calendar.MONTH);
      }

    /**

    • 内部方法。为指定日期增加相应的天数或月数
    • @param date 基准日期
    • @param amount 增加的数量
    • @param field 增加的单位,年,月或者日
    • @return 增加以后的日期
      */
      private static Date add(Date date, int amount, int field) {
      Calendar calendar = Calendar.getInstance();

      calendar.setTime(date);
      calendar.add(field, amount);

      return calendar.getTime();
      }

    /**

    • 计算两个日期相差天数。 用第一个日期减去第二个。如果前一个日期小于后一个日期,则返回负数
    • @param one 第一个日期数,作为基准
    • @param two 第二个日期数,作为比较
    • @return 两个日期相差天数
      */
      public static long diffDays(Date one, Date two) {
      return (one.getTime() - two.getTime()) / (24 * 60 * 60 * 1000);
      }

    /**

    • 计算两个日期相差天数。 用第一个日期减去第二个。如果前一个日期小于后一个日期,则返回负数
    • @param one 第一个日期数,作为基准
    • @param two 第二个日期数,作为比较
    • @return 两个日期相差天数
      */
      public static long diffDay(Date one, Date two) {
      Calendar calendar = Calendar.getInstance();

      // 得到第一个日期的年分和月份数
      calendar.setTime(one);
      int oneday = calendar.get(Calendar.DAY_OF_YEAR);

      // 得到第二个日期的年份和月份
      calendar.setTime(two);
      int twoday = calendar.get(Calendar.DAY_OF_YEAR);

      return twoday - oneday;
      }

    /**

    • 计算两个日期相差月份数 如果前一个日期小于后一个日期,则返回负数
    • @param one 第一个日期数,作为基准
    • @param two 第二个日期数,作为比较
    • @return 两个日期相差月份数
      */
      public static int diffMonths(Date one, Date two) {

      Calendar calendar = Calendar.getInstance();

      // 得到第一个日期的年分和月份数
      calendar.setTime(one);
      int yearOne = calendar.get(Calendar.YEAR);
      int monthOne = calendar.get(Calendar.MONDAY);

      // 得到第二个日期的年份和月份
      calendar.setTime(two);
      int yearTwo = calendar.get(Calendar.YEAR);
      int monthTwo = calendar.get(Calendar.MONDAY);

      return (yearOne - yearTwo) * 12 + (monthOne - monthTwo);
      }

    /**

    • 将一个字符串用给定的格式转换为日期类型。

    • 注意:如果返回null,则表示解析失败

    • @param datestr 需要解析的日期字符串
    • @param pattern 日期字符串的格式,默认为“yyyy-MM-dd”的形式
    • @return 解析后的日期
      */
      public static Date parse(String datestr, String pattern) {
      Date date = null;

      if (null == pattern || “”.equals(pattern)) {
      pattern = DEFAULT_DATE_FORMAT;
      }

      try {
      SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
      date = dateFormat.parse(datestr);
      } catch (ParseException e) {
      //
      }

      return date;
      }

    /**

    • 日期转成指定类型字符串
    • @param date
    • @param pattern
    • @return
    • @author 000386
      */
      public static String format(Date date, String pattern) {
      if (date != null) {
      if (null == pattern || “”.equals(pattern)) {
      pattern = DEFAULT_DATETIME_FORMAT;
      }
      SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
      return dateFormat.format(date);

      }
      return null;
      }

    /**

    • 返回本月的最后一天
    • @return 本月最后一天的日期
      */
      public static Date getMonthLastDay() {
      return getMonthLastDay(getNow());
      }

    /**

    • 返回给定日期中的月份中的最后一天
    • @param date 基准日期
    • @return 该月最后一天的日期
      */
      public static Date getMonthLastDay(Date date) {

      Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);

      // 将日期设置为下一月第一天
      calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 1);

      // 减去1天,得到的即本月的最后一天
      calendar.add(Calendar.DATE, -1);

      return calendar.getTime();
      }

    public static int compareDate(Date date1, Date date2) {

    1. if (null == date1 && date2 == null) {
    2. return 0;
    3. }
    4. try {
    5. Date dt1 = DateUtil.parse(DateUtil.getDateTime(date1, DateUtil.DEFAULT_DATETIME_FORMAT),
    6. DateUtil.DEFAULT_DATETIME_FORMAT);
    7. Date dt2 = DateUtil.parse(DateUtil.getDateTime(date2, DateUtil.DEFAULT_DATETIME_FORMAT),
    8. DateUtil.DEFAULT_DATETIME_FORMAT);
    9. if (dt1.getTime() > dt2.getTime()) {
    10. return 1;
    11. } else if (dt1.getTime() < dt2.getTime()) {
    12. return -1;
    13. } else {
    14. return 0;
    15. }
    16. } catch (Exception exception) {
    17. }
    18. return 0;

    }

    /**

    • 计算两个日期之间相差的天数
    • @param smdate 较小的时间
    • @param bdate 较大的时间
    • @return 相差天数
    • @throws ParseException
      */
      public static int daysBetween(Date smdate, Date bdate) {

      try {
      SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
      smdate = sdf.parse(sdf.format(smdate));
      bdate = sdf.parse(sdf.format(bdate));
      Calendar cal = Calendar.getInstance();
      cal.setTime(smdate);
      long time1 = cal.getTimeInMillis();
      cal.setTime(bdate);
      long time2 = cal.getTimeInMillis();
      long between_days = (time2 - time1) / (1000 * 3600 * 24);

      1. return Integer.parseInt(String.valueOf(between_days));

      } catch (ParseException e) {
      e.printStackTrace();
      }
      return -1;
      }

    /**

    • 计算date增加days天后的日期
    • @param days
    • @return
      */
      @SuppressWarnings(“static-access”)
      public static Date dateAddDays(Date date, int days) {
      Calendar calendar = new GregorianCalendar();
      calendar.setTime(date);
      calendar.add(calendar.DATE, days);
      return calendar.getTime();
      }

    /**

    • 计算时间差返回秒
    • @param bTime
    • @param sTime
    • @return
      */
      public static Long diffTime(Date bTime, Date sTime) {
      return (bTime.getTime() - sTime.getTime()) / 1000;
      }

    /**

    • 得到完整的时间戳,格式:yyyy-MM-dd HH:mm:ss.SSS(年-月-日 时:分:秒.毫秒)
    • @return 完整的时间戳
      */
      public static String getFullTimeStamp() {
      return new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss.SSS”).format(new Date());
      }

    /**

    • 得到简单的时间戳,格式:yyyyMMdd(年月日)
    • @return 简单的时间戳
      */
      public static String getSimpleTimeStamp() {
      return new SimpleDateFormat(“yyyy-MM-dd”).format(new Date());
      }

    /**

    • 根据指定的格式得到时间戳
    • @param pattern 指定的格式
    • @return 指定格式的时间戳
      */
      public static String getTimeStampByPattern(String pattern) {
      return new SimpleDateFormat(pattern).format(new Date());
      }

    /**

    • 得到当前日期格式化后的字符串,格式:yyyy-MM-dd(年-月-日)
    • @return 当前日期格式化后的字符串
      */
      public static String getTodayStr() {
      return new SimpleDateFormat(“yyyy-MM-dd”).format(new Date());
      }

    /**

    • 时间戳,格式:yyyy-MM-dd HH:mm:ss(年-月-日 时:分:秒)
    • @return 简单的时间戳
      */
      public static String getDateTimeStamp(Date date) {
      return new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”).format(date);
      }

    /**

    • @param
    • @return java.lang.String
    • @Description 年月日时分秒毫秒
    • @author QiMing
    • @date 2018/8/23 14:45
      */
      public static String YMDHMS() {
      return getDateTime(YMDHMSS);
      }

    /**

    • @param time
    • @return java.lang.Long
    • @Description 转换10位时间戳加1天
    • @author QiMing
    • @date 2018/8/23 15:10
      */
      public static Long ten(Date time) {

      Calendar calendar = Calendar.getInstance();
      calendar.setTime(time);
      calendar.add(Calendar.DAY_OF_YEAR, 1);
      Date date = calendar.getTime();
      Long s = date.getTime() / 1000;
      String timestamp = String.format(“%010d”, s);
      Long ss = Long.valueOf(timestamp);
      return ss;
      }

    /**

    • @param time
    • @return java.lang.Long
    • @Description Date类型转long类型 10位
    • @author QiMing
    • @date 2018/8/28 15:25
      */
      public static Long changeTen(Date time) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(time);
      Date date = calendar.getTime();
      Long s = date.getTime() / 1000;
      String timestamp = String.format(“%010d”, s);
      Long ss = Long.valueOf(timestamp);
      return ss;
      }

    /**

    • 时间戳,格式:yyyy-MM-dd HH:mm(年-月-日 时:分)
    • @return 简单的时间戳
      */
      public static String getYmdhm(Date date) {
      return new SimpleDateFormat(“yyyy-MM-dd HH:mm”).format(date);
      }

    /**

    • 加15分钟
    • @param time
    • @return
      */
      public static Long payFive(Date time) {
      long etime1 = (time.getTime() + 15 * 60 * 1000)/1000;//延时函数,单位毫秒,这里是延时了15分钟
      /SimpleDateFormat time2 = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
      String etime = time2.format(new Date(etime1));
      /
      String timestamp = String.format(“%010d”, etime1);
      Long ss = Long.valueOf(timestamp);
      return ss;
      }

    /**

    • 减15分钟
    • @param time
    • @return
      */
      public static Long lessFive(Date time) {
      long etime1 = (time.getTime() - 15 * 60 * 1000)/1000;//延时函数,单位毫秒,这里是延时了15分钟
      /SimpleDateFormat time2 = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
      String etime = time2.format(new Date(etime1));
      /
      String timestamp = String.format(“%010d”, etime1);
      Long ss = Long.valueOf(timestamp);
      return ss;
      }

    /**

    • 加15分钟返回时分秒
      /
      public static String dateFive(Date time) {
      long etime1 = (time.getTime() + 15 \
      60 * 1000);//延时函数,单位毫秒,这里是延时了15分钟
      /SimpleDateFormat time2 = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
      String etime = time2.format(new Date(etime1));/
      / String timestamp = String.format(“%010d”, etime1);
      Long ss = Long.valueOf(timestamp);\
      /

      SimpleDateFormat time2 = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
      String etime = time2.format(new Date(etime1));
      return etime;
      }

    public static String LessTenFive(Date time) {
    long etime1 = (time.getTime() - 15 * 60 * 1000);//延时函数,单位毫秒,这里是延时了15分钟
    /SimpleDateFormat time2 = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
    String etime = time2.format(new Date(etime1));
    /
    /* String timestamp = String.format(“%010d”, etime1);
    Long ss = Long.valueOf(timestamp);*/

    1. SimpleDateFormat time2 = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
    2. String etime = time2.format(new Date(etime1));
    3. return etime;

    }

    /**

    • 加7天
    • @param time
    • @return
      */
      public static Long SevenDay(Date time) {
      long etime1 = (time.getTime() + 7 * 24 * 60 * 60 * 1000)/1000;//延时函数,单位毫秒,这里是延时了7天
      /SimpleDateFormat time2 = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
      String etime = time2.format(new Date(etime1));
      /
      String timestamp = String.format(“%010d”, etime1);
      Long ss = Long.valueOf(timestamp);
      return ss;
      }

/**
* 加15分钟转成时间戳10位
*/
public static Long payTenFive(Date time) {
long etime1 = (time.getTime() + 15 * 60 * 1000) / 1000;//延时函数,单位毫秒,这里是延时了15分钟
String timestamp = String.format(“%010d”, etime1);
Long ss = Long.valueOf(timestamp);
return ss;
}

  1. /**
  2. * 时分
  3. *
  4. * @param str
  5. * @return
  6. */
  7. public static Date changeYMDHM(String str) {
  8. Date date = null;
  9. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  10. try {
  11. date = sdf.parse(str);
  12. } catch (ParseException e) {
  13. e.printStackTrace();
  14. }
  15. return date;
  16. }
  17. /**
  18. * 转换13位
  19. * @param time
  20. * @return
  21. */
  22. public static Long change13(Date time){
  23. Calendar calendar = Calendar.getInstance();
  24. calendar.setTime(time);
  25. Date date = calendar.getTime();
  26. Long s = date.getTime();
  27. return s;
  28. }
  29. /**
  30. * 转换10位
  31. */
  32. public static Long changeString(String time){
  33. Date date = changeYMDHM(time);
  34. long sss = (date.getTime()-60*60*1000)/1000;
  35. String timestamp = String.format("%010d", sss);
  36. long ss = Long.valueOf(timestamp);
  37. return ss;
  38. }
  39. /**
  40. * @Title stringToDate
  41. * @Description
  42. * @param time
  43. * @return java.util.Date
  44. * @author QiMing
  45. * @date 2018/12/28 12:29
  46. */
  47. public static Date stringToDate(String time) {
  48. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  49. Date date = new Date();
  50. try {
  51. date = sdf.parse(time);
  52. } catch (ParseException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. return date;
  57. }
  58. /**
  59. * 转换10位
  60. */
  61. public static Long tenNow(String time){
  62. SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
  63. Date date = new Date();
  64. try {
  65. date = sdf.parse(time);
  66. } catch (ParseException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }
  70. long sss = (date.getTime())/1000;
  71. String timestamp = String.format("%010d", sss);
  72. long ss = Long.valueOf(timestamp);
  73. return ss;
  74. }
  75. /**
  76. * String类型转换Date类型
  77. *
  78. * @param time
  79. * @return date
  80. */
  81. public static Date change(String time) {
  82. Date date = null;
  83. SimpleDateFormat sdf = new SimpleDateFormat(YMDHMS);
  84. try {
  85. date = sdf.parse(time);
  86. } catch (ParseException e) {
  87. // TODO Auto-generated catch block
  88. e.printStackTrace();
  89. }
  90. return date;
  91. }
  92. /**
  93. * 字符串转Date
  94. * @param str
  95. * @return
  96. */
  97. public static Date getDateByString(String str) {
  98. SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  99. Date dateTime = null;
  100. try {
  101. dateTime = sim.parse(str);
  102. } catch (ParseException e) {
  103. e.printStackTrace();
  104. }
  105. return dateTime;
  106. }
  107. /**
  108. * 自身时间加一天
  109. *
  110. * @param time
  111. * @return
  112. */
  113. public static Date plusOne(Date time) {
  114. Calendar calendar = Calendar.getInstance();
  115. calendar.setTime(time);
  116. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  117. calendar.add(Calendar.DAY_OF_YEAR, 1);
  118. Date date = calendar.getTime();
  119. return date;
  120. }
  121. /**
  122. * 自身时间减一天
  123. *
  124. * @param time
  125. * @return
  126. */
  127. public static Date minusOne(Date time) {
  128. Calendar calendar = Calendar.getInstance();
  129. calendar.setTime(time);
  130. calendar.add(Calendar.DAY_OF_YEAR, -1);
  131. Date date = calendar.getTime();
  132. return date;
  133. }
  134. /**
  135. * @param time, now
  136. * @return java.lang.Long
  137. * @Description 获取时间戳加一天差集
  138. * @author QiMing
  139. * @date 2018/8/22 14:09
  140. */
  141. public static Long plustime(Date time) {
  142. Calendar calendar = Calendar.getInstance();
  143. calendar.setTime(time);
  144. calendar.add(Calendar.DAY_OF_YEAR, 1);
  145. Date date = calendar.getTime();
  146. Date now = new Date();
  147. long t = date.getTime();
  148. long n = now.getTime();
  149. long s = t - n;
  150. return s;
  151. }
  152. /**
  153. * @param time
  154. * @return java.lang.Long
  155. * @Description 转换时间戳加一天
  156. * @author QiMing
  157. * @date 2018/8/22 14:47
  158. */
  159. public static Long Onetime(Date time) {
  160. Calendar calendar = Calendar.getInstance();
  161. calendar.setTime(time);
  162. calendar.add(Calendar.DAY_OF_YEAR, 1);
  163. Date date = calendar.getTime();
  164. Long s = date.getTime();
  165. return s;
  166. }

/**
* 判断是否润年
*/
public static boolean isLeapYear(String ddate) {

  1. /**
  2. * 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
  3. * 3.能被4整除同时能被100整除则不是闰年
  4. */
  5. Date d = strToDate(ddate);
  6. GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
  7. gc.setTime(d);
  8. int year = gc.get(Calendar.YEAR);
  9. if ((year % 400) == 0)
  10. return true;
  11. else if ((year % 4) == 0) {
  12. if ((year % 100) == 0)
  13. return false;
  14. else
  15. return true;
  16. } else
  17. return false;
  18. }

}

发表评论

表情:
评论列表 (有 0 条评论,530人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java 时间工具

    借鉴网上资源与工作中经验整理的时间工具类(主要针对java.util.Date的使用,java8后的时间工具下期整理),欢迎大家一起完善补充。 import

    相关 Java时间工具

    该时间工具类功能:时间戳格式化至毫秒、时间戳格式化至秒、时间戳格式化至日、时间戳格式化成时和分、Date对象格式化至毫秒、Date对象格式化至秒、Date对象格式化至日、Da

    相关 java时间工具

    在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作。一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度。 /