oracle字符串连接和时间格式处理(附加Calendar简单使用)

1.<span style="background-color: rgb(248, 248, 248); ">和其他数据库系统类似,</span><a target=_blank href="http://database.51cto.com/art/201010/231973.htm" style="text-indent: 28px; background-color: rgb(248, 248, 248); ">Oracle</a><span style="text-indent: 28px; background-color: rgb(248, 248, 248); ">字符串连接使用“||”进行字符串拼接,其使用方式和MSSQLServer中的加号“+”一样。</span>

2.oracle对对时间格式的数据存贮比较特殊,经常报ora-01861文字与格式字符串不匹配错误,只需在时间类型字段前加to_char进行转换:


3.Calendar在程序开发中使用还是很广泛的,下面只粘贴曾经使用过程序的Calendar应用:

(1)获取本周和上一周:startDate(上一周),endDate(本周)

Calendar cal1 = Calendar.getInstance();
				cal1.setTime(new Date());
				Calendar cal2 = Calendar.getInstance();
				cal2.setTime(new Date());
				
				int weekday1 = cal1.get(Calendar.DAY_OF_WEEK) - 1;
				cal1.add(Calendar.DATE, -weekday1-7*i);
				int weekday2 = cal2.get(Calendar.DAY_OF_WEEK) - 1;
				cal2.add(Calendar.DATE, 7-weekday2-7*i);
				
				startDate=sdf.format(cal1.getTime());
				endDate=sdf.format(cal2.getTime());


(2)

package com.vingsoft.dzjc.chart;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class cc {

	private static SimpleDateFormat bigLongSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");
    private static SimpleDateFormat hourSdf = new SimpleDateFormat("HH");
    private static SimpleDateFormat minutesSdf = new SimpleDateFormat("mm");
    private static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
    private static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat sdfCh = new SimpleDateFormat("yyyy年MM月dd日");
    
	public static void main(String[] args) throws ParseException {
//		 	Calendar c = Calendar.getInstance();
//	        int currentMonth = c.get(Calendar.MONTH) + 1;
//	        if (currentMonth >= 1 && currentMonth <= 3)
//	            c.set(Calendar.MONTH, 0);
//	        else if (currentMonth >= 4 && currentMonth <= 6)
//	            c.set(Calendar.MONTH, 3);
//	        else if (currentMonth >= 7 && currentMonth <= 9)
//	            c.set(Calendar.MONTH, 4);
//	        else if (currentMonth >= 10 && currentMonth <= 12)
//	            c.set(Calendar.MONTH, 9);
//	        c.set(Calendar.DATE, 1);
//	        
//	        System.out.println(shortSdf.format(c.getTime()));
		
		
		
		for(int i=0;i<2;i++){
			Calendar cal = Calendar.getInstance();
			cal.setTime(new Date());
			int weekday = cal.get(Calendar.DAY_OF_WEEK) - 1;
			System.out.println(-weekday-7*i+"    "+i+"    "+weekday);
//			cal.add(Calendar.DATE, -weekday-7*i);
			cal.add(Calendar.DATE, 7 - weekday-7*i);
	         
	        System.out.println(shortSdf.format(cal.getTime()));
	        
		}
		
		
         Calendar c = Calendar.getInstance();
         c.add(Calendar.WEEK_OF_YEAR, -1);
         SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
//         System.out.println("上周的今天为:" + sf.format(c.getTime()));
         
         String date=sdfCh.format(new Date());
         System.out.println("**********  "+date);
         System.out.println("**********  "+date.substring(5,11));

	}

}

(3)

package com.vingsoft.dzjc.chart;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtilCan {
	private static SimpleDateFormat bigLongSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSSS");
    private static SimpleDateFormat hourSdf = new SimpleDateFormat("HH");
    private static SimpleDateFormat minutesSdf = new SimpleDateFormat("mm");
    private static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
    private static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		 /**
	     * 获得本周的第一天,即周日
	     *
	     * @return
	     */
	    public static Date getCurrentWeekDayStartTime() {
	        Calendar c = Calendar.getInstance();
	        try {
	            int weekday = c.get(Calendar.DAY_OF_WEEK) - 1;
	            c.add(Calendar.DATE, -weekday);
	            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return c.getTime();
	    }
	    /**
	     * 获得本周的最后一天,即本周六
	     *
	     * @return
	     */
	    public static Date getCurrentWeekDayEndTime() {
	        Calendar c = Calendar.getInstance();
	        try {
	            int weekday = c.get(Calendar.DAY_OF_WEEK);
	            c.add(Calendar.DATE, 7 - weekday);
	            c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return c.getTime();
	    }
	    /**
	     * 获得本天的开始时间,即2012-01-01 00:00:00
	     *
	     * @return
	     */
	    public static Date getCurrentDayStartTime() {
	        Date now = new Date();
	        try {
	            now = shortSdf.parse(shortSdf.format(now));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    /**
	     * 获得本天的结束时间,即2012-01-01 23:59:59
	     *
	     * @return
	     */
	    public static Date getCurrentDayEndTime() {
	        Date now = new Date();
	        try {
	            now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    /**
	     * 获得本小时的开始时间,即2012-01-01 23:59:59
	     *
	     * @return
	     */
	    public static Date getCurrentHourStartTime() {
	        Date now = new Date();
	        try {
	            now = longHourSdf.parse(longHourSdf.format(now));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    /**
	     * 获得本小时的结束时间,即2012-01-01 23:59:59
	     *
	     * @return
	     */
	    public static Date getCurrentHourEndTime() {
	        Date now = new Date();
	        try {
	            now = longSdf.parse(longHourSdf.format(now) + ":59:59");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    /**
	     * 获得本月的开始时间,即2012-01-01 00:00:00
	     *
	     * @return
	     */
	    public static Date getCurrentMonthStartTime() {
	        Calendar c = Calendar.getInstance();
	        Date now = null;
	        try {
	            c.set(Calendar.DATE, 1);
	            now = shortSdf.parse(shortSdf.format(c.getTime()));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    /**
	     * 当前月的结束时间,即2012-01-31 23:59:59
	     *
	     * @return
	     */
	    public static Date getCurrentMonthEndTime() {
	        Calendar c = Calendar.getInstance();
	        Date now = null;
	        try {
	            c.set(Calendar.DATE, 1);
	            c.add(Calendar.MONTH, 1);
	            c.add(Calendar.DATE, -1);
	            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }

	    /**
	     * 当前年的开始时间,即2012-01-01 00:00:00
	     *
	     * @return
	     */
	    public static Date getCurrentYearStartTime() {
	        Calendar c = Calendar.getInstance();
	        Date now = null;
	        try {
	            c.set(Calendar.MONTH, 0);
	            c.set(Calendar.DATE, 1);
	            now = shortSdf.parse(shortSdf.format(c.getTime()));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    /**
	     * 当前年的结束时间,即2012-12-31 23:59:59
	     *
	     * @return
	     */
	    public static Date getCurrentYearEndTime() {
	        Calendar c = Calendar.getInstance();
	        Date now = null;
	        try {
	            c.set(Calendar.MONTH, 11);
	            c.set(Calendar.DATE, 31);
	            now = shortSdf.parse(shortSdf.format(c.getTime()));
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    /**
	     * 当前季度的开始时间,即2012-01-1 00:00:00
	     *
	     * @return
	     */
	    public static Date getCurrentQuarterStartTime() {
	        Calendar c = Calendar.getInstance();
	        int currentMonth = c.get(Calendar.MONTH) + 1;
	        Date now = null;
	        try {
	            if (currentMonth >= 1 && currentMonth <= 3)
	                c.set(Calendar.MONTH, 0);
	            else if (currentMonth >= 4 && currentMonth <= 6)
	                c.set(Calendar.MONTH, 3);
	            else if (currentMonth >= 7 && currentMonth <= 9)
	                c.set(Calendar.MONTH, 4);
	            else if (currentMonth >= 10 && currentMonth <= 12)
	                c.set(Calendar.MONTH, 9);
	            c.set(Calendar.DATE, 1);
	            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    /**
	     * 当前季度的结束时间,即2012-03-31 23:59:59
	     *
	     * @return
	     */
	    public static Date getCurrentQuarterEndTime() {
	        Calendar c = Calendar.getInstance();
	        int currentMonth = c.get(Calendar.MONTH) + 1;
	        Date now = null;
	        try {
	            if (currentMonth >= 1 && currentMonth <= 3) {
	                c.set(Calendar.MONTH, 2);
	                c.set(Calendar.DATE, 31);
	            } else if (currentMonth >= 4 && currentMonth <= 6) {
	                c.set(Calendar.MONTH, 5);
	                c.set(Calendar.DATE, 30);
	            } else if (currentMonth >= 7 && currentMonth <= 9) {
	                c.set(Calendar.MONTH, 8);
	                c.set(Calendar.DATE, 30);
	            } else if (currentMonth >= 10 && currentMonth <= 12) {
	                c.set(Calendar.MONTH, 11);
	                c.set(Calendar.DATE, 31);
	            }
	            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return now;
	    }
	    public static void main(String args[]) {
	        System.out.println("当前年开始时间:" + longSdf.format(getCurrentYearStartTime()));
	        System.out.println("当前年结束时间:" + longSdf.format(getCurrentYearEndTime()));
	        System.out.println("-------------------");
	        System.out.println("当前季度开始时间:" + longSdf.format(getCurrentQuarterStartTime()));
	        System.out.println("当前季度结束时间:" + longSdf.format(getCurrentQuarterEndTime()));
	        System.out.println("-------------------");
	        System.out.println("当前月开始时间:" + longSdf.format(getCurrentMonthStartTime()));
	        System.out.println("当前月结束时间:" + longSdf.format(getCurrentMonthEndTime()));
	        System.out.println("-------------------");
	        System.out.println("当前周开始时间:" + longSdf.format(getCurrentWeekDayStartTime()));
	        System.out.println("当前周结束时间:" + longSdf.format(getCurrentWeekDayEndTime()));
	        System.out.println("-------------------");
	        System.out.println("当前天开始时间:" + longSdf.format(getCurrentDayStartTime()));
	        System.out.println("当前天结束时间:" + longSdf.format(getCurrentDayEndTime()));
	        System.out.println("-------------------");
	        System.out.println("当前时开始时间:" + longSdf.format(getCurrentHourStartTime()));
	        System.out.println("当前时结束时间:" + longSdf.format(getCurrentHourEndTime()));
	    }

}

(4)
<span style="white-space:pre">	</span>     Calendar c1 = Calendar.getInstance();  //本周开始
		 Calendar c2 = Calendar.getInstance();	//本周结束
		 Calendar c3 = Calendar.getInstance(); 	//上周开始
		 Calendar c4 = Calendar.getInstance();	//上周结束
		 Calendar c5 = Calendar.getInstance();	//本月开始
		 Calendar c6 = Calendar.getInstance();	//本月结束
		 Calendar c7 = Calendar.getInstance();	//任意月开始
		 Calendar c8 = Calendar.getInstance();	//任意月结束
		 Date thisMonthStart = null;
		 Date thisMonthEnd = null;
		 Date anyMonthStart = null;
		 Date anyMonthEnd = null;
		 
		 //获取页面查询选择的开始日期和结束日期
		 try {  
	          int weekday1 = c1.get(Calendar.DAY_OF_WEEK) - 1;  
	          c1.add(Calendar.DATE, -weekday1);  
	          c1.setTime(longSdf.parse(shortSdf.format(c1.getTime()) + " 00:00:00"));  
	          
	          int weekday2 = c2.get(Calendar.DAY_OF_WEEK);  
	          c2.add(Calendar.DATE, 7 - weekday2);  
	          c2.setTime(longSdf.parse(shortSdf.format(c2.getTime()) + " 23:59:59")); 
	          
	          int weekday3 = c3.get(Calendar.DAY_OF_WEEK) - 1;  
	          c3.add(Calendar.DATE, -weekday3-7*1);  
	          c3.setTime(longSdf.parse(shortSdf.format(c3.getTime()) + " 00:00:00"));  
	          
	          int weekday4 = c4.get(Calendar.DAY_OF_WEEK);  
	          c4.add(Calendar.DATE, 7 - weekday4-7*1);  
	          c4.setTime(longSdf.parse(shortSdf.format(c4.getTime()) + " 23:59:59")); 
	          
	          c5.set(Calendar.DATE, 1); 
	          thisMonthStart = shortSdf.parse(shortSdf.format(c5.getTime()));
	          
	          c6.set(Calendar.DATE, 1);  
	          c6.add(Calendar.MONTH, 1);  
	          c6.add(Calendar.DATE, -1); 
	          thisMonthEnd = longSdf.parse(shortSdf.format(c6.getTime()) + " 23:59:59");  
	          
	          if(time != null && time.equals("anymonth")){
	       	   c7.setTime(anymonth_time);
	              c7.set(Calendar.DATE, 1); 
	              anyMonthStart = shortSdf.parse(shortSdf.format(c7.getTime()));
	              
	              c8.setTime(anymonth_time);
	              c8.set(Calendar.DATE, 1);  
	              c8.add(Calendar.MONTH, 1);  
	              c8.add(Calendar.DATE, -1); 
	              anyMonthEnd = longSdf.parse(shortSdf.format(c8.getTime()) + " 23:59:59"); 
	          }
	      } catch (Exception e) {  
	          e.printStackTrace();  
	      } 
      
		if(start_time == null) {
			start_time = DateUtil.start(new Date());
		}
		if(end_time == null) {
			end_time = new Date(DateUtil.day(start_time, 1).getTime() - 1);
		}
		
		if(time != null){
			if(time.equals("today")){
				start_time = DateUtil.start(new Date());
				end_time = new Date(DateUtil.day(start_time, 1).getTime() - 1);
			}else if(time.equals("yesterday")){
				start_time = DateUtil.start(new Date(System.currentTimeMillis()-1000*60*60*24));
				end_time = new Date(DateUtil.day(start_time, 1).getTime() - 1);
			}else if(time.equals("anyday")){
				start_time = anyday_time;
				end_time = new Date(DateUtil.day(start_time, 1).getTime() - 1);
			}else if(time.equals("thisweek")){
				start_time = c1.getTime();
				end_time = c2.getTime();
			}else if(time.equals("lastweek")){
				start_time = c3.getTime();
				end_time = c4.getTime();
			}else if(time.equals("thismonth")){
				start_time = thisMonthStart;
				end_time = thisMonthEnd;
			}else if(time.equals("anymonth")){
				start_time = anyMonthStart;
				end_time = anyMonthEnd;
			}
		}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值