获取当前时间的天、周、月、季度、半年度、年度开始和结束时间

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