[Java] Java操作日期时间范例代码大全 →→→→→进入此内容的聊天室

来自 , 2019-12-09, 写在 Java, 查看 115 次.
URL http://www.code666.cn/view/8ce1a43f
  1. package com.xtgd.main.util;
  2.  
  3. import java.sql.Timestamp;
  4. import java.text.DateFormat;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.Date;
  10. import java.util.List;
  11. import java.util.Locale;
  12.  
  13. import org.apache.commons.lang.StringUtils;
  14.  
  15. /**
  16.  * 日期工具类
  17.  *
  18.  * @author yugc
  19.  */
  20. public final class DateUtil {
  21.  
  22.     private DateUtil() {
  23.  
  24.     }
  25.  
  26.     /**
  27.      * yyyy-MM-dd
  28.      */
  29.     public static final String FORMAT1 = "yyyy-MM-dd";
  30.  
  31.     /**
  32.      * yyyy.MM.dd
  33.      */
  34.     public static final String FORMAT2 = "yyyy.MM.dd";
  35.  
  36.     /**
  37.      * yyyy/MM/dd
  38.      */
  39.     public static final String FORMAT3 = "yyyy/MM/dd";
  40.  
  41.     /**
  42.      * yyyy-MM-dd HH:mm
  43.      */
  44.     public static final String FORMAT4 = "yyyy-MM-dd HH:mm";
  45.  
  46.     /**
  47.      * yyyy.MM.dd HH:mm
  48.      */
  49.     public static final String FORMAT5 = "yyyy.MM.dd HH:mm";
  50.  
  51.     /**
  52.      * yyyy/MM/dd HH:mm
  53.      */
  54.     public static final String FORMAT6 = "yyyy/MM/dd HH:mm";
  55.  
  56.     /**
  57.      * yyyy-MM-dd HH:mm:ss
  58.      */
  59.     public static final String FORMAT7 = "yyyy-MM-dd HH:mm:ss";
  60.  
  61.     /**
  62.      * YYYY-MM-dd HH-mm-ss
  63.      */
  64.     public static final String FORMAT15 = "YYYY-MM-dd HH-mm-ss";
  65.  
  66.     /**
  67.      * yyyy.MM.dd HH:mm:ss
  68.      */
  69.     public static final String FORMAT8 = "yyyy.MM.dd HH:mm:ss";
  70.  
  71.     /**
  72.      * yyyy/MM/dd HH:mm:ss
  73.      */
  74.     public static final String FORMAT9 = "yyyy/MM/dd HH:mm:ss";
  75.  
  76.     /**
  77.      * yyyy_MM_dd_HH_mm_ss
  78.      */
  79.     public static final String FORMAT10 = "yyyy_MM_dd_HH_mm_ss";
  80.  
  81.     /**
  82.      * yy-MM-dd
  83.      */
  84.     public static final String FORMAT11 = "yy-MM-dd";
  85.  
  86.     /**
  87.      * yyyyMMdd
  88.      */
  89.     public static final String FORMAT12 = "yyyyMMdd";
  90.  
  91.     /**
  92.      * yyyyMMddHHmmss
  93.      */
  94.     public static final String FORMAT13 = "yyyyMMddHHmmss";
  95.  
  96.     /**
  97.      * yyyyMM
  98.      */
  99.     public static final String FORMAT14 = "yyyyMM";
  100.  
  101.     public static Date getCurrentDate() {
  102.  
  103.         return new Date(System.currentTimeMillis());
  104.     }
  105.  
  106.     public static Date getYesterDay() {
  107.  
  108.         return addDay(new Date(System.currentTimeMillis()), -1);
  109.     }
  110.  
  111.     public static String getYesterDayString() {
  112.  
  113.         return parseDateToString(addDay(new Date(System.currentTimeMillis()), -1), FORMAT1);
  114.     }
  115.  
  116.     /**
  117.      * 得到年月日的路径
  118.      * @return
  119.      */
  120.     public static String getThisYearMonthDay(String dateString) {
  121.  
  122.         Date date = parseStringToDate(dateString, FORMAT12);
  123.  
  124.         return DateUtil.getYear(date) + "/" + DateUtil.getMonth(date) + "/" + DateUtil.getDay(date) + "/";
  125.     }
  126.  
  127.     /**
  128.      * 返回当前日期或时间
  129.      *
  130.      * @param format
  131.      * @return
  132.      */
  133.     public static String getCurrentDate(String format) {
  134.  
  135.         if (StringUtils.isBlank(format)) {
  136.             format = FORMAT1;
  137.         }
  138.  
  139.         Date date = new Date();
  140.  
  141.         SimpleDateFormat formatter = new SimpleDateFormat(format);
  142.  
  143.         String currentTime = formatter.format(date);
  144.  
  145.         return currentTime;
  146.     }
  147.  
  148.     /**
  149.      * 返回当前时间
  150.      *
  151.      * @return
  152.      */
  153.     public static String getCurrentTime() {
  154.  
  155.         String datetime = getCurrentDate(FORMAT7);
  156.         String time = datetime.substring(datetime.indexOf(" ") + 1);
  157.  
  158.         return time;
  159.     }
  160.  
  161.     /**
  162.      * 返回当前时间加随机数
  163.      *
  164.      * @return
  165.      */
  166.     public static String getCurrentDateTimeRandom() {
  167.  
  168.         String datetime = getCurrentDate(DateUtil.FORMAT10) + "_" + Math.random();
  169.  
  170.         return datetime;
  171.     }
  172.  
  173.     /**
  174.      * 返回当前时间和日期
  175.      * @return
  176.      */
  177.     public static String getCurrentDateTimeString() {
  178.  
  179.         return getCurrentDate(DateUtil.FORMAT7);
  180.  
  181.     }
  182.  
  183.     /**
  184.      * 返回当前日期
  185.      *
  186.      * @return
  187.      */
  188.     public static Date getCurrentDateTime() {
  189.  
  190.         String datetime = getCurrentDate(FORMAT7);
  191.  
  192.         return parseStringToDate(datetime, "");
  193.     }
  194.  
  195.     public static Timestamp getCurrentTimestamp() {
  196.  
  197.         String datetime = getCurrentDate(FORMAT7);
  198.  
  199.         return parseStringToTimestamp(datetime, "");
  200.     }
  201.  
  202.     public static Timestamp getCurrentTimestamp(String format) {
  203.  
  204.         String datetime = getCurrentDate(format);
  205.  
  206.         return parseStringToTimestamp(datetime, "");
  207.     }
  208.  
  209.     /**
  210.      * 日期转换为字符串
  211.      *
  212.      * @param date 日期
  213.      * @param format 格式
  214.      * @return 返回字符型日期
  215.      */
  216.     public static String parseDateToString(Date date, String format) {
  217.  
  218.         String result = "";
  219.         DateFormat formatter = null;
  220.         try {
  221.             if (date != null) {
  222.                 if (StringUtils.isBlank(format)) {
  223.                     formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  224.                 }
  225.                 else {
  226.                     formatter = new SimpleDateFormat(format);
  227.                 }
  228.                 result = formatter.format(date);
  229.             }
  230.         }
  231.         catch (Exception e) {
  232.         }
  233.  
  234.         return result;
  235.     }
  236.  
  237.     /**
  238.      * 时间1-时间2的毫秒
  239.      *
  240.      * @param t1
  241.      * @param t2
  242.      * @return
  243.      */
  244.     public static long between(Timestamp t1, Timestamp t2) {
  245.  
  246.         if ((t1 != null) && (t2 != null)) {
  247.             return t1.getTime() - t2.getTime();
  248.         }
  249.  
  250.         return 0;
  251.     }
  252.  
  253.     /**
  254.      * 两个日期date1-date2相减,相差的天数
  255.      *
  256.      * @param date1
  257.      *             日期
  258.      * @param date2
  259.      *             日期
  260.      * @return 返回相减后的日期
  261.      */
  262.     public static int betweenTwoDates(Date date1, Date date2) {
  263.  
  264.         return (int) ((getMillis(date1) - getMillis(date2)) / (24 * 3600 * 1000));
  265.     }
  266.  
  267.     /**  
  268.      * 将字符串转换为日期  
  269.      *    
  270.      * @param str  
  271.      * @return  
  272.      * @throws ParseException
  273.      */
  274.     public static Date parseStringToDate(String str, String format) {
  275.  
  276.         DateFormat formatter = null;
  277.         Date date = null;
  278.         if (StringUtils.isNotBlank(str)) {
  279.  
  280.             if (StringUtils.isBlank(format)) {
  281.                 formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  282.             }
  283.             else {
  284.                 formatter = new SimpleDateFormat(format);
  285.             }
  286.  
  287.             try {
  288.                 date = formatter.parse(str);
  289.             }
  290.             catch (ParseException e) {
  291.                 e.printStackTrace();
  292.             }
  293.         }
  294.  
  295.         return date;
  296.     }
  297.  
  298.     /**
  299.      * 返回2007到今年的年份
  300.      *
  301.      * @return
  302.      */
  303.     public static List<Integer> get2007ToThisYear() {
  304.  
  305.         // 当前时间
  306.         Calendar c = Calendar.getInstance();
  307.         c.setTime(new Date());
  308.  
  309.         // 返回的年份
  310.         List<Integer> the2007ToThisYearList = new ArrayList<Integer>();
  311.         // 当前年
  312.         int thisYear = c.get(Calendar.YEAR);
  313.  
  314.         for (int i = thisYear; i >= 2007; i--) {
  315.             the2007ToThisYearList.add(i);
  316.         }
  317.  
  318.         return the2007ToThisYearList;
  319.     }
  320.  
  321.     /**
  322.      *  获取当前月的前几个月份的时间
  323.      * @param months
  324.      * @return
  325.      */
  326.     public static Date getDateBeforeMonths(int months) {
  327.  
  328.         // 当前时间
  329.         Calendar c = Calendar.getInstance();
  330.  
  331.         c.add(Calendar.MONTH, -months);
  332.  
  333.         return c.getTime();
  334.     }
  335.  
  336.     /**
  337.      * 获取当前日期的前几天的时间
  338.      * @param days
  339.      * @return
  340.      */
  341.     public static Date getDateBeforeDays(int days) {
  342.  
  343.         // 当前时间
  344.         Calendar c = Calendar.getInstance();
  345.  
  346.         c.add(Calendar.DATE, -days);
  347.         return c.getTime();
  348.     }
  349.  
  350.     /**
  351.      * 返回1-12月份
  352.      *
  353.      * @return
  354.      */
  355.     public static List<String> getAllMonth() {
  356.  
  357.         List<String> theMonthList = new ArrayList<String>();
  358.  
  359.         for (int i = 1; i < 13; i++) {
  360.             if (i < 10) {
  361.                 theMonthList.add("0" + i);
  362.             }
  363.             else {
  364.                 theMonthList.add("" + i);
  365.             }
  366.         }
  367.  
  368.         return theMonthList;
  369.     }
  370.  
  371.     /**
  372.      * 返回日期中的年份
  373.      *
  374.      * @param date
  375.      *             日期
  376.      * @return 返回年份
  377.      */
  378.     public static int getYear(Date date) {
  379.  
  380.         Calendar c = Calendar.getInstance();
  381.         c.setTime(date);
  382.  
  383.         return c.get(Calendar.YEAR);
  384.     }
  385.  
  386.     /**
  387.      * 返回日期中的月份
  388.      *
  389.      * @param date
  390.      *             日期
  391.      * @return 返回月份
  392.      */
  393.     public static int getMonth(Date date) {
  394.  
  395.         Calendar c = Calendar.getInstance();
  396.         c.setTime(date);
  397.  
  398.         return c.get(Calendar.MONTH) + 1;
  399.     }
  400.  
  401.     /**
  402.      * 返回日期中的日
  403.      *
  404.      * @param date
  405.      *             日期
  406.      * @return 返回日
  407.      */
  408.     public static int getDay(Date date) {
  409.  
  410.         Calendar c = Calendar.getInstance();
  411.         c.setTime(date);
  412.  
  413.         return c.get(Calendar.DAY_OF_MONTH);
  414.     }
  415.  
  416.     /**
  417.      * 返回日期中的小时
  418.      *
  419.      * @param date
  420.      *             日期
  421.      * @return 返回小时
  422.      */
  423.     public static int getHour(Date date) {
  424.  
  425.         Calendar c = Calendar.getInstance();
  426.         c.setTime(date);
  427.  
  428.         return c.get(Calendar.HOUR_OF_DAY);
  429.     }
  430.  
  431.     /**
  432.      * 返回日期中的分钟
  433.      *
  434.      * @param date
  435.      *             日期
  436.      * @return 返回分钟
  437.      */
  438.     public static int getMinute(Date date) {
  439.  
  440.         Calendar c = Calendar.getInstance();
  441.         c.setTime(date);
  442.  
  443.         return c.get(Calendar.MINUTE);
  444.     }
  445.  
  446.     /**
  447.      * 返回日期中的秒钟
  448.      *
  449.      * @param date
  450.      *             日期
  451.      * @return 返回秒钟
  452.      */
  453.     public static int getSecond(Date date) {
  454.  
  455.         Calendar c = Calendar.getInstance();
  456.         c.setTime(date);
  457.  
  458.         return c.get(Calendar.SECOND);
  459.     }
  460.  
  461.     /**
  462.      * 返回日期代表的毫秒
  463.      *
  464.      * @param date
  465.      *             日期
  466.      * @return 返回毫秒
  467.      */
  468.     public static long getMillis(Date date) {
  469.  
  470.         Calendar c = Calendar.getInstance();
  471.         c.setTime(date);
  472.  
  473.         return c.getTimeInMillis();
  474.     }
  475.  
  476.     /**
  477.      * 返回当前日期代表的毫秒
  478.      *
  479.      * @return
  480.      */
  481.     public static long getCurrentTimeMillis() {
  482.  
  483.         return System.currentTimeMillis();
  484.     }
  485.  
  486.     public static Date addMonth(Date date, int month) {
  487.  
  488.         Calendar c = Calendar.getInstance();
  489.         c.setTime(date);
  490.         c.add(c.MONTH, month);
  491.  
  492.         return c.getTime();
  493.     }
  494.  
  495.     /**
  496.      * 日期加天数,可以向前加,向后加
  497.      *
  498.      * @param date
  499.      *             日期
  500.      * @param day
  501.      *             天数
  502.      * @return 返回相加后的日期
  503.      */
  504.     public static Date addDay(Date date, int day) {
  505.  
  506.         Calendar c = Calendar.getInstance();
  507.         c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
  508.  
  509.         return c.getTime();
  510.     }
  511.  
  512.     /**
  513.      * 日期加秒数,可以向前加,向后加
  514.      *
  515.      * @param date
  516.      *             日期
  517.      * @param second
  518.      *             秒数
  519.      * @return 返回相加后的日期
  520.      */
  521.     public static Date addSecond(Date date, long second) {
  522.  
  523.         Calendar c = Calendar.getInstance();
  524.         c.setTimeInMillis(getMillis(date) + second * 1000);
  525.  
  526.         return c.getTime();
  527.     }
  528.  
  529.     /**  
  530.      * 根据一个日期,返回是星期几  
  531.      *    
  532.      * @param sdate  
  533.      * @return  
  534.      */
  535.     public static String getWeekByDate(String sdate) {
  536.  
  537.         // 再转换为时间
  538.         Date date = parseStringToDate(sdate, "");
  539.         Calendar c = Calendar.getInstance();
  540.         c.setTime(date);
  541.         int day = c.get(Calendar.DAY_OF_WEEK);
  542.         // day中存的就是星期几了,其范围 1~7
  543.         // 1=星期日 7=星期六,其他类推
  544.         return day + "";
  545.     }
  546.      
  547.      
  548.     /**  
  549.      * 根据一个日期,返回是星期几
  550.      * 1=星期日 7=星期六,其他类推  
  551.      *    
  552.      * @param sdate  
  553.      * @return  
  554.      */
  555.     public static int getWeekByDate(Date date) {
  556.  
  557.         // 再转换为时间
  558.         Calendar c = Calendar.getInstance();
  559.         c.setTime(date);
  560.         // day中存的就是星期几了,其范围 1~7
  561.         // 1=星期日 7=星期六,其他类推
  562.         return c.get(Calendar.DAY_OF_WEEK);
  563.     }
  564.      
  565.  
  566.     /**
  567.      * 日期为一年中的第几周
  568.      * @return
  569.      */
  570.     public static String getWeekNum(Date date) {
  571.  
  572.         Calendar c = Calendar.getInstance(Locale.CHINA);
  573.         c.setTime(date);
  574.         String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
  575.  
  576.         return week;
  577.  
  578.     }
  579.  
  580.     public static java.sql.Date parseUtilDateToSqlDate(Date date) {
  581.  
  582.         if (date != null) {
  583.             return new java.sql.Date(date.getTime());
  584.         }
  585.         else {
  586.             return null;
  587.         }
  588.     }
  589.  
  590.     public static java.sql.Date parseStringToSqlDate(String dateStr, String format) {
  591.  
  592.         Date date = null;
  593.         if (StringUtils.isBlank(format)) {
  594.             date = parseStringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
  595.         }
  596.         else {
  597.             date = parseStringToDate(dateStr, format);
  598.         }
  599.  
  600.         return parseUtilDateToSqlDate(date);
  601.     }
  602.  
  603.     public static Timestamp parseUtilDateToTimestamp(Date date, String format) {
  604.  
  605.         return parseStringToTimestamp(parseDateToString(date, format), format);
  606.     }
  607.  
  608.     public static Date parseTimestampToUtilDate(Timestamp date, String format) {
  609.  
  610.         return parseStringToDate(parseDateToString(date, format), format);
  611.     }
  612.  
  613.     public static Timestamp parseStringToTimestamp(String dateStr, String format) {
  614.  
  615.         if (StringUtils.isBlank(dateStr)) {
  616.             return null;
  617.         }
  618.  
  619.         Date date = null;
  620.         if (StringUtils.isBlank(format)) {
  621.             date = parseStringToDate(dateStr, "yyyy-MM-dd HH:mm:ss");
  622.         }
  623.         else {
  624.             date = parseStringToDate(dateStr, format);
  625.         }
  626.  
  627.         if (date != null) {
  628.             long t = date.getTime();
  629.             return new Timestamp(t);
  630.         }
  631.         else {
  632.             return null;
  633.         }
  634.     }
  635.  
  636.     /**
  637.      * 获取时间2099-12-31,23:59:59
  638.      *
  639.      * @return
  640.      */
  641.     public static Timestamp getMaxTimestamp() {
  642.  
  643.         return DateUtil.parseStringToTimestamp("2099-12-31 23:59:59", DateUtil.FORMAT7);
  644.     }
  645.  
  646.     /**
  647.      * 返回日期中的年月日,格式化成yyyyMMdd
  648.      * @param date
  649.      * @return
  650.      */
  651.     public static String getYearMonthDay(Date date) {
  652.  
  653.         SimpleDateFormat formatter = new SimpleDateFormat(FORMAT12);
  654.  
  655.         String currentTime = formatter.format(date);
  656.  
  657.         return currentTime;
  658.     }
  659.  
  660.     /**
  661.      * 取得指定月份的第一天
  662.      *
  663.      * @param strdate String
  664.      * @return String
  665.      */
  666.     public static String getMonthBegin(String strdate) {
  667.  
  668.         return parseDateToString(parseStringToDate(strdate, "yyyy-MM"), "");
  669.     }
  670.  
  671.     /**
  672.      * 取得指定月份的最后一天
  673.      *
  674.      * @param strdate String
  675.      * @return String
  676.      */
  677.     public static String getMonthEnd(String strdate) {
  678.  
  679.         Date date = parseStringToDate(getMonthBegin(strdate), "");
  680.         Calendar calendar = Calendar.getInstance();
  681.         calendar.setTime(date);
  682.         calendar.add(Calendar.MONTH, 1);
  683.         calendar.add(Calendar.DAY_OF_YEAR, -1);
  684.  
  685.         return parseDateToString(calendar.getTime(), "");
  686.     }
  687.  
  688.     public static String getPreviousMonthBegin() {
  689.  
  690.         Calendar cal = Calendar.getInstance();
  691.         cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
  692.         Date date = new Date(cal.getTimeInMillis());
  693.  
  694.         return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT1);
  695.     }
  696.  
  697.     public static String getPreviousMonthEnd() {
  698.  
  699.         Date date = parseStringToDate(getPreviousMonthBegin(), FORMAT1);
  700.         Calendar calendar = Calendar.getInstance();
  701.         calendar.setTime(date);
  702.         calendar.add(Calendar.MONTH, 1);
  703.         calendar.add(Calendar.DAY_OF_YEAR, -1);
  704.  
  705.         return parseDateToString(calendar.getTime(), FORMAT1);
  706.     }
  707.  
  708.     /**
  709.      * 上个月
  710.      *
  711.      * @return
  712.      */
  713.     public static String getPreviousMonth() {
  714.  
  715.         Calendar cal = Calendar.getInstance();
  716.         cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
  717.         Date date = new Date(cal.getTimeInMillis());
  718.  
  719.         return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT14);
  720.     }
  721.  
  722.     /**
  723.      * 上两个月
  724.      *
  725.      * @return
  726.      */
  727.     public static String getPreviousTwoMonth() {
  728.  
  729.         Calendar cal = Calendar.getInstance();
  730.         cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 2);
  731.         Date date = new Date(cal.getTimeInMillis());
  732.  
  733.         return parseDateToString(parseStringToDate(parseDateToString(date, FORMAT1), "yyyy-MM"), FORMAT14);
  734.     }
  735.  
  736.     /**
  737.      * 判断两个日期是否在同一周
  738.      * @param date1
  739.      * @param date2
  740.      * @return
  741.      */
  742.     public static boolean isSameWeekDates(Date date1, Date date2) {
  743.  
  744.         Calendar cal1 = Calendar.getInstance();
  745.         Calendar cal2 = Calendar.getInstance();
  746.         cal1.setTime(date1);
  747.         cal2.setTime(date2);
  748.         int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
  749.         if (0 == subYear) {
  750.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
  751.                 return true;
  752.             }
  753.         }
  754.         else if ((1 == subYear) && (11 == cal2.get(Calendar.MONTH))) {
  755.             // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
  756.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
  757.                 return true;
  758.             }
  759.         }
  760.         else if ((-1 == subYear) && (11 == cal1.get(Calendar.MONTH))) {
  761.             if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR)) {
  762.                 return true;
  763.             }
  764.         }
  765.         return false;
  766.     }
  767.  
  768.     public static void main(String[] args) {
  769.  
  770.         System.out.println(getPreviousMonthBegin());
  771.         System.out.println(getPreviousMonthEnd());
  772.         System.out.println(getYearMonthDay(new Date()));
  773.         System.out.println(getYearMonthDay(parseStringToDate("2009-11-2 12:1:21", FORMAT7)));
  774.         System.out.println("current time: " + getCurrentDateTime());
  775.         System.out.println("addsecond: " + addSecond(getCurrentDateTime(), -1L));
  776.         Date date = new Date();
  777.         System.out.println("current date: " + date.toString());
  778.  
  779.         System.out.println("test parseDateToString: " + parseDateToString(date, ""));
  780.         System.out.println("test parseStringToDate: " + parseStringToDate("1990-01-1 00:00:00", ""));
  781.  
  782.         System.out.println("test getYear: " + getYear(date));
  783.         System.out.println("test getMonth: " + getMonth(date));
  784.         System.out.println("test getDay: " + getDay(date));
  785.         System.out.println("test getHour: " + getHour(date));
  786.         System.out.println("test getMinute: " + getMinute(date));
  787.         System.out.println("test getSecond: " + getSecond(date));
  788.         System.out.println("test getMillis: " + getMillis(date));
  789.         System.out.println("test addDate: " + addDay(date, 2));
  790.         System.out.println("test betweenTwoDays: " + betweenTwoDates(date, addDay(date, 2)));
  791.         System.out.println("test getWeekNum: " + getWeekNum(addDay(date, -2)));
  792.         System.out.println("test getWeekByDate: " + getWeekByDate(parseDateToString(date, "")));
  793.         System.out.println("test getMonthBegin: " + getMonthBegin(parseDateToString(date, "")));
  794.         System.out.println("test getMonthEnd: " + getMonthEnd(parseDateToString(date, "")));
  795.         System.out.println("test isSameWeekDates: " + isSameWeekDates(date, addDay(date, -2)));
  796.  
  797.         System.out.println(getPreviousTwoMonth());
  798.         System.out.println(getPreviousMonth());
  799.         List<Integer> yearList = get2007ToThisYear();
  800.         List<String> monthList = getAllMonth();
  801.     }
  802.  
  803.     public static String addOneDay(String stopTime) {
  804.         String finishTime = stopTime;
  805.         Date finishDate = null;
  806.         if(stopTime != null && !"".equals(stopTime))
  807.         {
  808.             if(stopTime.length() < 19)
  809.             {
  810.                 finishTime = stopTime.substring(0, 10) + " 00:00:00";
  811.             }
  812.             finishDate = DateUtil.parseStringToDate(finishTime, DateUtil.FORMAT7);
  813.             finishDate = DateUtil.addDay(finishDate, 1);
  814.             return DateUtil.parseDateToString(finishDate, DateUtil.FORMAT7);
  815.         }else{
  816.             return null;
  817.         }
  818.     }
  819.      
  820.      
  821.     /**
  822.      * 给date加1天
  823.      * @param date
  824.      * @return
  825.      */
  826.     public static Date addOneDay(Date date) {
  827.  
  828.         Calendar calendar = Calendar.getInstance();
  829.         calendar.setTime(date);
  830.         calendar.add(Calendar.DATE, 1);
  831.         return calendar.getTime();
  832.     }
  833.  
  834. }
  835. //java/6806

回复 "Java操作日期时间范例代码大全"

这儿你可以回复上面这条便签

captcha