100分求在jsp中两个日期段之间的周次 比如20090101至20090110分别为20090101-20090103 第一周 20090104-20090110 第二周 谢谢了!~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ls说的对,用Calendar可以取到年、月、日、星期等,相当方便 Calendar找到中间的星期六吗?应该很方便的。 下面这段代码是个例子。取1995年12月5日所在的那个星期的星期天(每周开始的第一天,当然如果你要改成周一是每周的开始的话可以把代码作适当修改,比如把week-1改成(week+6)%7)的年月日[注意!calendar的月份这一项是从0-11,而不是我们日常所使用的1-12,一定要注意]能够查出某个日期所在的星期的第一天,只需要做个循环,就能够把区间中所有的星期的第一天都找出来。后面lz就应该会了吧? GregorianCalendar calendar = new GregorianCalendar(1995, 11, 5, 3, 2, 1); int week = calendar.get(Calendar.DAY_OF_WEEK); calendar.setTimeInMillis(calendar.getTimeInMillis() - 24l * 60 * 60 * 1000 * (week-1));//这里重新设置calendar,用的是毫秒数来设置,目的是寻找本周的第一天 System.out.println(""+calendar.get(calendar.YEAR)+calendar.get(calendar.MONTH+1)+calendar.get(calendar.DATE)); 如果你要在纯Jsp中进行计算,并且只在前台计算,不使用Calendar,我也有一个公式可以使用,具体方法只能你自己去写了。W=([Y/4]-[Y/100]+[Y/400]+S)%7各项参数的含义是:W:所求的星期(星期一是1,星期二是2,等等,星期天是0)Y:年份;S:从该年的1月1号开始到所需要计算的日期这个区间中的天数(不包括当天。这个需要另外写一个简单的逻辑去计算)方括号表示取整(不是四舍五入)比如要计算2009年1月4号,则代入:W=([2009/4]-[2009/100]+[2009/400]+3)%7=(502-20+5=3)%7=0所以2009年1月4号是星期天。祝你好运。 public class Test { public static void main(String[] args) throws Exception { printCalendar("20090122", "20090224"); } private static void printCalendar(String begin, String end) throws Exception { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); Calendar c1 = Calendar.getInstance(); c1.setTime(simpleDateFormat.parse(begin)); Calendar c2 = Calendar.getInstance(); c2.setTime(simpleDateFormat.parse(end)); int count = 0; int week = 0; while (c2.after(c1)) { week = c1.get(Calendar.DAY_OF_WEEK); System.out.print (simpleDateFormat.format(c1.getTime()) + "-"); c1.add(Calendar.DATE, 8 - week); System.out.print (simpleDateFormat.format(c2.after(c1) ? c1.getTime() : c2.getTime())); System.out.println (" 第"+(++count)+"周"); } }}我个人认为利用Calendar类比在jsp页面中调用javascript方法方便LZ可以把算出的值放到相应作用域中,在jsp页面中可用EL或其他标签都可得到 还好我这里有一个现成的,晕了,前段时间刚开发的,你看看能不能用吧,不过我想计算周次肯定是这样的,经测算,没有问题.我用的就是这个,因为是做统计必须用,//为了更好的扩展,所以这里加上粒度,以免以后需要月,日,时等格式. //第一个是粒度,第二个是求出的时间模型,第三个是时间,取周的时候用到.最后一个用来加增加周数 @SuppressWarnings("static-access") public String formatTime(String granularity,String time,Date stDate,int add){ String reTime = "";//返回时间 //int week = Integer.parseInt(time.substring(5,7));//第几周 try{ SimpleDateFormat config = new SimpleDateFormat("MM月dd日"); //SimpleDateFormat configDate = new SimpleDateFormat("yyyy-MM-dd"); Calendar ca1 = Calendar.getInstance(Locale.CHINA); Calendar ca2 = Calendar.getInstance(Locale.CHINA); ca1.setTime(stDate);//开始 ca2.setTime(stDate);//setTimeInMillis(stDate.getTime()+7*1000*3600*24);//结束 ca1.setFirstDayOfWeek(Calendar.MONDAY); ca2.setFirstDayOfWeek(Calendar.MONDAY); ca1.set(ca1.DAY_OF_WEEK, Calendar.MONDAY); ca2.set(ca2.DAY_OF_WEEK, Calendar.SUNDAY); ca1.add(ca1.WEEK_OF_YEAR, add); ca2.add(ca2.WEEK_OF_YEAR, add); /*System.out.println("开始时间"+config.format(ca1.getTime())); System.out.println("结束时间"+config.format(ca2.getTime())); System.out.println(time);*/ String forMonth = config.format(ca1.getTime());//开始月时间 String aftMonth = config.format(ca2.getTime());//结束月时间 int year = 0; if(forMonth.substring(0,2).equals("12")&&aftMonth.substring(0,2).equals("01")){ year = Integer.parseInt(time.substring(0,4))+1; }else{ year = Integer.parseInt(time.substring(0,4)); } String weekOfYear = "";//第几周 if(time.length()==6){ weekOfYear = "0"+time.substring(5,6); }else{ weekOfYear = time.substring(5,7); } if(granularity.equals("2")){//格式为 XXXX年第XX周(XX月XX日--XX月XX日) reTime = year+"月第"+weekOfYear+"周("+forMonth+aftMonth+")"; } }catch (Exception e) { e.printStackTrace(); } return reTime; } Windows下创建的txt文件到solaris上用java读取变成乱码~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ xpath evaluate 小型管理系统的问题 为什么找不到符号? 请教静态变量的线程安全问题 JBulder里数据库文件的问题? Set是没有顺序的,意思是说和我加入Set时的顺序无关吗? 关于编译的简单问题,请帮忙 关于线程 JAVA的一些基础知识点 java 如何在窗体最小化时实现状态栏闪动效果?就像某一个窗体触发某一事件,或就像QQ接收到对方发送的消息时,状态栏的最小化图标闪动的效果 如何將人的聲音轉變成聲波圖顯示
应该很方便的。
int week = calendar.get(Calendar.DAY_OF_WEEK);
calendar.setTimeInMillis(calendar.getTimeInMillis() - 24l * 60 * 60 * 1000 * (week-1));//这里重新设置calendar,用的是毫秒数来设置,目的是寻找本周的第一天
System.out.println(""+calendar.get(calendar.YEAR)+calendar.get(calendar.MONTH+1)+calendar.get(calendar.DATE));
W=([Y/4]-[Y/100]+[Y/400]+S)%7各项参数的含义是:
W:所求的星期(星期一是1,星期二是2,等等,星期天是0)
Y:年份;
S:从该年的1月1号开始到所需要计算的日期这个区间中的天数(不包括当天。这个需要另外写一个简单的逻辑去计算)
方括号表示取整(不是四舍五入)比如要计算2009年1月4号,则代入:W=([2009/4]-[2009/100]+[2009/400]+3)%7=(502-20+5=3)%7=0
所以2009年1月4号是星期天。祝你好运。
public static void main(String[] args) throws Exception {
printCalendar("20090122", "20090224");
}
private static void printCalendar(String begin, String end) throws Exception {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar c1 = Calendar.getInstance();
c1.setTime(simpleDateFormat.parse(begin));
Calendar c2 = Calendar.getInstance();
c2.setTime(simpleDateFormat.parse(end));
int count = 0;
int week = 0;
while (c2.after(c1)) {
week = c1.get(Calendar.DAY_OF_WEEK);
System.out.print (simpleDateFormat.format(c1.getTime()) + "-");
c1.add(Calendar.DATE, 8 - week);
System.out.print (simpleDateFormat.format(c2.after(c1) ? c1.getTime() : c2.getTime()));
System.out.println (" 第"+(++count)+"周");
}
}
}我个人认为利用Calendar类比在jsp页面中调用javascript方法方便
LZ可以把算出的值放到相应作用域中,在jsp页面中可用EL或其他标签都可得到
//第一个是粒度,第二个是求出的时间模型,第三个是时间,取周的时候用到.最后一个用来加增加周数
@SuppressWarnings("static-access")
public String formatTime(String granularity,String time,Date stDate,int add){
String reTime = "";//返回时间
//int week = Integer.parseInt(time.substring(5,7));//第几周
try{
SimpleDateFormat config = new SimpleDateFormat("MM月dd日");
//SimpleDateFormat configDate = new SimpleDateFormat("yyyy-MM-dd");
Calendar ca1 = Calendar.getInstance(Locale.CHINA);
Calendar ca2 = Calendar.getInstance(Locale.CHINA);
ca1.setTime(stDate);//开始
ca2.setTime(stDate);//setTimeInMillis(stDate.getTime()+7*1000*3600*24);//结束
ca1.setFirstDayOfWeek(Calendar.MONDAY);
ca2.setFirstDayOfWeek(Calendar.MONDAY);
ca1.set(ca1.DAY_OF_WEEK, Calendar.MONDAY);
ca2.set(ca2.DAY_OF_WEEK, Calendar.SUNDAY);
ca1.add(ca1.WEEK_OF_YEAR, add);
ca2.add(ca2.WEEK_OF_YEAR, add);
/*System.out.println("开始时间"+config.format(ca1.getTime()));
System.out.println("结束时间"+config.format(ca2.getTime()));
System.out.println(time);*/
String forMonth = config.format(ca1.getTime());//开始月时间
String aftMonth = config.format(ca2.getTime());//结束月时间
int year = 0;
if(forMonth.substring(0,2).equals("12")&&aftMonth.substring(0,2).equals("01")){
year = Integer.parseInt(time.substring(0,4))+1;
}else{
year = Integer.parseInt(time.substring(0,4));
}
String weekOfYear = "";//第几周
if(time.length()==6){
weekOfYear = "0"+time.substring(5,6);
}else{
weekOfYear = time.substring(5,7);
}
if(granularity.equals("2")){//格式为 XXXX年第XX周(XX月XX日--XX月XX日)
reTime = year+"月第"+weekOfYear+"周("+forMonth+aftMonth+")";
}
}catch (Exception e) {
e.printStackTrace();
}
return reTime;
}