java 月初_Java用于取得当前日期相对应的月初,月末,季初,季末,年初,年末时间...

1 packagecom.zrar.date;2 importjava.util.Calendar;3 /**

4 *5 * 描述:此类用于取得当前日期相对应的月初,月末,季初,季末,年初,年末,返回值均为String字符串6 * 1、得到当前日期 today()7 * 2、得到当前月份月初 thisMonth()8 * 3、得到当前月份月底 thisMonthEnd()9 * 4、得到当前季度季初 thisSeason()10 * 5、得到当前季度季末 thisSeasonEnd()11 * 6、得到当前年份年初 thisYear()12 * 7、得到当前年份年底 thisYearEnd()13 * 8、判断输入年份是否为闰年 leapYear14 *15 * 注意事项: 日期格式为:xxxx-yy-zz (eg: 2007-12-05)16 *17 * 实例:18 *19 *@authorpure20 */

21 public classDateThis {22 private int x; //日期属性:年

23 private int y; //日期属性:月

24 private int z; //日期属性:日

25 private Calendar localTime; //当前日期

26 publicDateThis() {27 localTime =Calendar.getInstance();28 }29 /**

30 * 功能:得到当前日期 格式为:xxxx-yy-zz (eg: 2007-12-05)
31 *@returnString32 *@authorpure33 */

34 publicString today() {35 String strY = null;36 String strZ = null;37 x =localTime.get(Calendar.YEAR);38 y = localTime.get(Calendar.MONTH) + 1;39 z =localTime.get(Calendar.DATE);40 strY = y >= 10 ? String.valueOf(y) : ("0" +y);41 strZ = z >= 10 ? String.valueOf(z) : ("0" +z);42 return x + "-" + strY + "-" +strZ;43 }44 /**

45 * 功能:得到当前月份月初 格式为:xxxx-yy-zz (eg: 2007-12-01)
46 *@returnString47 *@authorpure48 */

49 publicString thisMonth() {50 String strY = null;51 x =localTime.get(Calendar.YEAR);52 y = localTime.get(Calendar.MONTH) + 1;53 strY = y >= 10 ? String.valueOf(y) : ("0" +y);54 return x + "-" + strY + "-01";55 }56 /**

57 * 功能:得到当前月份月底 格式为:xxxx-yy-zz (eg: 2007-12-31)
58 *@returnString59 *@authorpure60 */

61 publicString thisMonthEnd() {62 String strY = null;63 String strZ = null;64 boolean leap = false;65 x =localTime.get(Calendar.YEAR);66 y = localTime.get(Calendar.MONTH) + 1;67 if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {68 strZ = "31";69 }70 if (y == 4 || y == 6 || y == 9 || y == 11) {71 strZ = "30";72 }73 if (y == 2) {74 leap =leapYear(x);75 if(leap) {76 strZ = "29";77 }78 else{79 strZ = "28";80 }81 }82 strY = y >= 10 ? String.valueOf(y) : ("0" +y);83 return x + "-" + strY + "-" +strZ;84 }85 /**

86 * 功能:得到当前季度季初 格式为:xxxx-yy-zz (eg: 2007-10-01)
87 *@returnString88 *@authorpure89 */

90 publicString thisSeason() {91 String dateString = "";92 x =localTime.get(Calendar.YEAR);93 y = localTime.get(Calendar.MONTH) + 1;94 if (y >= 1 && y <= 3) {95 dateString = x + "-" + "01" + "-" + "01";96 }97 if (y >= 4 && y <= 6) {98 dateString = x + "-" + "04" + "-" + "01";99 }100 if (y >= 7 && y <= 9) {101 dateString = x + "-" + "07" + "-" + "01";102 }103 if (y >= 10 && y <= 12) {104 dateString = x + "-" + "10" + "-" + "01";105 }106 returndateString;107 }108 /**

109 * 功能:得到当前季度季末 格式为:xxxx-yy-zz (eg: 2007-12-31)
110 *@returnString111 *@authorpure112 */

113 publicString thisSeasonEnd() {114 String dateString = "";115 x =localTime.get(Calendar.YEAR);116 y = localTime.get(Calendar.MONTH) + 1;117 if (y >= 1 && y <= 3) {118 dateString = x + "-" + "03" + "-" + "31";119 }120 if (y >= 4 && y <= 6) {121 dateString = x + "-" + "06" + "-" + "30";122 }123 if (y >= 7 && y <= 9) {124 dateString = x + "-" + "09" + "-" + "30";125 }126 if (y >= 10 && y <= 12) {127 dateString = x + "-" + "12" + "-" + "31";128 }129 returndateString;130 }131 /**

132 * 功能:得到当前年份年初 格式为:xxxx-yy-zz (eg: 2007-01-01)
133 *@returnString134 *@authorpure135 */

136 publicString thisYear() {137 x =localTime.get(Calendar.YEAR);138 return x + "-01" + "-01";139 }140 /**

141 * 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: 2007-12-31)
142 *@returnString143 *@authorpure144 */

145 publicString thisYearEnd() {146 x =localTime.get(Calendar.YEAR);147 return x + "-12" + "-31";148 }149 /**

150 * 功能:判断输入年份是否为闰年
151 *152 *@paramyear153 *@return是:true 否:false154 *@authorpure155 */

156 public boolean leapYear(intyear) {157 booleanleap;158 if (year % 4 == 0) {159 if (year % 100 == 0) {160 if (year % 400 == 0) leap = true;161 else leap = false;162 }163 else leap = true;164 }165 else leap = false;166 returnleap;167 }168 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值