java时间处理工具类总结四[week]

package date.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

public class WeekUtil {
public String getWeek() {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五",
"星期六" };
SimpleDateFormat sdfInput = new SimpleDateFormat(
"yyyy-MM-dd HH:mm");
//SimpleDateFormat 是一个以与语言环境相关的方式来格式化和分析日期的具体类。它允许进行格式化(日期 -> 文本)、分析(文本 -> 日期)和规范化。
Calendar calendar = Calendar.getInstance();
Date date = new Date();
try {
date = sdfInput.parse(sdfInput.format(date));
} catch (ParseException ex) {
}
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
//get 和 set 的字段数字,指示一个星期中的某天。
System.out.println(
"dayOfWeek:"+dayOfWeek);
return dayNames[dayOfWeek - 1];
}

public String getWeek(Date date) {
SimpleDateFormat df = new SimpleDateFormat(
"yyyy");
String year = df.format(date);
//将一个 Date 格式化为日期/时间字符串。
System.out.println(
"year:"+year);
df = new SimpleDateFormat(
"MM");
String month = df.format(date);
System.out.println(
"month:"+month);
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.MONDAY);
// 设置一星期的第一天是哪一天;例如,在美国,这一天是 SUNDAY,而在法国,这一天是 MONDAY。
c.setTime(date);
//使用给定的 Date 设置此 Calendar 的时间。
int week = c.get(Calendar.WEEK_OF_YEAR);
//get 和 set 的字段数字,指示当前年中的星期数。
System.out.println(
"week:"+week);

if (week == 1 && month.equals(
"12")) {
year = new Integer(year) + 1 +
"";
}
if (week > 9) {
return year + week;
} else {
return year +
"0" + week;
}
}

public Date addDate(Date date, int field, int amount) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
gc.add(field, amount);
return gc.getTime();
}

public List applyCard()
    {
        List list = new ArrayList();
        List tr = null;
        List card = new ArrayList();
        card.add(new AwardCardModel(
"951110301716"));
        card.add(new AwardCardModel(
"951110301717"));
        card.add(new AwardCardModel(
"951110301718"));
        card.add(new AwardCardModel(
"951110301719"));
        card.add(new AwardCardModel(
"951110301720"));
        card.add(new AwardCardModel(
"9511103017121"));
        card.add(new AwardCardModel(
"9511103017122"));
        card.add(new AwardCardModel(
"9511103017123"));
        card.add(new AwardCardModel(
"9511103017124"));
        card.add(new AwardCardModel(
"9511103017125"));
        card.add(new AwardCardModel(
"9511103017126"));
        card.add(new AwardCardModel(
"9511103017196"));
        card.add(new AwardCardModel(
"95111030172277"));
        card.add(new AwardCardModel(
"95111030171228"));
        card.add(new AwardCardModel(
"95111030171229"));
        card.add(new AwardCardModel(
"95111030171231"));
        
        Iterator i = card.iterator();
        
        AwardCardModel model = null;
        int j = 1;
        int size = 1;
        while(i.hasNext())
        {
            model = (AwardCardModel)i.next();
            if (j == 1)
            {
                tr = new ArrayList();
            }
            tr.add(model);
            if (j==5 || size == card.size())
            {
                list.add(tr);
            }
            if (j==5)
            {
                j = 1;
            }
            else
            {
                j++;
            }
            size ++ ;
        }
        return list;
    }
}

lunzi   2008-01-12 23:45:07 评论:0   阅读:1753   引用:0

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog