当前位置:首页 > 开发 > Web前端 > 前端 > 正文

时间 java方法(当前时间所在一周and一年)

发表于: 2011-07-18   作者:coolhappiness_rgb   来源:转载   浏览:
摘要: 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.Simpl
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方法(当前时间所在一周and一年)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号