package com.zrar.date;2 import java.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: -12-05)16 *17 * 实例:18 *19 * @author pure20 */21 public class DateThis {22private int x; // 日期属性:年23private int y; // 日期属性:月24private int z; // 日期属性:日25private Calendar localTime;// 当前日期26public DateThis() {27 localTime = Calendar.getInstance();28}29/**30* 功能:得到当前日期 格式为:xxxx-yy-zz (eg: -12-05)<br> 31* @return String32* @author pure33*/34public String 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: -12-01)<br> 46* @return String47* @author pure48*/49public String 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: -12-31)<br> 58* @return String59* @author pure60*/61public String 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: -10-01)<br> 87* @return String88* @author pure89*/90public String 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 return dateString;107}108/**109* 功能:得到当前季度季末 格式为:xxxx-yy-zz (eg: -12-31)<br> 110* @return String111* @author pure112*/113public String 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 return dateString;130}131/**132* 功能:得到当前年份年初 格式为:xxxx-yy-zz (eg: -01-01)<br> 133* @return String134* @author pure135*/136public String thisYear() {137 x = localTime.get(Calendar.YEAR);138 return x + "-01" + "-01";139}140/**141* 功能:得到当前年份年底 格式为:xxxx-yy-zz (eg: -12-31)<br> 142* @return String143* @author pure144*/145public String thisYearEnd() {146 x = localTime.get(Calendar.YEAR);147 return x + "-12" + "-31";148}149/**150* 功能:判断输入年份是否为闰年<br>151* 152* @param year153* @return 是:true 否:false154* @author pure155*/156public boolean leapYear(int year) {157 boolean leap;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 return leap;167}168 }
1200字范文,内容丰富有趣,写作的好帮手!