取得当前日期相对应的月初,月末,季初,季末,年初,年末

转自:http://www.atimin.com/read.php/205.htm       

Atimin's Blog!

 

package vivi.test;

import java.util.Calendar;
/**
  *
  * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串
  *      1、得到当前日期         today()
  *      2、得到当前月份月初      thisMonth()
  *      3、得到当前月份月底      thisMonthEnd()
  *      4、得到当前季度季初      thisSeason()
  *      5、得到当前季度季末      thisSeasonEnd()
  *      6、得到当前年份年初      thisYear()
  *      7、得到当前年份年底      thisYearEnd()
  *      8、判断输入年份是否为闰年 leapYear  
  * 注意事项:  日期格式为:xxxx-yy-zz (eg: 2007-12-05)
  * 实例:
  * @author pure
  */
public class Test {
  private int x;                  // 日期属性:年
  private int y;                  // 日期属性:月
  private int z;                  // 日期属性:日
  private Calendar localTime;     // 当前日期
  
  public Test() {
      localTime = Calendar.getInstance();
  }
  
  public static void main(String[] args){
    Test test = new Test();
    test.today();
  }
  /**
   * 功能:得到当前日期 格式为:xxxx-yy-zz (eg: 2007-12-05)
   * @return String
   * @author pure
   */
  public String today() {
    String strY = null;
    String strZ = null;
    x = localTime.get(Calendar.YEAR);
    y = localTime.get(Calendar.MONTH) + 1;
    z = localTime.get(Calendar.DATE);
    strY = y >= 10 ? String.valueOf(y) : ("0" + y);
    strZ = z >= 10 ? String.valueOf(z) : ("0" + z);
    return x + "-" + strY + "-" + strZ;
  }
  /**
    * 功能:得到当前月份月初 格式为:xxxx-yy-zz (eg: 2007-12-01)
    * @return String
    * @author pure
    */
  public String thisMonth() {
    String strY = null;
    x = localTime.get(Calendar.YEAR);
    y = localTime.get(Calendar.MONTH) + 1;
    strY = y >= 10 ? String.valueOf(y) : ("0" + y);return x + "-" + strY + "-01";
  }
  /**
    * 功能:得到当前月份月底 格式为:xxxx-yy-zz (eg: 2007-12-31)
    * @return String
    * @author pure
    **/
  public String thisMonthEnd() {
    String strY = null;
    String strZ = null;
    boolean leap = false;
    x = localTime.get(Calendar.YEAR);
    y = localTime.get(Calendar.MONTH) + 1;
    if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
      strZ = "31";
    }
    if (y == 4 || y == 6 || y == 9 || y == 11) {
      strZ = "30";
    }
    if (y == 2) {
      leap = leapYear(x);
      if (leap) {
        strZ = "29";
      }else {
        strZ = "28";
      }
    }
    strY = y >= 10 ? String.valueOf(y) : ("0" + y);
    return x + "-" + strY + "-" + strZ;
  }

  /**
    * 功能:得到当前季度季初 格式为:xxxx-yy-zz (eg: 2007-10-01)<br/>
      * @return String
      * @author pure
      */
  public String thisSeason() {
    String dateString = "";
    x = localTime.get(Calendar.YEAR);
    y = localTime.get(Calendar.MONTH) + 1;
    if (y >= 1 && y <= 3) {
      dateString = x + "-" + "01" + "-" + "01";
    }
    if (y >= 4 && y <= 6) {
      dateString = x + "-" + "04" + "-" + "01";
    }
    if (y >= 7 && y <= 9) {
      dateString = x + "-" + "07" + "-" + "01";
    }
    if (y >= 10 && y <= 12) {
      dateString = x + "-" + "10" + "-" + "01";
    }
    return dateString;
    }

  /**
    * 功能:得到当前季度季末 格式为:xxxx-yy-zz (eg: 2007-12-31)<br/>
    * @return String
    * @author pure
    */
  public String thisSeasonEnd() {
    String dateString = "";
    x = localTime.get(Calendar.YEAR);
    y = localTime.get(Calendar.MONTH) + 1;
    if (y >= 1 && y <= 3) {
      dateString = x + "-" + "03" + "-" + "31";
    }
    if (y >= 4 && y <= 6) {
      dateString = x + "-" + "06" + "-" + "30";
    }
    if (y >= 7 && y <= 9) {
      dateString = x + "-" + "09" + "-" + "30";
    }
    if (y >= 10 && y <= 12) {
      dateString = x + "-" + "12" + "-" + "31";
    }
    return dateString;
  }

  /**
    * 功能:得到当前年份年初 格式为:xxxx-yy-zz (eg: 2007-01-01)<br/>
    * @return String
    * @author pure
    */
  public String thisYear() {
    x = localTime.get(Calendar.YEAR);
    return x + "-01" + "-01";
  }
  
  /**
    * 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: 2007-12-31)<br/>
    * @return String
    * @author pure
    */
  public String thisYearEnd() {
    x = localTime.get(Calendar.YEAR);
    return x + "-12" + "-31";
  }
  
  /**
    * 功能:判断输入年份是否为闰年<br/>
    * @param year
    * @return 是:true  否:false
    * @author pure
    */
  public boolean leapYear(int year) {
    boolean leap;
    if (year % 4 == 0) {
      if (year % 100 == 0) {
        if (year % 400 == 0) leap = true;
          else leap = false;
        }
      else leap = true;
    }
    else leap = false;
    return leap;
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值