Java取得:月初,月末

Java代码
  1. package vivi.test;   
  2.   
  3. import java.util.Calendar;   
  4. /**  
  5.   *  
  6.   * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串  
  7.   *      1、得到当前日期         today()  
  8.   *      2、得到当前月份月初      thisMonth()  
  9.   *      3、得到当前月份月底      thisMonthEnd()  
  10.   *      4、得到当前季度季初      thisSeason()  
  11.   *      5、得到当前季度季末      thisSeasonEnd()  
  12.   *      6、得到当前年份年初      thisYear()  
  13.   *      7、得到当前年份年底      thisYearEnd()  
  14.   *      8、判断输入年份是否为闰年 leapYear    
  15.   * 注意事项:  日期格式为:xxxx-yy-zz (eg: 2007-12-05)  
  16.   * 实例:  
  17.   * @author pure  
  18.   */  
  19. public class Test {   
  20.     private int x;                  // 日期属性:年   
  21.     private int y;                  // 日期属性:月   
  22.     private int z;                  // 日期属性:日   
  23.     private Calendar localTime;     // 当前日期   
  24.        
  25.     public Test() {   
  26.         localTime = Calendar.getInstance();   
  27.     }   
  28.        
  29.     public static void main(String[] args){   
  30.         Test test = new Test();   
  31.         test.today();   
  32.     }   
  33.     /**  
  34.      * 功能:得到当前日期 格式为:xxxx-yy-zz (eg: 2007-12-05)  
  35.      * @return String  
  36.      * @author pure  
  37.      */  
  38.     public String today() {   
  39.         String strY = null;   
  40.         String strZ = null;   
  41.         x = localTime.get(Calendar.YEAR);   
  42.         y = localTime.get(Calendar.MONTH) + 1;   
  43.         z = localTime.get(Calendar.DATE);   
  44.         strY = y >= 10 ? String.valueOf(y) : ("0" + y);   
  45.         strZ = z >= 10 ? String.valueOf(z) : ("0" + z);   
  46.         return x + "-" + strY + "-" + strZ;   
  47.     }   
  48.     /**  
  49.       * 功能:得到当前月份月初 格式为:xxxx-yy-zz (eg: 2007-12-01)  
  50.       * @return String  
  51.       * @author pure  
  52.       */  
  53.     public String thisMonth() {   
  54.         String strY = null;   
  55.         x = localTime.get(Calendar.YEAR);   
  56.         y = localTime.get(Calendar.MONTH) + 1;   
  57.         strY = y >= 10 ? String.valueOf(y) : ("0" + y);return x + "-" + strY + "-01";   
  58.     }   
  59.     /**  
  60.       * 功能:得到当前月份月底 格式为:xxxx-yy-zz (eg: 2007-12-31)  
  61.       * @return String  
  62.       * @author pure  
  63.       **/  
  64.     public String thisMonthEnd() {   
  65.         String strY = null;   
  66.         String strZ = null;   
  67.         boolean leap = false;   
  68.         x = localTime.get(Calendar.YEAR);   
  69.         y = localTime.get(Calendar.MONTH) + 1;   
  70.         if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {   
  71.             strZ = "31";   
  72.         }   
  73.         if (y == 4 || y == 6 || y == 9 || y == 11) {   
  74.             strZ = "30";   
  75.         }   
  76.         if (y == 2) {   
  77.             leap = leapYear(x);   
  78.             if (leap) {   
  79.                 strZ = "29";   
  80.             }else {   
  81.                 strZ = "28";   
  82.             }   
  83.         }   
  84.         strY = y >= 10 ? String.valueOf(y) : ("0" + y);   
  85.         return x + "-" + strY + "-" + strZ;   
  86.     }   
  87.   
  88.     /**  
  89.       * 功能:得到当前季度季初 格式为:xxxx-yy-zz (eg: 2007-10-01)<br>  
  90.       * @return String  
  91.       * @author pure  
  92.       */  
  93.     public String thisSeason() {   
  94.         String dateString = "";   
  95.         x = localTime.get(Calendar.YEAR);   
  96.         y = localTime.get(Calendar.MONTH) + 1;   
  97.         if (y >= 1 && y <= 3) {   
  98.             dateString = x + "-" + "01" + "-" + "01";   
  99.         }   
  100.         if (y >= 4 && y <= 6) {   
  101.             dateString = x + "-" + "04" + "-" + "01";   
  102.         }   
  103.         if (y >= 7 && y <= 9) {   
  104.             dateString = x + "-" + "07" + "-" + "01";   
  105.         }   
  106.         if (y >= 10 && y <= 12) {   
  107.             dateString = x + "-" + "10" + "-" + "01";   
  108.         }   
  109.         return dateString;   
  110.         }   
  111.   
  112.     /**  
  113.       * 功能:得到当前季度季末 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>  
  114.       * @return String  
  115.       * @author pure  
  116.       */  
  117.     public String thisSeasonEnd() {   
  118.         String dateString = "";   
  119.         x = localTime.get(Calendar.YEAR);   
  120.         y = localTime.get(Calendar.MONTH) + 1;   
  121.         if (y >= 1 && y <= 3) {   
  122.             dateString = x + "-" + "03" + "-" + "31";   
  123.         }   
  124.         if (y >= 4 && y <= 6) {   
  125.             dateString = x + "-" + "06" + "-" + "30";   
  126.         }   
  127.         if (y >= 7 && y <= 9) {   
  128.             dateString = x + "-" + "09" + "-" + "30";   
  129.         }   
  130.         if (y >= 10 && y <= 12) {   
  131.             dateString = x + "-" + "12" + "-" + "31";   
  132.         }   
  133.         return dateString;   
  134.     }   
  135.   
  136.     /**  
  137.       * 功能:得到当前年份年初 格式为:xxxx-yy-zz (eg: 2007-01-01)<br>  
  138.       * @return String  
  139.       * @author pure  
  140.       */  
  141.     public String thisYear() {   
  142.         x = localTime.get(Calendar.YEAR);   
  143.         return x + "-01" + "-01";   
  144.     }   
  145.        
  146.     /**  
  147.       * 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: 2007-12-31)<br>  
  148.       * @return String  
  149.       * @author pure  
  150.       */  
  151.     public String thisYearEnd() {   
  152.         x = localTime.get(Calendar.YEAR);   
  153.         return x + "-12" + "-31";   
  154.     }   
  155.        
  156.     /**  
  157.       * 功能:判断输入年份是否为闰年<br>  
  158.       * @param year  
  159.       * @return 是:true  否:false  
  160.       * @author pure  
  161.       */  
  162.     public boolean leapYear(int year) {   
  163.         boolean leap;   
  164.         if (year % 4 == 0) {   
  165.             if (year % 100 == 0) {   
  166.                 if (year % 400 == 0) leap = true;   
  167.                     else leap = false;   
  168.                 }   
  169.             else leap = true;   
  170.         }   
  171.         else leap = false;   
  172.         return leap;   
  173.     }   
  174. }  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值