package com.zyna.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import com.zyna.db.DbAccessor;
import com.zyna.dbstructure.XML;
import com.zyna.pojo.OperateTask;
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SampleServlet() {
super();
}
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
XML xml = new XML();
try {
xml.init();
} catch (ParserConfigurationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//SampleServlet?time="+time+"justUnit="+justUnit+"department="+department+"eventType="+eventType;
//接收 查询参数
String time = request.getParameter("time");
String justUnit = request.getParameter("justUnit");
String department = request.getParameter("department");
String eventType = request.getParameter("eventType");
//response.setContentType("text/html");
//response.setCharacterEncoding("GBK");
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
//connection database
DbAccessor dbAccessor = new DbAccessor();
//sql
StringBuffer sql = new StringBuffer(256);
sql.append("SELECT ot_id, ot_item, ot_type, ot_level, ot_frequency, ot_desc, ot_attachment, ot_busid, ot_createtime, ot_createuser FROM t_opreate_task where 1=1");
if(null != time && time != ""&&null != justUnit && justUnit != ""){
Map<String,String> map = splitDateStr(time);
String day = map.get("day");
String month = map.get("month");
String year = map.get("year");
String startDay = "";
String endDay = "";
//周报表
if("week".equals(justUnit)){
Map<String,Date> map_Week = getThisWeekStartDayAndEndDay(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
Date calFirstDayInThisWeek = map_Week.get("calFirstDayInThisWeek");
Date calLastDayInThisWeek = map_Week.get("calLastDayInThisWeek");
String calFirstDayInThisWeek_Str = dateToString(calFirstDayInThisWeek,"yyyy-MM-dd");
startDay = calFirstDayInThisWeek_Str;
String calLastDayInThisWeek_Str = dateToString(calLastDayInThisWeek,"yyyy-MM-dd");
endDay = calLastDayInThisWeek_Str;
}//月报表
else if("month".equals(justUnit)){
Map<String,Date> map_Month = getThisMonthStartDayAndEndDay(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
Date calFirstDayInThisMonth = map_Month.get("calFirstDayInThisMonth");
Date calLastDayInThisMonth = map_Month.get("calLastDayInThisMonth");
String calFirstDayInThisMonth_Str = dateToString(calFirstDayInThisMonth,"yyyy-MM-dd");
String ccalLastDayInThisMonth_Str = dateToString(calLastDayInThisMonth,"yyyy-MM-dd");
startDay = calFirstDayInThisMonth_Str;
endDay = ccalLastDayInThisMonth_Str;
}else{
return;
}
//where ot_createtime between '2011-04-13' and '2011-11-13';
sql.append(" and")
.append(" ot_createtime")
.append(" between")
.append(" '")
.append( startDay)
.append(" '")
.append(" and")
.append(" '")
.append( endDay)
.append(" '")
;
// System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
}
if(null != time && time != "" && (justUnit=="" || justUnit == null)){
Map<String,String> tempMap = splitDateStr(time);
String day = tempMap.get("day");
String month = tempMap.get("month");
String year = tempMap.get("year");
Calendar calendar = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day));
Date date = calendar.getTime();
String date_Str = dateToString(date,"yyyy-MM-dd");
sql.append(" and")
.append(" ot_createtime = ")
.append(" '")
.append(date_Str)
.append(" '")
;
}
if(null != department && department != ""){
sql.append(" and")
.append(" ot_busid=")
.append(" '")
.append(department)
.append(" '")
;
}
if(null != eventType && eventType != "" && 1==1){
sql.append(" and")
.append(" ot_type='"+eventType+"'");
}
for(int i = 0 ; i < 100 ; i++){
System.err.println("sql-->"+sql.toString());
}
//String sql = "SELECT ot_id, ot_item, ot_type, ot_level, ot_frequency, ot_desc, ot_attachment, ot_busid, ot_createtime, ot_createuser FROM t_opreate_task";
//run sql
ResultSet resultSet = dbAccessor.execute(sql.toString());
List<OperateTask> list = new ArrayList<OperateTask>();
try {
while(resultSet.next()){
String[] strs = new String[10] ;
//ot_id
strs[0]= String.valueOf(resultSet.getInt(1));
//ot_item
strs[1] =resultSet.getString(2);
//ot_type
strs[2]=String.valueOf(resultSet.getInt(3));
//ot_level
strs[3]=String.valueOf(resultSet.getInt(4));
//ot_frequency
strs[4]=String.valueOf(resultSet.getInt(5));
xml.create(strs);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//out.println("The locale you selected is " + locale+"t_opreate_task"+id+"-->"+list);
try {
while(resultSet.next()){
int id = resultSet.getInt(1);
System.out.println("id-->"+id);
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//out.println(locale+"xiaoyu"+resultSet.toString());
try {
xml.output(out);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
// Calendar calendar = Calendar.getInstance();
//
// int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
//
// Calendar calFirstDayInThisWeek = (Calendar)calendar.clone();
//
// //例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点: add(Calendar.DAY_OF_MONTH, -5)。
// calFirstDayInThisWeek.add(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_WEEK)-dayOfWeek);
//
// Calendar calLastDayInThisWeek = (Calendar)calendar.clone();
//
// calLastDayInThisWeek.add(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_WEEK)-dayOfWeek);
//
// System.out.println("今天是本周的第"+dayOfWeek+"天"+"--本周星期一具日期为:"+calFirstDayInThisWeek.getTime()+"-->"+calLastDayInThisWeek.getTime());
//
// List<Map<String,Date>> tempList = getThisWeekStartDayAndEndDay("");
//
// Map<String,Date> tempMap = tempList.get(0);
//
// System.out.println("tempMap_1-->"+tempMap.get("calFirstDayInThisWeek")+"---"+tempMap.get("calLastDayInThisWeek"));
//splitDateStr("");
//Map map = splitDateStr("07/11/2011");
// System.out.println("map_day-->"+map.get("day"));
//
// System.out.println("map_month-->"+map.get("month"));
//
// System.out.println("map_year-->"+map.get("year"));
//
// Map map = getThisWeekStartDayAndEndDay(2011,7,11);
//
// System.out.println("calFirstDayInThisWeek-->"+map.get("calFirstDayInThisWeek"));
//
// System.out.println("calLastDayInThisWeek-->"+map.get("calLastDayInThisWeek"));
//System.out.println("Calendar.JULY"+Calendar.JULY);
//getThisMonthStartDayAndEndDay(1,2,3);
//Calendar calendar = Calendar.getInstance();
//int maximum = calendar.getMaximum(calendar.DAY_OF_MONTH);
//System.out.println("maximum-->"+maximum);
// Map<String ,Date> map = getThisMonthStartDayAndEndDay(2011,7,11);
// Date calFirstDayInThisMonth = map.get("calFirstDayInThisMonth");
// Date calLastDayInThisMonth = map.get("calLastDayInThisMonth");
// //2011-04-13
// DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
// String calFirstDayInThisMonth_str = df.format(calFirstDayInThisMonth);
// String calLastDayInThisMonth_str = df.format(calLastDayInThisMonth);
// System.out.println("-----------------------------------------");
// System.out.println("calFirstDayInThisMonth_str-->"+calFirstDayInThisMonth_str);
// System.out.println("calLastDayInThisMonth_str-->"+calLastDayInThisMonth_str);
// System.out.println("-----------------------------------------");
// Map<String,String> map = splitDateStr("07/11/2011");
//
// String day = map.get("day");
//
// String month = map.get("month");
//
// String year = map.get("year");
//
//
// System.out.println("今天是:"+year+"_"+month+"_"+day);
String[] arrayStr =new String[]{"a","b","c","d","e","f","g","h","i"};
System.out.println("arrayStr.length-->"+arrayStr.length);
int ladyQ = arrayStr.length << 4;
System.out.println("length << 4-->"+ladyQ);
}
/**
* 根据给定输入时间得到本周起始时间和本周结束时间
* key1:calFirstDayInThisWeek
* key2:calLastDayInThisWeek
*/
public static Map<String,Date> getThisWeekStartDayAndEndDay(int year,int month,int day){
Calendar calendar = new GregorianCalendar(year, month-1, day);
//Calendar calendar = Calendar.getInstance();
//Calendar cal = new GregorianCalendar(2005, Calendar.JANUARY, 30);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;
Calendar calFirstDayInThisWeek = (Calendar)calendar.clone();
//例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点: add(Calendar.DAY_OF_MONTH, -5)。
calFirstDayInThisWeek.add(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_WEEK)-dayOfWeek);
Calendar calLastDayInThisWeek = (Calendar)calendar.clone();
calLastDayInThisWeek.add(Calendar.DATE,calendar.getActualMaximum(Calendar.DAY_OF_WEEK)-dayOfWeek);
System.out.println("今天是本周的第"+dayOfWeek+"天"+"--本周星期一具日期为:"+calFirstDayInThisWeek.getTime()+"-->"+calLastDayInThisWeek.getTime());
Map map = new HashMap();
map.put("calFirstDayInThisWeek", calFirstDayInThisWeek.getTime());
map.put("calLastDayInThisWeek", calLastDayInThisWeek.getTime());
return map;
}
/**
* key_1:calFirstDayInThisMonth
* key_2:calLastDayInThisMonth
* @param year
* @param month
* @param day
* @return
*/
public static Map<String,Date> getThisMonthStartDayAndEndDay(int year,int month,int day){
//Calendar calendar = Calendar.getInstance();
Calendar calendar = new GregorianCalendar(year, month-1, day);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
Calendar calFirstDayInThisMonth = (Calendar)calendar.clone();
//例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点: add(Calendar.DAY_OF_MONTH, -5)。
calFirstDayInThisMonth.add(Calendar.DATE, calendar.getActualMinimum(Calendar.DAY_OF_MONTH)-dayOfMonth);
Calendar calLastDayInThisMonth = (Calendar)calendar.clone();
calLastDayInThisMonth.add(Calendar.DATE, calendar.getMaximum(Calendar.DAY_OF_MONTH)-dayOfMonth);
Map<String,Date> map = new HashMap<String,Date>();
map.put("calFirstDayInThisMonth", calFirstDayInThisMonth.getTime());
map.put("calLastDayInThisMonth", calLastDayInThisMonth.getTime());
return map;
}
/**
* 分割 dateSstr 喂 getThisWeekStartDayAndEndDay
* @param dateStr
* @return
*/
public static Map<String,String> splitDateStr(String dateStr){
//07/11/2011
String tempDateStr = "07/11/2011";
String[] strArr = dateStr.split("/");
String month = strArr[0];
String day = strArr[1];
String year = strArr[2];
if(strArr.length != 3){
return null;
}
Map map = new HashMap();
map.put("day", day);
map.put("month", month);
map.put("year", year);
return map;
}
public static String dateToString(Date date,String ruler){
//String ruler_1 = "yyyy-MM-dd";
DateFormat df = new SimpleDateFormat(ruler);
String date_Str = df.format(date);
return date_Str;
}
}
分享到:
相关推荐
java获取日期 本周本年 本月 上一年java获取日期 本周本年 本月 上一年java获取日期 本周本年 本月 上一年java获取日期 本周本年 本月 上一年java获取日期 本周本年 本月 上一年java获取日期 本周本年 本月 上一年...
- **计算一年的第一天是星期几**:根据给定年份计算该年1月1日是一周中的哪一天(0表示星期日)。 ```java public static int firstDay(int y) { long n = y * 365; for (int i = 1; i ; i++) { if ...
在Java编程语言中,我们经常需要处理日期和时间,包括获取当前日期、格式化日期以及计算过去或未来的日期。在给定的文档中,有两个主要的知识点:获取当前日期并按照“xxxx年xx月xx日”的格式输出,以及获取最近一年...
非常全面,除了常规时间转换,还有 获取当月最大日期、当前的日期那一年的第几周、当前日期是那一年的第几天、当年、月第一天、最后一天、获得起始时间和结束时间范围内,所有的月初和月末等等
商务风JAVA工程师一年经验春招简历.docx
获取某一年周的总数 ```java public static int getMaxWeekNumOfYear(int year) ``` 此方法用于计算指定年份的总周数。它通过设置`GregorianCalendar`的时间为该年份的最后一天(12月31日23:59:59),然后调用`...
创建一个`GregorianCalendar`实例时,你可以指定年、月、日、小时、分钟等参数,如果不提供,它会使用当前系统时间作为默认值。 ```java GregorianCalendar calendar = new GregorianCalendar(); ``` 要获取一年内...
Java基础编程:判断这一天是这一年的第几天
干货!资深java工程师面试要点大全+一年整理.zip
同时,可以使用`isBefore()`和`isAfter()`方法比较日期,找出当前日期所在节气区间。 6. **线程安全与效率**:如果这个功能需要在多线程环境中使用,那么要考虑线程安全问题。通常,获取当前节气的计算不需要同步,...
在Android开发中,有时我们需要对日期进行比较,例如判断一个日期是否在未来的某段时间范围内,如一年以内。本文将详细讲解如何实现这个功能,并提供一个具体的算法实例。 首先,我们需要理解日期比较的基本方法。...
java操作文件,得到文件名,大小,时间,及修改时间java操作文件,得到文件名,大小,时间,及修改时间java操作文件,得到文件名,大小,时间,及修改时间
Java Swing 是Java GUI(图形用户界面)库的一部分,它提供了丰富的组件来构建桌面应用程序。在Swing中,处理日期和时间选择的控件是至关重要的,因为它们为用户提供了一个直观的方式来选择日期或时间,而无需手动...
6个目标文件,EJB来模拟银行ATM机的流程及操作:获取系统属性,初始化JNDI,取得Home对象的引用,创建EJB对象,并将当前的计数器初始化,调用每一个EJB对象的count()方法,保证Bean正常被激活和钝化,EJB对象是用...
网上很多资源都没有输出结果是(相差几年几个月几天的代码)希望对大家有帮助 有不足地方 望大家多多指点
要获取某个日期所在周的序号,或者设置某一年的第一周,可以使用以下方法: - **获取指定日期所在的周序号** ```java Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2006); cal.set(Calendar....
有一年经验的模板,也有多年经验的模板,共计java45套简历模板,应对各个时期的java程序员!!!! 有一年经验的模板,也有多年经验的模板,共计java45套简历模板,应对各个时期的java程序员!!!! 有一年经验的...
在本项目中,"日历备忘录Java源码" 提供了一套完整的日历应用程序的源代码,主要由四个核心文件组成:NotePad.java、CalendarPad.java、Month.java 和 Year.java。这些文件分别代表了备忘录、日历、月份和年份的功能...
这里,`getTime()`方法返回一个`java.util.Date`对象,表示日历的当前时间。为了适应Java 8引入的`java.time`包,我们可以将`Date`转换为`Instant`,然后转换为`LocalDate`,这样可以获得更友好的日期操作。 此外,...