week

package com.isoftstone.dcf.portal.bmanager.report.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeekReportDateUtils {
    
    /**
     * (周)计算(周) 上期和去年同期的起止日期和起止周
     * 计算上期的起止时间 和去年同期  type 0本期    1上期   2去年同期
     * 起始日期key  beginkey  endkey
     * 本期:begin    end
     * (标记)此方法暂无问题
     */
    public static Map<String,String> getWeekDate(int type,String beginkey,String endkey,String begin,String end){
        //将时间推算到所在周的第一天和最后一天
        begin = getWeekStartAndEnd(0,begin);//计算开始时间所在的周的周一
        end = getWeekStartAndEnd(1,end);//计算结束时间所在周的周末最后一天
        //开始日期和结束日期分布所在的周
        int beginWeek = getDateOfWeek(begin);
        int endWeek = getDateOfWeek(end);
        
        Map<String, String> map = new HashMap<String, String>();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null; // 开始日期
        Date dEnd = null; // 结束日期
        try {
            date1 = sdf.parse(begin);
            dEnd = sdf.parse(end);
            
            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(date1);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(dEnd);
            calBegin.setFirstDayOfWeek(Calendar.MONDAY);  //本年周一为第一天
            calEnd.setFirstDayOfWeek(Calendar.MONDAY);

            calBegin.setMinimalDaysInFirstWeek(1);//第一个周最少为1天
            calEnd.setMinimalDaysInFirstWeek(1);
            
            if (type == 1) {// 计算上期
                // 计算查询时间段相隔多少周
                int week = getWeekCount(sdf.format(date1),sdf.format(dEnd));
                //计算上一年有多少个周
                int sweekCount = getAllWeeks(String.valueOf(calBegin.get(Calendar.YEAR)-1));
                //计算本期年的本周 往前推是否涉及跨年
                if(beginWeek-week<=0){//需要跨年  
                    //去年应该推到的周
                    int qunianw = sweekCount-(week-beginWeek);
                    calBegin.add(Calendar.YEAR,-1);
                    if(qunianw==53){//最后一周是12月31号结束
                       calBegin.set(Calendar.MONTH, 11);
                       calBegin.set(Calendar.DAY_OF_MONTH,31);
                    }else if(qunianw==1){
                        //第一周是1月1号开始
                         calBegin.set(Calendar.MONTH,0);
                         calBegin.set(Calendar.DAY_OF_MONTH,1);
                    }else{
                        //如果不是开始周和结束周就这么干
                        calBegin.set(Calendar.WEEK_OF_YEAR,qunianw);    
                    }
                }else{
                    //不需要跨年
                    //去年应该推到的周
                    int jinnianweek = beginWeek-week;
                    if(jinnianweek==1){
                        //第一周是1月1号开始
                         calBegin.set(Calendar.MONTH,0);
                         calBegin.set(Calendar.DAY_OF_MONTH,1);
                    }else{
                        calBegin.set(Calendar.WEEK_OF_YEAR,jinnianweek);// 像前推week周
                    }
                }
                //计算本期年的本周 往前推是否涉及跨年
                if(endWeek-week<=0){//需要跨年
                    calEnd.add(Calendar.YEAR, -1);
                    // 去年应该推到的周
                    int qunianw = sweekCount - (week - endWeek);
                    if (qunianw == 53) {// 最后一周是12月31号结束
                        calEnd.set(Calendar.MONTH, 11);
                        calEnd.set(Calendar.DAY_OF_MONTH, 31);
                    } else if (qunianw == 1) {
                        // 第一周是1月1号开始
                        calEnd.set(Calendar.MONTH, 0);
                        calEnd.set(Calendar.DAY_OF_MONTH, 1);
                    } else {
                        // 如果不是开始周和结束周就这么干
                        calEnd.set(Calendar.WEEK_OF_YEAR, qunianw);
                    }
                }else{
                    calEnd.add(Calendar.WEEK_OF_YEAR, -week);// 像前推week周
                }
                map.put(beginkey, getWeekStartAndEnd(0,sdf.format(calBegin.getTime())));
                map.put(endkey, getWeekStartAndEnd(1,sdf.format(calEnd.getTime())));
//                //得到起始周的周一   和结束周的周末
            }else if(type==2){
                calBegin.add(Calendar.YEAR, -1);// 去年同期
                calEnd.add(Calendar.YEAR, -1);// 去年同期
                if (beginWeek == 53) {// 最后一周是12月31号结束
                    calBegin.set(Calendar.MONTH, 11);
                    calBegin.set(Calendar.DAY_OF_MONTH, 31);
                } else if (beginWeek == 1) {
                    // 第一周是1月1号开始
                    calBegin.set(Calendar.MONTH, 0);
                    calBegin.set(Calendar.DAY_OF_MONTH, 1);
                } else {
                    // 如果不是开始周和结束周就这么干
                    calBegin.set(Calendar.WEEK_OF_YEAR, beginWeek);
                }
                if (endWeek == 53) {// 最后一周是12月31号结束
                    calEnd.set(Calendar.MONTH, 11);
                    calEnd.set(Calendar.DAY_OF_MONTH, 31);
                } else if (endWeek == 1) {
                    // 第一周是1月1号开始
                    calEnd.set(Calendar.MONTH, 0);
                    calEnd.set(Calendar.DAY_OF_MONTH, 1);
                } else {
                    // 如果不是开始周和结束周就这么干
                    calEnd.set(Calendar.WEEK_OF_YEAR, endWeek);
                }
                //得到起始周的周一   和结束周的周末
                map.put(beginkey, getWeekStartAndEnd(0,sdf.format(calBegin.getTime())));
                map.put(endkey, getWeekStartAndEnd(1,sdf.format(calEnd.getTime())));
            }
            
          } catch (ParseException e) {
              e.printStackTrace();
          }
        return map;
        
    }
    
    
    
    /**
     * (周)返回起止时间内的所有自然周
     * @param begin  时间起
     * @param end   时间止
     * @param startw  周起
     * @param endW    周止
     * (标记)此方法无问题
     * @return
     */
    public static List<String> getWeeksList(String begin,String end){
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
         List<String> lDate = new ArrayList<String>();
          Date date1 = null; // 开始日期
          Date dEnd = null; // 结束日期
          try {
            date1 = sdf.parse(begin);
            dEnd = sdf.parse(end);
            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(date1);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(dEnd);
            //开始时间是今年的第几周
            //添加第一个周
            int beginww = getDateOfWeek(begin);
            int endww = getDateOfWeek(end);
            
            int beginY = calBegin.get(Calendar.YEAR);
            int endY = calEnd.get(Calendar.YEAR);
            
            int weekall = getAllWeeks(beginY+"");//开始日期总共有多少周
            //如果是同一年
            do{
                lDate.add(beginY+"年第"+beginww+"周");
                if(beginww==weekall){
                    beginww = 0;
                    beginY++;
                    weekall = getAllWeeks(beginY+"");
                }
                if(beginY==endY &&beginww==endww){
                    break;
                }
                beginww++;
            }while(beginY<=endY);
          } catch (ParseException e) {
              e.printStackTrace();
          }
         return lDate;
    }

    /**
     * 获取两个日期段相差的周数
     * (标记)(此方法没有问题)
     */
    public static int getWeekCount(String date1,String dEnd){
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c_begin = Calendar.getInstance();
        Calendar c_end = Calendar.getInstance();
        try {
            c_begin.setTime(sdf.parse(date1));
            c_end.setTime(sdf.parse(dEnd));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int count = 1;
        c_begin.setFirstDayOfWeek(Calendar.MONDAY);
        c_end.setFirstDayOfWeek(Calendar.MONDAY);
        c_begin.setMinimalDaysInFirstWeek(1);
        c_end.setMinimalDaysInFirstWeek(1);
        //
        int beginy = c_begin.get(Calendar.YEAR);
        int endy = c_end.get(Calendar.YEAR);
        
        int beginw = getDateOfWeek(date1);//获取当前所在的周
        int endw = getDateOfWeek(dEnd);//获取当前所在的周
        int beginSumWeek = getAllWeeks(String.valueOf(beginy));
        
        count = (endy-beginy)*beginSumWeek+(endw-beginw)+1;
        
        return count;
    }
    
    /**
     * 返回该年有多少个自然周
     * @param year   最多53  一般52
     * 如果12月月末今天在本年53周(属于第二年第一周) 那么按照当年53周算 哪怕只有1天
     * @return
     */
    public static int getAllWeeks(String year){
          Calendar calendar = Calendar.getInstance();
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          try {
            calendar.setTime(sdf.parse(year+"-12-31"));
          } catch (ParseException e) {
            e.printStackTrace();
          }
          calendar.setFirstDayOfWeek(Calendar.MONDAY);// 周的第一天为周一
          calendar.setMinimalDaysInFirstWeek(1);
          
          
          int week =  calendar.get(Calendar.WEEK_OF_YEAR);
          if(week==1){
              week = 53;
          }
//          if(week != 53){
//             week = 52;
//          }
          return week;
    }
    
    /**
     * 根据周 返回日期所在周的第一天或最后一天
     * type:0 第一天    type:1最后一天
     * (标记)此方法经过测试 符合本次要求
     */
    public static String getWeekStartAndEnd(int type,String startDate){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null; // 日期
        try {
            date1 = df.parse(startDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c1 = Calendar.getInstance();
        c1.setTime(date1);
        c1.setFirstDayOfWeek(Calendar.MONDAY);// 周的第一天为周一
        c1.setMinimalDaysInFirstWeek(1);
        // 去年的开始的本周
//        c1.set(Calendar.WEEK_OF_YEAR, Integer.parseInt(startWeek));
        if(type==0){
            // 得到起始周的周一 和结束周的周末        考虑跨年情况
            int day_of_week = c1.get(Calendar.DAY_OF_WEEK)-1;
            int year = c1.get(Calendar.YEAR);
            if (day_of_week == 0)
                day_of_week = 7;
            c1.add(Calendar.DATE, -day_of_week+1);
            int newy = c1.get(Calendar.YEAR);//如果往前推  年发生变化  那么是存在跨年情况  设置本周起始为1月1号
            if(year!=newy){
                //存在跨年
                c1.set(year, 0, 1);
            }
        }else if(type==1){
            // 本周周末
            int day_of_week_end = c1.get(Calendar.DAY_OF_WEEK) - 1;
            int year = c1.get(Calendar.YEAR);
            if (day_of_week_end == 0)
                day_of_week_end = 7;
            c1.add(Calendar.DATE, -day_of_week_end + 7);
            int newy = c1.get(Calendar.YEAR);//如果往前推  年发生变化  那么是存在跨年情况  设置本周起始为1月1号
            if(year!=newy){
                //存在跨年
                c1.set(year, 11, 31);
            }
        }
        return df.format(c1.getTime());
    }
    
    /**
     * 获取当前日期为第几周
     * 次方法经过每年1号  12月28  30好测试 暂无问题  (标记)
     * @param date
     * @return
     */
    public static int getDateOfWeek(String date){
         Calendar calendar = Calendar.getInstance();
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
          try {
            calendar.setTime(sdf.parse(date));
          } catch (ParseException e) {
            e.printStackTrace();
          }
          calendar.setFirstDayOfWeek(Calendar.MONDAY);// 周的第一天为周一
          calendar.setMinimalDaysInFirstWeek(1);//第一周最少1天
          int count = calendar.get(Calendar.WEEK_OF_YEAR);//得到其实日期的周
          //获取月
          int month = calendar.get(Calendar.MONTH)+1;
          //如果是12月
          if(month==12&&count==1){
              return 53;
          }
        return count;
    }
    
    
    
    public static void main(String[] args) throws Exception{
          //获取本周的第一天
//          System.out.println(getWeekStartAndEnd(1,"2014-12-30"));
        
//        System.out.println("两个时间相差:"+getWeekCount("2014-12-29","2015-01-05")+"周");
        
        //测试两个时间内的所有的周
//        System.out.println(getWeeksList("2014-12-29","2015-01-05"));
        
        
//        System.out.println(getWeekDate(1,"s","e","2014-5-20","2014-06-10"));
          
        System.out.println(getDateOfWeek("2012-12-24"));
          
        //测试当天在本年度的第几天
//        System.out.println("2014-01-01:"+getDateOfWeek("2014-12-28"));
//        System.out.println("2015-01-01:"+getDateOfWeek("2015-12-28"));
//        System.out.println("2016-01-01:"+getDateOfWeek("2016-12-28"));
//        System.out.println("2017-01-01:"+getDateOfWeek("2017-12-28"));
//        System.out.println("2018-01-01:"+getDateOfWeek("2018-12-28"));
//        System.out.println("2019-01-01:"+getDateOfWeek("2019-12-28"));
//        System.out.println("2020-01-01:"+getDateOfWeek("2020-12-28"));
//        System.out.println("2021-01-01:"+getDateOfWeek("2021-12-28"));
//        System.out.println("2022-01-01:"+getDateOfWeek("2022-12-28"));
//        System.out.println("2023-01-01:"+getDateOfWeek("2023-12-28"));
//        System.out.println("2024-01-01:"+getDateOfWeek("2024-12-28"));
//        System.out.println("2025-01-01:"+getDateOfWeek("2025-12-28"));
//        System.out.println("2026-01-01:"+getDateOfWeek("2026-12-28"));
//        System.out.println("2027-01-01:"+getDateOfWeek("2027-12-28"));
//        System.out.println("2028-01-01:"+getDateOfWeek("2028-12-28"));
     }
    
}



内容概要:本文档详细介绍了Analog Devices公司生产的AD8436真均方根-直流(RMS-to-DC)转换器的技术细节及其应用场景。AD8436由三个独立模块构成:轨到轨FET输入放大器、高动态范围均方根计算内核和精密轨到轨输出放大器。该器件不仅体积小巧、功耗低,而且具有广泛的输入电压范围和快速响应特性。文档涵盖了AD8436的工作原理、配置选项、外部组件选择(如电容)、增益调节、单电源供电、电流互感器配置、接地故障检测、三相电源监测等方面的内容。此外,还特别强调了PCB设计注意事项和误差源分析,旨在帮助工程师更好地理解和应用这款高性能的RMS-DC转换器。 适合人群:从事模拟电路设计的专业工程师和技术人员,尤其是那些需要精确测量交流电信号均方根值的应用开发者。 使用场景及目标:①用于工业自动化、医疗设备、电力监控等领域,实现对交流电压或电流的精准测量;②适用于手持式数字万用表及其他便携式仪器仪表,提供高效的单电源解决方案;③在电流互感器配置中,用于检测微小的电流变化,保障电气安全;④应用于三相电力系统监控,优化建立时间和转换精度。 其他说明:为了确保最佳性能,文档推荐使用高质量的电容器件,并给出了详细的PCB布局指导。同时提醒用户关注电介质吸收和泄漏电流等因素对测量准确性的影响。
03-09
### Week 4 Content or Tasks In the context provided, week 4 content or tasks are not explicitly detailed within the given references. However, based on the discussion regarding `User.all_tasks` collection changes and applying expiration using event hooks in SQLAlchemy[^1], one can infer that managing task collections dynamically might be part of the curriculum. For a more specific focus on week 4: If the course involves progressively complex topics related to database interactions with Python frameworks like SQLAlchemy, by week 4 students may delve into advanced techniques such as listening for events on relationships (like appending, removing, or bulk replacing items in `all_tasks`). This technique ensures that when certain attributes change—such as adding new tasks—the application can automatically expire cached properties or recalibrate dependent views without manual intervention. The code snippet demonstrates how to set up listeners for these operations: ```python from sqlalchemy import event, inspect @event.listens_for(User.all_tasks, "append") @event.listens_for(User.all_tasks, "remove") @event.listens_for(User.all_tasks, "bulk_replace") def _expire_user_current_week_tasks(target, value, initiator): inspect(target).session.expire(target, ['current_week_tasks']) ``` This approach allows developers to maintain consistency between different parts of an application where updates need synchronization across multiple components or caches. For instance, updating weekly summaries whenever underlying data changes could prevent stale information from being displayed.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值