Java获取当前年、半年、季度、月、日、小时开始结束时间等

  1. import java.text.SimpleDateFormat;   
  2. import java.util.Calendar;   
  3. import java.util.Date;   
  4.   
  5. /**  
  6.  * 获取  当前年、半年、季度、月、日、小时 开始结束时间  
  7.  */  
  8.   
  9. private final SimpleDateFormat shortSdf;   
  10.     private final SimpleDateFormat longHourSdf;   
  11.     private final SimpleDateFormat longSdf;   
  12.        
  13.     public RemindDateUtils(){   
  14.         this.shortSdf = new SimpleDateFormat("yyyy-MM-dd");   
  15.         this.longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");   
  16.         this.longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  17.     }   
  18.        
  19.     /**  
  20.      * 获得本周的第一天,周一  
  21.      *   
  22.      * @return  
  23.      */  
  24.     public   Date getCurrentWeekDayStartTime() {   
  25.         Calendar c = Calendar.getInstance();   
  26.         try {   
  27.             int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;   
  28.             c.add(Calendar.DATE, -weekday);   
  29.             c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));   
  30.         } catch (Exception e) {   
  31.             e.printStackTrace();   
  32.         }   
  33.         return c.getTime();   
  34.     }   
  35.   
  36.     /**  
  37.      * 获得本周的最后一天,周日  
  38.      *   
  39.      * @return  
  40.      */  
  41.     public   Date getCurrentWeekDayEndTime() {   
  42.         Calendar c = Calendar.getInstance();   
  43.         try {   
  44.             int weekday = c.get(Calendar.DAY_OF_WEEK);   
  45.             c.add(Calendar.DATE, 8 - weekday);   
  46.             c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));   
  47.         } catch (Exception e) {   
  48.             e.printStackTrace();   
  49.         }   
  50.         return c.getTime();   
  51.     }   
  52.   
  53.     /**  
  54.      * 获得本天的开始时间,即2012-01-01 00:00:00  
  55.      *   
  56.      * @return  
  57.      */  
  58.     public   Date getCurrentDayStartTime() {   
  59.         Date now = new Date();   
  60.         try {   
  61.             now = shortSdf.parse(shortSdf.format(now));   
  62.         } catch (Exception e) {   
  63.             e.printStackTrace();   
  64.         }   
  65.         return now;   
  66.     }   
  67.   
  68.     /**  
  69.      * 获得本天的结束时间,即2012-01-01 23:59:59  
  70.      *   
  71.      * @return  
  72.      */  
  73.     public   Date getCurrentDayEndTime() {   
  74.         Date now = new Date();   
  75.         try {   
  76.             now = longSdf.parse(shortSdf.format(now) + " 23:59:59");   
  77.         } catch (Exception e) {   
  78.             e.printStackTrace();   
  79.         }   
  80.         return now;   
  81.     }   
  82.   
  83.     /**  
  84.      * 获得本小时的开始时间,即2012-01-01 23:59:59  
  85.      *   
  86.      * @return  
  87.      */  
  88.     public   Date getCurrentHourStartTime() {   
  89.         Date now = new Date();   
  90.         try {   
  91.             now = longHourSdf.parse(longHourSdf.format(now));   
  92.         } catch (Exception e) {   
  93.             e.printStackTrace();   
  94.         }   
  95.         return now;   
  96.     }   
  97.   
  98.     /**  
  99.      * 获得本小时的结束时间,即2012-01-01 23:59:59  
  100.      *   
  101.      * @return  
  102.      */  
  103.     public   Date getCurrentHourEndTime() {   
  104.         Date now = new Date();   
  105.         try {   
  106.             now = longSdf.parse(longHourSdf.format(now) + ":59:59");   
  107.         } catch (Exception e) {   
  108.             e.printStackTrace();   
  109.         }   
  110.         return now;   
  111.     }   
  112.   
  113.     /**  
  114.      * 获得本月的开始时间,即2012-01-01 00:00:00  
  115.      *   
  116.      * @return  
  117.      */  
  118.     public   Date getCurrentMonthStartTime() {   
  119.         Calendar c = Calendar.getInstance();   
  120.         Date now = null;   
  121.         try {   
  122.             c.set(Calendar.DATE, 1);   
  123.             now = shortSdf.parse(shortSdf.format(c.getTime()));   
  124.         } catch (Exception e) {   
  125.             e.printStackTrace();   
  126.         }   
  127.         return now;   
  128.     }   
  129.   
  130.     /**  
  131.      * 当前月的结束时间,即2012-01-31 23:59:59  
  132.      *   
  133.      * @return  
  134.      */  
  135.     public   Date getCurrentMonthEndTime() {   
  136.         Calendar c = Calendar.getInstance();   
  137.         Date now = null;   
  138.         try {   
  139.             c.set(Calendar.DATE, 1);   
  140.             c.add(Calendar.MONTH, 1);   
  141.             c.add(Calendar.DATE, -1);   
  142.             now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");   
  143.         } catch (Exception e) {   
  144.             e.printStackTrace();   
  145.         }   
  146.         return now;   
  147.     }   
  148.   
  149.     /**  
  150.      * 当前年的开始时间,即2012-01-01 00:00:00  
  151.      *   
  152.      * @return  
  153.      */  
  154.     public   Date getCurrentYearStartTime() {   
  155.         Calendar c = Calendar.getInstance();   
  156.         Date now = null;   
  157.         try {   
  158.             c.set(Calendar.MONTH, 0);   
  159.             c.set(Calendar.DATE, 1);   
  160.             now = shortSdf.parse(shortSdf.format(c.getTime()));   
  161.         } catch (Exception e) {   
  162.             e.printStackTrace();   
  163.         }   
  164.         return now;   
  165.     }   
  166.   
  167.     /**  
  168.      * 当前年的结束时间,即2012-12-31 23:59:59  
  169.      *   
  170.      * @return  
  171.      */  
  172.     public   Date getCurrentYearEndTime() {   
  173.         Calendar c = Calendar.getInstance();   
  174.         Date now = null;   
  175.         try {   
  176.             c.set(Calendar.MONTH, 11);   
  177.             c.set(Calendar.DATE, 31);   
  178.             now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");   
  179.         } catch (Exception e) {   
  180.             e.printStackTrace();   
  181.         }   
  182.         return now;   
  183.     }   
  184.   
  185.     /**  
  186.      * 当前季度的开始时间,即2012-01-1 00:00:00  
  187.      *   
  188.      * @return  
  189.      */  
  190.     public   Date getCurrentQuarterStartTime() {   
  191.         Calendar c = Calendar.getInstance();   
  192.         int currentMonth = c.get(Calendar.MONTH) + 1;   
  193.         Date now = null;   
  194.         try {   
  195.             if (currentMonth >= 1 && currentMonth <= 3)   
  196.                 c.set(Calendar.MONTH, 0);   
  197.             else if (currentMonth >= 4 && currentMonth <= 6)   
  198.                 c.set(Calendar.MONTH, 3);   
  199.             else if (currentMonth >= 7 && currentMonth <= 9)   
  200.                 c.set(Calendar.MONTH, 4);   
  201.             else if (currentMonth >= 10 && currentMonth <= 12)   
  202.                 c.set(Calendar.MONTH, 9);   
  203.             c.set(Calendar.DATE, 1);   
  204.             now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");   
  205.         } catch (Exception e) {   
  206.             e.printStackTrace();   
  207.         }   
  208.         return now;   
  209.     }   
  210.   
  211.     /**  
  212.      * 当前季度的结束时间,即2012-03-31 23:59:59  
  213.      *   
  214.      * @return  
  215.      */  
  216.     public   Date getCurrentQuarterEndTime() {   
  217.         Calendar c = Calendar.getInstance();   
  218.         int currentMonth = c.get(Calendar.MONTH) + 1;   
  219.         Date now = null;   
  220.         try {   
  221.             if (currentMonth >= 1 && currentMonth <= 3) {   
  222.                 c.set(Calendar.MONTH, 2);   
  223.                 c.set(Calendar.DATE, 31);   
  224.             } else if (currentMonth >= 4 && currentMonth <= 6) {   
  225.                 c.set(Calendar.MONTH, 5);   
  226.                 c.set(Calendar.DATE, 30);   
  227.             } else if (currentMonth >= 7 && currentMonth <= 9) {   
  228.                 c.set(Calendar.MONTH, 8);   
  229.                 c.set(Calendar.DATE, 30);   
  230.             } else if (currentMonth >= 10 && currentMonth <= 12) {   
  231.                 c.set(Calendar.MONTH, 11);   
  232.                 c.set(Calendar.DATE, 31);   
  233.             }   
  234.             now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");   
  235.         } catch (Exception e) {   
  236.             e.printStackTrace();   
  237.         }   
  238.         return now;   
  239.     }   
  240.   
  241.     /**  
  242.      * 获取前/后半年的开始时间  
  243.      * @return  
  244.      */  
  245.     public   Date getHalfYearStartTime(){   
  246.         Calendar c = Calendar.getInstance();   
  247.         int currentMonth = c.get(Calendar.MONTH) + 1;   
  248.         Date now = null;   
  249.         try {   
  250.             if (currentMonth >= 1 && currentMonth <= 6){   
  251.                 c.set(Calendar.MONTH, 0);   
  252.             }else if (currentMonth >= 7 && currentMonth <= 12){   
  253.                 c.set(Calendar.MONTH, 6);   
  254.             }   
  255.             c.set(Calendar.DATE, 1);   
  256.             now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");   
  257.         } catch (Exception e) {   
  258.             e.printStackTrace();   
  259.         }   
  260.         return now;   
  261.            
  262.     }   
  263.   
  264.     /**  
  265.      * 获取前/后半年的结束时间  
  266.      * @return  
  267.      */  
  268.     public   Date getHalfYearEndTime(){   
  269.         Calendar c = Calendar.getInstance();   
  270.         int currentMonth = c.get(Calendar.MONTH) + 1;   
  271.         Date now = null;   
  272.         try {   
  273.             if (currentMonth >= 1 && currentMonth <= 6){   
  274.                 c.set(Calendar.MONTH, 5);   
  275.                 c.set(Calendar.DATE, 30);   
  276.             }else if (currentMonth >= 7 && currentMonth <= 12){   
  277.                 c.set(Calendar.MONTH, 11);   
  278.                 c.set(Calendar.DATE, 31);   
  279.             }   
  280.             now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");   
  281.         } catch (Exception e) {   
  282.             e.printStackTrace();   
  283.         }   
  284.         return now;   
  285.     }   
  286.   
  287.     /**  
  288.      * 获取前一天日期 及星期  
  289.      */  
  290.     public class RemindDateUtils {   
  291.         private void initDate(){   
  292.             String[] weekDays = {"周日","周一","周二","周三","周四","周五","周六"};   
  293.              Calendar cal = Calendar.getInstance();   
  294.              cal.add(Calendar.Date,-1);   
  295.              int i = cal.get(Calendar.DAY_OF_WEEK)-1;   
  296.              if(i<0){   
  297.                 i=0;   
  298.             }   
  299.             String yesterday = new SimpleDateFormate("yyyy年MM月dd日").format(cal.getTime());   
  300.          String w = weekDays[i];    
  301.         }   
  302.   
  303.     }       
  304.   
  305. }  

PS:SimpleDateFormat是线程不安全的。

  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓