java 常用按 开始日期 和 结束日期 查询处理
package com.crm.mess.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
// @Test
public static String SEDate(String start, String end) throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat formatStr = new SimpleDateFormat("yyyy/MM/dd");
int dayMis = 1000 * 60 * 60 * 24;//
// CreateTime=2015/01/16&EndTime=2015/01/16 23:59:59
// 如果日期为空 查询全部
if ((start == null || "".equals(start))
&& (end == null || "".equals(end))) {
return "";
}
// 如果开始日期有 结束日期没有 则查询 开始日期当天的时间
if ((start != null && !"".equals(start))
&& (end == null || "".equals(end))) {
long endLong = formatStr.parse(start).getTime() + dayMis - 1;
String endStr = format.format(new Date(endLong));
System.out.println(endStr);
return "CreateTime=" + start + "&EndTime=" + endStr;
}
// 如果结束日期有 开始日期没有 则查询 结束日期当天的时间
if ((start == null || "".equals(start))
&& (end != null && !"".equals(end))) {
long endLong = formatStr.parse(end).getTime() + dayMis - 1;
String endStr = format.format(new Date(endLong));
System.out.println(endStr);
return "CreateTime=" + end + "&EndTime=" + endStr;
}
// 正常日期查询
if (start != null && !"".equals(start) && end != null
&& !"".equals(end)) {
long endLong = formatStr.parse(end).getTime() + dayMis - 1;
String endStr = format.format(new Date(endLong));
System.out.println(endStr);
return "CreateTime=" + start + "&EndTime=" + endStr;
}
return "CreateTime=" + start + "&EndTime=" + end;
}
public static void main(String[] args) throws ParseException {
SEDate("", "");
}
}
第二种情况
package com.crm.mess.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
// @Test
public static String SEDate(String start, String end) throws ParseException {
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat formatStr = new SimpleDateFormat("yyyy/MM/dd");
int dayMis = 1000 * 60 * 60 * 24;//
// CreateTime=2015/01/16&EndTime=2015/01/16 23:59:59
// 如果日期为空 查询全部
if ((start == null || "".equals(start))
&& (end == null || "".equals(end))) {
return "";
}
// 如果开始日期有 结束日期没有 则查询 结束日期为当天时间
if ((start != null && !"".equals(start))
&& (end == null || "".equals(end))) {
//取当天日间
//long endLong = formatStr.parse(start).getTime() + dayMis - 1;
long endLong =new Date().getTime();
String endStr = format.format(new Date(endLong));
System.out.println(endStr);
return "CreateTime=" + start + "&EndTime=" + endStr;
}
// 如果结束日期有 开始日期没有 则查询 开始全部
if ((start == null || "".equals(start))
&& (end != null && !"".equals(end))) {
long endLong = formatStr.parse(end).getTime() + dayMis - 1;
String endStr = format.format(new Date(endLong));
start="2015/01/01";
return "CreateTime=" + start + "&EndTime=" + endStr;
}
// 正常日期查询
if (start != null && !"".equals(start) && end != null
&& !"".equals(end)) {
long endLong = formatStr.parse(end).getTime() + dayMis - 1;
String endStr = format.format(new Date(endLong));
System.out.println(endStr);
return "CreateTime=" + start + "&EndTime=" + endStr;
}
return "CreateTime=" + start + "&EndTime=" + end;
}
public static void main(String[] args) throws ParseException {
SEDate("", "");
}
}
第三种情况
package com.crm.audit.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtil {
// @Test
public static Date[] SEDate(Date start, Date end) {
Date[] result = new Date[2];
// 如果日期为空 查询全部
if ((start == null || "".equals(start))
&& (end == null || "".equals(end))) {
result[0] = new Date(1388505660000L);// 2014/01/01
result[1] = new Date(1514736060000L);
return result;
}
// 如果开始日期有 结束日期没有 则查询 结束日期为当天时间
if ((start != null && !"".equals(start))
&& (end == null || "".equals(end))) {
// 取当天日间
long endLong = new Date().getTime();
result[0] = start;
result[1] = new Date(endLong);
return result;
}
// 如果结束日期有 开始日期没有 则查询 开始全部
if ((start == null || "".equals(start))
&& (end != null && !"".equals(end))) {
result[0] = new Date(1388505660000L);
result[1] = end;
return result;
}
// 正常日期查询
if (start != null && !"".equals(start) && end != null
&& !"".equals(end)) {
result[0] = start;
result[1] = end;
return result;
}
return result;
}
public static void main(String[] args) throws ParseException {
// SEDate("", "");
String sd = "2014/01/01";
String sd2 = "2018/01/01";
SimpleDateFormat s = new SimpleDateFormat("yyyy/mm/dd");
Date d1 = s.parse(sd);
System.out.println(d1.getTime());
Date d2 = s.parse(sd2);
System.out.println(d2.getTime());
}
}
Date[] dates=DateUtil.SEDate(proCheckQuery.getSdate(), proCheckQuery.getEdate());
proCheckQuery.setSdate(dates[0]);
proCheckQuery.setEdate(dates[1]);