记得当初做了个直播回看的小项目用到过,根据当天获取数据进行一个星期内的运算(二月份没分析瑞年与否),实现效果如下:
MVC模式代码,不要见笑;
private int getYear;
private int getMonth;
private int getDay;
private int getWay;
private int mgetWay;
private int mYear;
private int mMonth;
private int mDay;
private String mWay;
private int Day = 0;
boolean isDayAdd = false;
public void StringData() {
final Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
getYear =Integer.parseInt(String.valueOf(c.get(Calendar.YEAR))); //获取当前年份
getMonth =Integer.parseInt(String.valueOf(c.get(Calendar.MONTH) + 1));//获取当前月份
getDay =Integer.parseInt(String.valueOf(c.get(Calendar.DAY_OF_MONTH)));//获取当前月份的日期号码
getWay =Integer.parseInt(String.valueOf(c.get(Calendar.DAY_OF_WEEK)));
mgetWay = getWay;
for (int i = 0; i < 7; i++) {
mMonth = getMonth;
mYear = getYear;
mDay = getDay - i;
if (isDayAdd)
Day++;
if (mDay < 1) {
isDayAdd = true;
mMonth = mMonth - 1;
if (mMonth % 2 == 0 & mMonth != 0) {
if (mMonth >= 8)
mDay = 31 - Day;
else
mDay = 30 - Day;
if (mMonth == 2)
mDay = 28 - Day;
} else if (mMonth == 0) {
mDay = 31 - Day;// 十二月时
} else {
if (mMonth < 8)
mDay = 31 - Day;
else
mDay = 30 - Day;
}
if (mMonth < 1) {
mMonth = 12;// 当月份小于1时减一年等于12月
mYear = mYear - 1;
}
}
if (mgetWay == 0)
mgetWay = 7;
mgetWay = mgetWay - 1;
if (0 == mgetWay) {
mWay = "天";
} else if (1 == mgetWay) {
mWay = "一";
} else if (2 == mgetWay) {
mWay = "二";
} else if (3 == mgetWay) {
mWay = "三";
} else if (4 == mgetWay) {
mWay = "四";
} else if (5 == mgetWay) {
mWay = "五";
} else if (6 == mgetWay) {
mWay = "六";
}
tvButtomNvaData = mYear + "年" + mMonth + "月" + mDay +"日";
tvButtomNvaWeek = "星期" + mWay;
// tvDataInfoBean = new TVDataInfoBean();
// tvDataInfoBean.setTvData(tvButtomNvaData);
// tvDataInfoBean.setTvWeek(tvButtomNvaWeek);
if (i==0) {
Log.i("tag", "tvButtomNvaData=====" + tvButtomNvaData
+ "\\\ttvButtomNvaWeek====" +tvButtomNvaWeek+"\\\tnowDay==="+"当前时间");
}else {
Log.i("tag", "tvButtomNvaData=====" + tvButtomNvaData
+ "\\\ttvButtomNvaWeek====" + tvButtomNvaWeek);
}
}
}