一周乱弹(1,spring 获取 servletContext 方式 2,Calendar常用操作及与date,string的转换)

来源:互联网 发布:做淘宝客服需要交钱吗 编辑:程序博客网 时间:2024/06/04 18:12

1,spring 获取 servletContext 方式。
对于web容器来说,ServletContext接口定义了一个servlet环境对象,这个对象定义了一个在servlet引擎上的servlet的视图。通过使用这个对象,servlet可以记录事件,得到资源并得到来自servlet的引擎类。
servlet容器在启动时会加载web应用,并为每个web应用创建唯一的servlet context对象(每个web应用内的所有servlet共享同一个servletContext),可以把ServletContext看成是一个Web应用的服务器端组件的共享内存,在ServletContext中可以存放共享数据,他提供了4个读取和设置共享数据的方法。具体见api帮助文档。
方式一:
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
方式二:
public void aa( HttpServletRequest request ){
ServletContext servletContext=request.getSession().getServletContext();

String AbsolutelyPath =servletContext.getRealPath(“/template/alarm/报警求助导出模版.xls”);

2,Calendar常用操作。

1.Calendar 转化 String  //获取当前时间的具体情况,如年,月,日,week,date,分,秒等         Calendar calendat = Calendar.getInstance();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");String dateStr = sdf.format(calendar.getTime());2.String 转化CalendarString str="2010-5-27";SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");Date date =sdf.parse(str);Calendar calendar = Calendar.getInstance();calendar.setTime(date);3.Date 转化StringSimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");String dateStr=sdf.format(new Date());4.String 转化Date String str="2010-5-27";SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");Date birthday = sdf.parse(str);5.Date 转化CalendarCalendar calendar = Calendar.getInstance();calendar.setTime(new java.util.Date());6.Calendar转化DateCalendar calendar = Calendar.getInstance();java.util.Date date =calendar.getTime();

相关方法:

    /**     * @description:获取当前月第一天     * @param:     * @return: date     * @author:kanggw     * @createTime:2017-05-24 10:49:40     */    public static Date getCurrentMonthfirstDay() {        //获取当前月第一天:        Calendar c = Calendar.getInstance();        c.add(Calendar.MONTH, 0);       //当前月        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天        c.set(Calendar.HOUR,0);     //设置hour为0        c.set(Calendar.MINUTE, 0);        c.set(Calendar.SECOND,0);        Date  firstDay= c.getTime(); // Mon May 01 00:00:00 CST 2017        return firstDay;    }    /**     * @description:获取当前月最后一天     * @param:     * @return:     * @author:kanggw     * @createTime:2017-05-24 10:50:50     */    public static Date getCurrentMonthLastDay() {        //获取当前月最后一天        Calendar ca = Calendar.getInstance();        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));        ca.set(Calendar.HOUR,0);     //设置hour为0        ca.set(Calendar.MINUTE, 0);        ca.set(Calendar.SECOND,0);        Date lastDay =  ca.getTime();        return lastDay;    }    /*** @desc   得到一天起始时间* @author kanggw* @datetime 2017/1/16,14:35*/public static Date getTimesmorning(Date d){ Calendar cal = Calendar.getInstance();cal.setTime(d);cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 0); Date date = cal.getTime();return date; }/** * @desc   得到一天结束时间 * @author kanggw * @datetime 2017/1/16,14:35 */public static Date getTimesnight(Date d){ Calendar cal = Calendar.getInstance();cal.setTime(d);cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.MILLISECOND, 0);Date date = cal.getTime();return date; }
原创粉丝点击