java 根据 week num 利用calendar 得到这一周的起始时间

最近做一个项目,在根据时间段查询数据时,做法比较变态,按照我们通常的做法,放两个日期控件,作为起始时间,然后去进行查询。这种做法很灵活,程序也很好写,也简单。但这是一个小日本的项目,非常的BT。 查询居然按照周数来差,将一年分成很多个周,然后可以选取一个周来查询数据。这种设计方法,我实在无语,但人家都这么设计了,我们做外包的只能接受这种做法。用java 来 实现 这种做法:
程序代码 程序代码

import java.util.Calendar;
import java.util.GregorianCalendar;

public class dateTest {
    
    public static void main(String[] args) {
        Calendar gregorianCalendar = new GregorianCalendar();  
        gregorianCalendar.setFirstDayOfWeek(Calendar.MONDAY);          
        gregorianCalendar.setMinimalDaysInFirstWeek(4);  
          
        int numWeekofYear = 32;
        int year = 2013;  
          
        gregorianCalendar.set(Calendar.YEAR , year);  
        gregorianCalendar.set(Calendar.WEEK_OF_YEAR , numWeekofYear);  
        
        System.out.println( gregorianCalendar.get(Calendar.DAY_OF_MONTH)  
                            + "/" + (gregorianCalendar.get(Calendar.MONTH)  
                            + 1 ) + "/" + gregorianCalendar.get  
                            (Calendar.YEAR));
        
        gregorianCalendar.add(Calendar.DAY_OF_WEEK, 6);
        
        System.out.println( gregorianCalendar.get(Calendar.DAY_OF_MONTH)  
                + "/" + (gregorianCalendar.get(Calendar.MONTH)  
                + 1 ) + "/" + gregorianCalendar.get  
                (Calendar.YEAR));
        
    }

}


这样,可以成功得到 某一个周的起始时间。

但如果是javascript 端,如何做到呢,毕竟周数是由前端传送给后端的。所以还必须在前端得到。

程序代码 程序代码

<html>
<body>
<SCRIPT Language="JavaScript">
    
    /*得到当前日期是第几周.*/
    function getIsoWeeks(d) {
         var first = new Date(d.getFullYear(),0,4);
         var dayms = 1000 * 60 * 60 * 24;
         var numday = ((d - first)/dayms)
         var weeks = Math.ceil((numday + first.getDay()+1) / 7) ;
         return weeks  
    }

    function isLeapYear(year)
    {
       return (year % 4 === 0) && (year % 100 !== 0) || (year % 400 === 0);
    }
    
     function getWeeksOfYear(year)
     {
       var firstDayOfYear = new Date(year, 0, 1);
       var days = firstDayOfYear.getDay() + (isLeapYear(year) ? 366 : 365);
    
       return Math.ceil(days/7)
     }
    
         Date.prototype.getWeek = function() {
            var onejan = new Date(this.getFullYear(),0,1);
            return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
        }
        
        function getDateRangeOfWeek(weekNo){
                var d1 = new Date();
                numOfdaysPastSinceLastMonday = eval(d1.getDay()- 1);
                d1.setDate(d1.getDate() - numOfdaysPastSinceLastMonday);
                var weekNoToday = d1.getWeek();
                var weeksInTheFuture = eval( weekNo - weekNoToday );
                d1.setDate(d1.getDate() + eval( 7 * weeksInTheFuture ));
                var rangeIsFrom =  eval(d1.getMonth()+1)   +"/"  +  d1.getDate()  + "/"  +   d1.getFullYear();
                d1.setDate(d1.getDate() + 6);        
                var rangeIsTo = eval(d1.getMonth()+1)   +"/"  +  d1.getDate()  + "/"  +   d1.getFullYear() ;
                return rangeIsFrom + " to "+rangeIsTo;
        };


var mydate = new Date();
document.write(getIsoWeeks(mydate));
document.write(getDateRangeOfWeek(getIsoWeeks(mydate)));
document.write(getWeeksOfYear(2013));
</SCRIPT>
</body>
</html>




除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
[本日志由 轻舞肥羊 于 2013-08-06 00:10 AM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.