据当前时间获取本学期周次

 已经设定好了本学期第一个星期星期一的日期,怎样根据当前时间获取获取周次呢

DateTime dtime=DateTime.Now;

        string datetime = "2011-3-1";//本学期第一个星期一的日期

        TimeSpan ts1 = new TimeSpan(dtime.Ticks);
        TimeSpan ts2 = new TimeSpan(Convert.ToDateTime(datetime).Ticks);
        TimeSpan ts = ts2.Subtract(ts1).Duration();

        //确定此时间在一年中的位置
        int dayOfYear = ts.Days;
        //本学期第一天
        DateTime tempDate = new DateTime(Convert.ToDateTime(datetime).Year, Convert.ToDateTime(datetime).Month, Convert.ToDateTime(datetime).Day);

        int tempDayOfWeek = (int)tempDate.DayOfWeek;
        tempDayOfWeek = tempDayOfWeek == 0 ? 7 : tempDayOfWeek;
        确定星期几
        int index = (int)dtime.DayOfWeek;
        index = index == 0 ? 7 : index;

        //当前周的范围
        DateTime retStartDay = dtime.AddDays(-(index - 1));
        DateTime retEndDay = dtime.AddDays(7 - index);

        //确定当前是第几周
        int weekIndex = (int)Math.Ceiling(((double)dayOfYear + tempDayOfWeek) / 7);
        Response.Write("当前是第"+weekIndex+"周");

 

http://wenku.baidu.com/view/f23f9b67f5335a8102d22014.html
http://hi.baidu.com/yssc/blog/item/db24d1580e68c68d810a18ee.html

http://www.cnblogs.com/wulg/archive/2011/03/17/1986994.html

package com.sw; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author Czy * @time Jul 6, 2019 * @detail * */ public class MainSw { /** * 强智教务系统 */ //////////////////////////////////////////////////////// private String account = ""; private String password = ""; private String url = "https://jwgl.sdust.edu.cn/app.do"; //////////////////////////////////////////////////////// /** * 注意:由于处理Json需要引入Json包,所以暂时不处理Json数据,假设当前学期是2018-2019-2,当前是18 * 引入Json包后可以直接调用getCurrentTime()方法得到字符串再转化为Json获取数据 * 其实学期当前是可以自行计算的,还可以减少对强智服务器的请求 */ //////////////////////////////////////////////////////// private String curWeek = "18"; private String curTerm = "2018-2019-2"; //////////////////////////////////////////////////////// private Map<String, String> params = new HashMap<>(); private Map<String, String> headers = new HashMap<>(); public MainSw() { this.params.put("method", "authUser"); this.params.put("xh", this.account); this.params.put("pwd", this.password); String reqResult = Http.httpRequest(this.url, this.params, "GET", this.headers); System.out.println(reqResult); String[] reqResultArr = reqResult.split(","); if(reqResultArr[0].charAt(9) == '0') { System.out.println("登录失败"); System.exit(0); }else { this.headers.put("token", reqResultArr[2].substring(9, reqResultArr[2].length()-1)); } } public MainSw getStudentInfo() { this.params.put("method", "getUserInfo"); this.params.put("xh", this.account); return this; } public MainSw getCurrentTime() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); this.params.put("method", "getCurrentTime"); this.params.put("currDate", df.format(new Date())); return this; } public MainSw
最新发布
03-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值