`
dingran
  • 浏览: 372398 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

拉尔森函数,用于根据日期计算星期几

 
阅读更多

现在的函数输入字符串的格式是:yyyy-MM-dd

例如:2011-01-22

输出是字符串:星期六

 

/**
     * James larsson method.Caculate week from date.
     * @param date fromat:yyyy-MM-dd
     * @return
     */
    private String CaculateWeekDay(String date)
    {
       
        String dateYear = date.substring(0, 4);
        String dateMonth = date.substring(5, 7);
        String dateDay = date.substring(8, 10);
        int y = Integer.parseInt(dateYear);
        int m = Integer.parseInt(dateMonth);
        int d = Integer.parseInt(dateDay);
       
        if(m==1||m==2) {
            m+=12;
            y--;
        }
        int iWeek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
        switch(iWeek)
        {
        case 0:
            return this.getString(“周一”);
        case 1:
            return this.getString(“周二”);
        case 2:
            return this.getString(“周三”);
        case 3:
            return this.getString(“周四”);
        case 4:
            return this.getString(“周五”);
        case 5:
            return this.getString(“周六”);
        case 6:
            return this.getString(“周日”);
        }
        return "";
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics