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

本文介绍了一个Android应用程序中用于获取当前时间的各种开始和结束时间的方法,包括天、周、月、季度、半年度及年度的时间范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值