java日期类封装及My97DatePicker日期插件使用总结

/**
 * @author xiaoqun.yi 日期工具类
 */
public class DateTimeHelper {
	
	/**
	 * 获取当前年
	 * @return yyyy
	 */
	public static String getCurrentYear() {
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); // 获取东八区时间
		String year = String.valueOf(c.get(Calendar.YEAR)); // 获取年
		return year;
	}

	/**
	 * 获取当前月
	 * 格式:mm
	 */
	public static String getCurrentMonth() {
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); // 获取东八区时间
		String month = String.valueOf(c.get(Calendar.MONTH) + 1).length() == 2 ? String
				.valueOf(c.get(Calendar.MONTH) + 1) : "0"
				+ String.valueOf(c.get(Calendar.MONTH) + 1);
		return month;
	}

	/**
	 * 获取当前天
	 * @return dd 不足10前面补0
	 */
	public static String getCurrentDay() {
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); // 获取东八区时间
		String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH)).length() == 2 ? String
				.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1) : "0"
				+ String.valueOf(c.get(Calendar.DAY_OF_MONTH));

		return day;
	}

	/**
	 * 获取当前小时 
	 * @return mm 不足10前面补0
	 */
	public static String getCurrentHour() {
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); // 获取东八区时间
		String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY)).length() == 2 ? String
				.valueOf(c.get(Calendar.HOUR_OF_DAY)) : "0"
				+ String.valueOf(c.get(Calendar.HOUR_OF_DAY));
		return hour;
	}
	/**
	 * 获取当前分钟
	 * @return mm 不足10前面补0
	 */
	public static String getCurrentMinute() {
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); // 获取东八区时间
		String min = String.valueOf(c.get(Calendar.MINUTE)).length() == 2 ? String
				.valueOf(c.get(Calendar.MINUTE)) : "0"
				+ String.valueOf(c.get(Calendar.MINUTE));
		return min;
	}
	/**
	 * 获取当前日期
	 * @return yyyy-MM-dd
	 */
	public static String getCurrentDate(){
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); 
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Timestamp(calendar.getTime().getTime()));
	}
	/**
	 * 
	 * @param month 前几个月
	 * @param day   前几天
	 * @return yyyyMMdd
	 */
	public static String getBeforeDate(int month,int day){
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));   
		calendar.add(Calendar.MONTH, month);    //得到前一
		calendar.add(Calendar.DATE, day);    //得到前一
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Timestamp(calendar.getTime().getTime()));
	}
	
	/**
	 * 格式显示日期
	 * @param str yyyyMMdd
	 * @return yyyy年MM月dd日
	 */
	public static String formtDate(String str){
		return str.substring(0,4)+"年"+str.substring(4,6)+"月"+str.substring(6,8)+"日";
	}

	/**
	 * 获取系统当期时间,
	 * @return Timestamp
	 */
	public static Timestamp getNowTimestamp()
	{
		return new Timestamp(System.currentTimeMillis());
	}
	
	/**
	 * 系统当期时间
	 * @return  Date
	 */
	public static Date getNowDate()
	{
		return new Date(System.currentTimeMillis());
	}
	
	/**
	 * 获取当期时间字符串
	 * @param pattern 格式化字符串
	 * @return
	 */
	public static String getNowDateStr(String pattern)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date(System.currentTimeMillis()));
	}
	
	/**
	 * 时间转化 字符串转换成时间
	 * @param dateStr 字符串
	 * @param pattern 格式
	 * @return Timestamp
	 */
	public static Timestamp convertToTimestamp(String dateStr,String pattern)
	{
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			Date date = sdf.parse(dateStr);
			return new Timestamp(date.getTime());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 
	 * @param timeMillis 时间转换字符串
	 * @param pattern
	 * @return
	 */
	public static String converToStr(long timeMillis, String pattern)
	{
		Timestamp t = new Timestamp(timeMillis);
		return convert(t, pattern);
	}
	
	/**
	 * 时间转化 字符串转换成时间
	 * @param dateStr
	 * @param pattern
	 * @return Date
	 */
	public static Date convertToDate(String dateStr, String pattern)
	{
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat(pattern);
			return sdf.parse(dateStr);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 时间转化 时间字符串
	 * @param timestamp
	 * @param pattern
	 * @return String
	 */
	public static String convert(Timestamp timestamp, String pattern)
	{
		if (timestamp == null)
		{
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(new Date(timestamp.getTime()));
	}
	
	/**
	 * 时间转化 根据格式转换时间为字符串
	 * @param date
	 * @param pattern
	 * @return String 
	 */
	public static String conver(Date date, String pattern)
	{
		if (date == null)
		{
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	/**
	 * 时间增加 天数
	 * @param currentmi
	 * @param day
	 * @return Date
	 */
	public static Date addDateTime(long currentmi, int day)
	{
		Calendar cl = Calendar.getInstance();
		cl.setTimeInMillis(currentmi);
		cl.add(Calendar.DATE, day);
		return new Date(cl.getTimeInMillis());
	}
	
	/**
	 * 给时间增加一定天数
	 * @param currentmi 
	 * @param day 增加的天数
	 * @param pattern
	 * @returnn String
	 */
	public static String addDateTimeToStr(long currentmi, int day,String pattern)
	{
		Calendar cl = Calendar.getInstance();
		cl.setTimeInMillis(currentmi);
		cl.add(Calendar.DATE, day);
		long result = cl.getTimeInMillis();
		return conver(new Date(result), pattern);
	}
	
	/**
	 * 获取当前月的第一天
	 * @return Timestamp
	 */
	public static Timestamp getFirstDayOfMonth()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
		return new Timestamp(cal.getTime().getTime());
	}
	/**
	 * 获取当前月第一天
	 * @return  yyyyMMdd
	 */
	public static String getCurrentMonthFirstDay1(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return 	sdf.format(getFirstDayOfMonth());
	}
	/**
	 * 获取当前月第一天
	 * @return  yyyy年MM月dd日
	 */
	public static String getCurrentMonthFirstDay2(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM年dd日");
		return 	sdf.format(getFirstDayOfMonth());
	}
	
	/**
	 * 获取当月的最后一天
	 * @return  Timestamp 
	 */
	public static Timestamp getLastDayOfMonth()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 1);  
		cal.roll(Calendar.DATE, -1);  
		return new Timestamp(cal.getTime().getTime());
	}
	
	/**
	 * 获取当前月最后一天
	 * @return  yyyyMMdd
	 */
	public static String getCurrentMontLastDay1(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, 1);  
		cal.roll(Calendar.DATE, -1);  
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return 	sdf.format(cal.getTime());
	}

	
	/** 获取当月的最后一天
	 * 返回yyyy年mm月dd日
	 * @return yyyy年MM月dd日
	 */
	public static String getCurrentMontLastDay2(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		return 	sdf.format(getLastDayOfMonth());
	}
	
	/**
	 * 得到今天的前一天
	 * @return yyyyMMdd
	 */
	public static String getOneDayfore(){
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));   
		calendar.add(Calendar.DATE, -1);    //得到前一
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(new Timestamp(calendar.getTime().getTime()));
		
	}
	
	
}

定义公共引入的文件:include.inc

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入日期工具类 -->
<%@ page language="java" import="com.chinacreator.code.utils.DateTimeHelper" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
	//上下文路径
	String contextPath=request.getContextPath();
	//主题路径
	String themePath="/template/themes/default";
	String filePath="jsp/fileupload/";
	String year=DateTimeHelper.getCurrentYear();
	String month=DateTimeHelper.getCurrentMonth();
	String day=DateTimeHelper.getCurrentDay();
	String hour=DateTimeHelper.getCurrentHour();
	String minute=DateTimeHelper.getCurrentMinute();
	String currentDay=DateTimeHelper.getCurrentDate();//获取系统当前日期格式yyyy-mm-dd
	String beforeOneDay=DateTimeHelper.getBeforeDate(0,-1);//获取前一天日期格式yyyy-mm-dd
	String nextOneDay=DateTimeHelper.getBeforeDate(0,1);//获取今天的后一天的日期yyyy-mm-dd
	pageContext.setAttribute("themePath",themePath);
	pageContext.setAttribute("contextPath",contextPath);
	pageContext.setAttribute("filePath",filePath);
	pageContext.setAttribute("year",year);
	pageContext.setAttribute("month",month);
	pageContext.setAttribute("day",day);
	pageContext.setAttribute("hour",hour);
	pageContext.setAttribute("minute",minute);
	pageContext.setAttribute("hour",hour);
	pageContext.setAttribute("minute",minute);
	pageContext.setAttribute("currentDay",currentDay);
	pageContext.setAttribute("beforeOneDay",beforeOneDay);
	pageContext.setAttribute("nextOneDay",nextOneDay);
	
	
%>
<!-- jquery库 -->
<script type="text/javascript" src="${contextPath}${themePath}/js/jquery-1.4.2.js"></script>
<!-- 分页js -->
<script type="text/javascript" src="${contextPath}${themePath}/js/paging.js"></script>
<!-- 公共封装js -->
<script type="text/javascript" src="${contextPath}${themePath}/js/common.js"></script>
<!-- 验证身份证 -->
<script type="text/javascript" src="${contextPath}${themePath}/js/checkidcard.js"></script>
<!-- 主样式 -->
<link rel="stylesheet" href="${contextPath}${themePath}/css/layout.css" type="text/css"></link>
<!-- 日期插件 -->
<script type="text/javascript" src="${contextPath}/plugs/My97DatePicker/WdatePicker.js"></script>
<!--图表插件 非常好看的fuctionchart flash动画图表,做统计分析 -->
<script type="text/javascript" src="${contextPath}/plugs/FusionCharts/JSClass/FusionCharts.js"></script>
<!-- ztree插件 -->
<script type="text/javascript" src="${contextPath}/plugs/ztree/js/jquery.ztree.all-3.5.min.js"></script>
<script>
	var themePath="<%=themePath%>";
	var contextPath="<%=contextPath%>";
	var filePath="<%=filePath%>";
	var year="<%=year%>";
	var month="<%=month%>";
	var day="<%=day%>";
	var hour="<%=hour%>";
	var minute="<%=minute%>";
	var currentDay="<%=currentDay%>";
	var beforeOneDay="<%=beforeOneDay%>";
	var nextOneDay="<%=nextOneDay%>";
</script>

Jsp页面引入:
<%@ include file="/template/include.inc"%>

My97DatePicker控件免费下载

http://download.csdn.net/detail/u012750578/6597619
包含这个文件后就获取设置属性值,引入的样式,js文件,定义的全局的js变量了

公共查询条件Bean:

在java类中
bean继承BaseQuery

/**
 * 
 *@author xiaoqun.yi
 */
public class BaseQuery implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2128648445206020540L;
	// 查询条件用
	private String startDate;
	private String endDate;
	private String keywords;
	private String dept_name;
	private String emp_name;
	

	
	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getDept_name() {
		return dept_name;
	}

	public void setDept_name(String dept_name) {
		this.dept_name = dept_name;
	}

	public String getEmp_name() {
		return emp_name;
	}

	public void setEmp_name(String emp_name) {
		this.emp_name = emp_name;
	}
	
}

时间比较使用字符串yymmdd既方便比较又不需要转换
处罚时间: 最大时间不能大于当前时间,这里使用maxDate:'currentDay'这是前面引入文件包含的currentDay,currentDay当前时间为服务器时间
,第二个日期大于第一个日期,日期显示值为:yyyy-mm-dd 日期实际值为yyyymmdd (日期为服务器时间)

<input type="text" id="date01" class="tdInput_80" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',realDateFmt:'yyyyMMdd',vel:'startDate',maxDate:'#F{$dp.$D(\'date02\')||\'%y-%M-{%d}\'}',readOnly:true})"/> --
<input type="hidden" id="startDate" name="punish.startDate" />
<input type="text"  id="date02" class="tdInput_80" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',realDateFmt:'yyyyMMdd',vel:'endDate',minDate:'#F{$dp.$D(\'date01\')}', maxDate:'currentDay',readOnly:true})"/>
<input type="hidden" id="endDate" name="punish.endDate" />



sql:这是使用的是xml,其实不用xml也一样,条件判断不为空就加上条件
    #if($startDate && !$startDate.equals(""))
        and p.peccancy_time>=#[startDate]
    #end
    #if($endDate && !$endDate.equals(""))
        and p.peccancy_time<=#[endDate]
    #end

选择的日期不能大于当前日期(日期为服务器时间) maxDate:'currentDay'

<input type="text" id="checkdate_add" class="tdInput" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',realDateFmt:'yyyyMMdd', vel:'checkdate',maxDate:'currentDay',readOnly:true})"/>
<input type="hidden" id="checkdate" name="checkreform.checkdate" />


选择的最小日期大于当前日期,后面的日期大于前面的日期,日期格式为:yyyy-mm-dd 值为yyyymmdd (日期为服务器时间)

<input type="text" id="date01" class="tdInput" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',realDateFmt:'yyyyMMdd',vel:'timelimit',minDate:'currentDay',maxDate:'#F{$dp.$D(\'date02\')}',readOnly:true})"/>
<input type="hidden"  id="timelimit" name="checkreform.timelimit" />
<input type="text"  id="date02" class="tdInput"class="tdInput" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',realDateFmt:'yyyyMMdd',vel:'acceptdate',minDate:'#F{$dp.$D(\'date01\')}',readOnly:true})"/>
<input type="hidden"  name="checkreform.acceptdate" id="acceptdate" />

设置默认服务器时间的前一天时间 (日期为服务器时间)

<input type="text"  id="format_chcekdate_add"   value="${beforeOneDay}" class="tdInput" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',realDateFmt:'yyyyMMdd',vel:'chcekdate_add',maxDate:'currentDay',readOnly:true})"/>
<input type="hidden"  id="chcekdate_add"  name="devicecheck.checkdate"  />

设置默认服务器时间的后一天时间 (日期为服务器时间)

<input type="text"  class="tdInput"  value="${nextOneDay}" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',realDateFmt:'yyyyMMdd',vel:'format_dutydate',minDate:'%y-%M-{%d+1}',readOnly:true})"/>
<input type="hidden"   name="leaderDuty.dutydate"  id="format_dutydate"  />

sql显示日期(oracle)日期存数据库用字符串用yyyymmdd是为了方便比较
 substr(r.timelimit,1,4)||'-'||substr(r.timelimit,5,2)||'-'||substr(r.timelimit,7,2) as timelimit,


时间:显示为hh:实际值为:hhmi是为了两个时间段的比较方便 (日期为服务器时间)

<input type="text" id="date03" class="tdInput" οnfοcus="WdatePicker({dateFmt:'HH:mm',readOnly:true})" /			
<input type="hidden" id="starttime_update" name="dutytime.starttime" />

<input type="text" id="date04" class="tdInput" οnfοcus="WdatePicker({dateFmt:'HH:mm',readOnly:true})" />
<input type="hidden" id="endtime_update" name="dutytime.endtime" />

var starttime=$("#date01").val();
starttime=starttime.substring(0,2)+starttime.substring(3,5);
var endtime=$("#date02").val();
endtime=endtime.substring(0,2)+endtime.substring(3,5);
$("#starttime_add").val(starttime);
$("#endtime_add").val(endtime);
sql查询显示(oracle)
substr(t.starttime,1,2)||':'||substr(t.starttime,3,2) as starttime,
substr(t.endtime,1,2)||':'||substr(t.endtime,3,2) as endtime,



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值