java


public static Object getCloneObject(Object bean) {
Object cloneBean = null;
try {
ByteArrayOutputStream byout = new ByteArrayOutputStream();
ObjectOutputStream obj = new ObjectOutputStream(byout);
obj.writeObject(bean);
ByteArrayInputStream byin = new ByteArrayInputStream(byout
.toByteArray());
ObjectInputStream ins = new ObjectInputStream(byin);
cloneBean = (Object) ins.readObject();

} catch (Exception ex) {
ex.printStackTrace();
}
return cloneBean;

}


DateUtil,StringUtil,UserUtil,UserSession,AggregatedsarchDTO,CacheManage,WebCacheMangeUtil,Constants,DateUtil


public static String getDateFormat( java.util.Date date, String pattern )

{

if(date==null)
return null;
synchronized ( sdf )
{
String str = null;
sdf.applyPattern( pattern );
str = sdf.format( date );
return str;
}
}


public static int getInt(String strInt, int defval) {
try {
return Integer.parseInt(strInt);
} catch (Exception ex) {
return defval;
}
}


AggregatedSearchDTO dto = new AggregatedSearchDTO();
dto.setStatementId(sqlId);
dto.setMulti(true);
dto.setCurrentObject(listSearchDTO);
List list = (List) dispatch.dispatchRequest(dto,"AggregatedSearchAction");



package com.tpaic.ec.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.springframework.util.StringUtils;

import com.tpaic.tpfa.app.biz.service.BusinessServiceException;

/**
* 字符串工具
*
* @author xiemingmei
* @date 2008-10-2
*/
public class StringUtil {
public final static String COMMA = ",";
private final static String EQUAL = "=";
private final static String NULL = "null";
public final static String NULL_CHN = "空";
public final static String SEMICOLON = ";";
public static String msString(String value) {
if (value == null) {
return "";
}

return value;
}

/**
* 是否为空
*
* @param str
* @return
*/
public static boolean isEmptyString(String str) {
if (str == null) {
return true;
}

if (str.equals("")) {
return true;
}
return false;
}

/**
* 获取指定长度字符串,如长度小于指定长度,会根据isPre变量在前或在后补充指定字符;如大于指定长度会根据isPre或前或后截取字符串
*
* @param String
* str 待处理字符串
* @param char
* c 长度不够时填充的字符
* @param int
* length 返回字符串长度
* @param boolean
* isPre 为true,在前补0,或者从最后一位开始截取字符串; 为false,在后补0,或者从第一位开始截取字符串
* @return 处理后定长字符串
* @author zhanglin
*/
public static String getTheLengthString(String str, int length, char c, boolean isPre) {
int strLength = 0;
if (str != null) {
strLength = str.length();
} else {
str = "";
}
if (strLength > length) {
if (isPre) {
str = str.substring(strLength - length, strLength);
} else {
str = str.substring(0, length);
}
} else if (strLength == length) {
return str;
} else {
StringBuffer sb = new StringBuffer();
length = length - strLength;
for (int i = 0; i < length; i++) {
sb.append(c);
}
if (isPre) {
sb.append(str);
} else {
sb.insert(0, str);
}
str = sb.toString();
}
return str;
}

/**
* 获取指定长度字符串,源字符串或前补0,或从最后一位截取,舍去高位
*
* @param str
* 待处理字符串
* @param length
* 输出字符串长度
* @return 指定长度字符串
*/
public static String getPreLengthString(String str, int length) {
return getTheLengthString(str, length, '0', true);
}

/**
* 获取指定长度字符串,源字符串或后补0,或从第一位截取,舍去低位
*
* @param str
* 待处理字符串
* @param length
* 输出字符串长度
* @return 指定长度字符串
*/
public static String getPostLengthString(String str, int length) {
return getTheLengthString(str, length, '0', false);
}

/**
* added by ex_xiemingmei 转换空格、回车换行符、< > 等字符以适合HTML显示
*
* @param inStr
* String
* @return String
*/
public static String toHTMLString2(String inStr) {
if (inStr == null)
return null;
inStr = inStr.replaceAll(" ", " ");
inStr = inStr.replaceAll("<", "<");
inStr = inStr.replaceAll(">", ">");
inStr = inStr.replaceAll("\r\n", "<br>");
inStr = inStr.replaceAll("\n", "<br>");
return inStr;
}

/**
* 将键值对字符串(形如{key=value,key=value})转换为Map
* @param kvs
* @return
*/
public static Map kvString2Map(String kvs){
if(StringUtils.hasText(kvs)){
kvs = kvs.substring(1, kvs.length() - 1);
String[] ss = kvs.split(COMMA);

Map maps = new HashMap();
for (int i = 0; i < ss.length; i++) {
int b = ss[i].indexOf(EQUAL);
String key = ss[i].substring(0, b);
String value = ss[i].substring(b + 1);
if (value.equals(NULL)) {
value = null;
}
maps.put(key.trim(), value);
}
return maps;
}
return null;
}

public static String trim(String val){
if(val == null) return null;
return val.trim();
}

/**
* @param receiveData
* @throws BusinessServiceException
*/
public static Map populatedToMap(String receiveData) throws BusinessServiceException {
receiveData = receiveData.substring(1, receiveData.length() - 1);
String[] receiveDataArr = receiveData.split(COMMA);

Map maps = new HashMap();
for (int i = 0; i < receiveDataArr.length; i++) {
int b = receiveDataArr[i].indexOf(EQUAL);
String key = receiveDataArr[i].substring(0, b);
String value = receiveDataArr[i].substring(b + 1);
if (value.equals(NULL)) {
value = null;
}
maps.put(key.trim(), value);
}

return maps;
}
/**
* 过滤字符串中所有的空格、换行、制表符等空白字符
* @param in
* @return
*/
public static String fileWhitespace(String in){
return in!=null?Pattern.compile("\\s*").matcher(in).replaceAll(""):null;
}

public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}

}





/****************************************************************************
Module: 日期转换处理
Version: 1.0
*****************************************************************************/

package com.tpaic.ec.util;

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

/**
* 日期转换工具
*/
public class DateUtil {

/**
* 日期(yyyy-MM-dd HH:mm:ss)转换成Date
*/
public static Date stringToDate(String value) {
if (value == null || value.trim().equals(""))
return null;
try {
Calendar calendar = Calendar.getInstance();
value = value.trim();
// 设置yyyymmmdd
String valueDate;
String valueTime;
if(value.indexOf(" ")>0){
valueDate = value.substring(0,value.indexOf(" "));
valueTime = value.substring(value.indexOf(" ")+1);
}
else{
valueDate = value;
valueTime = "00:00:00";
}
String[] d = valueDate.split("-");
//calendar.set(Integer.parseInt(value.substring(0, 4)), Integer.parseInt(value.substring(5, 7)) - 1, Integer.parseInt(value.substring(8, 10)));
calendar.set(Integer.parseInt(d[0]),Integer.parseInt(d[1])-1,Integer.parseInt(d[2]));
// 设置HH:mm:ss
try {
String[] t = valueTime.split(":");
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(t[0]));
calendar.set(Calendar.MINUTE, Integer.parseInt(t[1]));
calendar.set(Calendar.SECOND, Integer.parseInt(t[2]));
// if (value.length() > 10) {
// calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(value.substring(11, 13)));
// // Util.instance().writeDebug(null,"Calendar.HOUR_OF_DAY"+value.substring(9,
// // 11));
// if (value.length() > 13) {
// calendar.set(Calendar.MINUTE, Integer.parseInt(value.substring(14, 16)));
// // Util.instance().writeDebug(null,"Calendar.MINUTE"+value.substring(12,
// // 14));
// if (value.length() > 16)
// calendar.set(Calendar.SECOND, Integer.parseInt(value.substring(17, 19)));
// // Util.instance().writeDebug(null,"Calendar.SECOND"+value.substring(15,
// // 17));
// }
// }
} catch (Exception ex) {
//CommonFunctions.printLine(ex.toString());
// Util.instance().writeDebug(null,ex.toString());
}
return calendar.getTime();
} catch (Exception e) {
return null;
}
}

/**
* 时间(hh:mm:ss)转换成Date
*/
public static Date timeToDate(String value) {
if (value == null || value.trim().equals(""))
return null;
try {
value = value.trim();
Calendar cal = Calendar.getInstance();
int hour = Integer.parseInt(value.substring(0, 2));
int minute = Integer.parseInt(value.substring(3, 5));
int second = Integer.parseInt(value.substring(6, 8));
cal.set(cal.HOUR_OF_DAY, hour);
cal.set(cal.MINUTE, minute);
cal.set(cal.SECOND, second);

return cal.getTime();
} catch (Exception e) {
return null;
}
}

/**
* 格式化日期时间
*/

public static String datetimeToString(Date value, String dateFormat) {
if (value == null)
return "";
SimpleDateFormat vFormat = new SimpleDateFormat(dateFormat);
return vFormat.format(value);
}

/**
* 格式化日期
*/
public static String dateToString(Date value) {
return dateToString(value, "-");
}

public static String dateToString(Date value, String pattern) {
if (value == null)
return "";
SimpleDateFormat vFormat = new SimpleDateFormat("yyyy" + pattern + "MM" + pattern + "dd");
return vFormat.format(value);
}

/**
* 格式化时间
*/
public static String timeToString(Date value) {
if (value == null)
return "";
SimpleDateFormat vFormat = new SimpleDateFormat("hh:mm:ss");
return vFormat.format(value);
}

/**
* 格式Date类型为String (yyyy)
*/
public static String dateToStringYear(Date value){
if (value == null)
return "";
SimpleDateFormat vFormat = new SimpleDateFormat("yyyy");
return vFormat.format(value);
}

/**
* 判断输入年月日参数是否为日期
*
* @param year
* @param month
* @param day
* @return
*/
public static boolean isDate(String year, String month, String day) {
try {
if (year == null || month == null || day == null)
return false;
if (month.length() == 1)
month = "0" + month;
if (day.length() == 1)
day = "0" + day;
String dateString = year + "/" + month + "/" + day;
Date date = DateFormat.getDateInstance().parse(dateString);
if (dateToString(date).equals(dateString))
return true;
return false;
} catch (Exception e) {
return false;
}
}

/**
* added by seny 2003-05-12 取得两个日期的相隔天数
*
* @param start_time
* 开始日期(YYYY-MM-DD HH24:MI类型)
* @param end_time
* 结束日期(YYYY-MM-DD HH24:MI类型)
* @return 相隔天数
*/
public static long BetweenDays(String start_time, String end_time) {
try {
if (start_time == null || end_time == null)
return -1;
else {
java.sql.Timestamp start = java.sql.Timestamp.valueOf(start_time + ":00.000000000");
java.sql.Timestamp end = java.sql.Timestamp.valueOf(end_time + ":00.000000000");
long between_days = (end.getTime() - start.getTime()) / (1000 * 3600 * 24);
if (between_days < 0)
return -1;
else
return between_days;
}
} catch (Exception e) {
return -1;
}
}

/**
* added by seny 2003-05-12 取得两个日期的相隔天数
*
* @param start_time
* 开始日期(YYYY-MM-DD类型)
* @param end_time
* 结束日期(YYYY-MM-DD类型)
* @return 相隔天数
*/

public static long BetweenDaysByDate(Date start_time, Date end_time) {
try {
if (start_time == null || end_time == null)
return -1;
else {
java.sql.Timestamp start = java.sql.Timestamp.valueOf(dateToString(start_time) + " 00:00:00.000000000");
java.sql.Timestamp end = java.sql.Timestamp.valueOf(dateToString(end_time) + " 00:00:00.000000000");
long between_days = (end.getTime() - start.getTime()) / (1000 * 3600 * 24);
if (between_days < 0)
return -1;
else
return between_days;
}
} catch (Exception e) {
return -1;
}
}

/**
* added by lizh 2003-05-16 取得两个日期的相隔小时数
*
* @param start_time
* 开始日期(YYYY-MM-DD HH24:MI类型)
* @param end_time
* 结束日期(YYYY-MM-DD HH24:MI类型)
* @return 相隔小时数
*/
public static double betweenHours(String start_time, String end_time) {
try {
if (start_time == null || end_time == null)
return 0;
else {
java.sql.Timestamp start = java.sql.Timestamp.valueOf(start_time + ":00.000000000");
java.sql.Timestamp end = java.sql.Timestamp.valueOf(end_time + ":00.000000000");
double between_hours = (double) (end.getTime() - start.getTime()) / (1000 * 3600d);
if (between_hours < 0)
return 0;
else
return between_hours;
}
} catch (Exception e) {
return -1;
}
}

/**
* added by lizh 2003-05-16 取得两个日期的相隔小时数
*
* @param start_time
* 开始日期(YYYY-MM-DD HH24:MI类型)
* @param end_time
* 结束日期(Date类型)
* @return 相隔小时数
*/
public static double betweenHours(String start_time, Date end_time) {
try {
if (start_time == null || end_time == null)
return 0;
else {
java.sql.Timestamp start = java.sql.Timestamp.valueOf(start_time + ":00.000000000");
double between_hours = (double) (end_time.getTime() - start.getTime()) / (1000 * 3600d);
if (between_hours < 0)
return 0;
else
return between_hours;
}
} catch (Exception e) {
return 0;
}
}

/**
* added by huangwc 2006-05-02 判断betweendate是否在date1和date2之间
*
* @param date1
* @param date2
* @param betweendate
* @return
*/
public static boolean betweenDate(String date1, String date2, String betweendate) {
try {
boolean bt = false;
Date date1_date = stringToDate(date1);
Date date2_date = stringToDate(date2);
Date between_date = stringToDate(betweendate);
if (between_date.after(date1_date) && date2_date.after(between_date))
bt = true;
return bt;
} catch (Exception e) {
return false;
}
}

/**
* 获取系统当前的日期,格式化 yyyy-MM-dd
*
* @return
*/
public static String getDefaultFormatedDate() {
Date date = new Date();
SimpleDateFormat vFormat = new SimpleDateFormat("yyyy-MM-dd");
return vFormat.format(date);
}

/**将Date格式的日期,增加1天
* @param addDay
* @return
*/
public static Date addDay(String strDate, int addDay) throws Exception {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = df.parse(strDate);
Calendar t = Calendar.getInstance();
t.setTime(date);
t.add(Calendar.DATE, addDay);
return t.getTime();

}

/**
* 计算两个日期之间相差的整月数,日期是××××年××月××日00时00分00秒
* 如果不足一个月按照一个月计算
*/
public static int betweenMonths(String begin, String end) {
SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
try {
Date begined = dataFormat.parse(getCharacterReplace(begin));
Date ended = dataFormat.parse(getCharacterReplace(end));

Calendar begingc = Calendar.getInstance();
begingc.setTime(begined);
int beginYear = begingc.get(Calendar.YEAR);
int beginMonth = begingc.get(Calendar.MONTH);
int beginDay = begingc.get(Calendar.DAY_OF_MONTH);

Calendar endgc = Calendar.getInstance();
endgc.setTime(ended);
int endYear =endgc.get(Calendar.YEAR);
int endMonth = endgc.get(Calendar.MONTH);
int endDay = endgc.get(Calendar.DAY_OF_MONTH);

int between = (endYear-beginYear)*12 + (endMonth-beginMonth);

if(endDay >beginDay)
between = between +1;
else if(endDay < beginDay)
between = between -1;
return between;

} catch (ParseException e) {
throw new RuntimeException(e);
}
}

/**
* 计算两个Date日期之间相差的整月数
* 如果不足一个月按照一个月计算
*/
public static int betweenMonthDate(Date begin, Date end) {
Calendar begingc = Calendar.getInstance();
begingc.setTime(begin);
int beginYear = begingc.get(Calendar.YEAR);
int beginMonth = begingc.get(Calendar.MONTH);
int beginDay = begingc.get(Calendar.DAY_OF_MONTH);

Calendar endgc = Calendar.getInstance();
endgc.setTime(end);
int endYear =endgc.get(Calendar.YEAR);
int endMonth = endgc.get(Calendar.MONTH);
int endDay = endgc.get(Calendar.DAY_OF_MONTH);

int between = (endYear-beginYear)*12 + (endMonth-beginMonth);

if(endDay >beginDay)
between = between +1;
else if(endDay < beginDay)
between = between -1;
return between;
}

/**
* 将yyyy-mmm-dd格式转化为 ××××年××月××日00时00分00秒
*/
public static String getCharacterReplace(String data){
StringBuffer buffer = new StringBuffer();
buffer.append(data.substring(0, 4)).append("年");
buffer.append(data.substring(5, 7)).append("月");
buffer.append(data.substring(8, 10)).append("日");
buffer.append("00时00分00秒");
return buffer.toString();
}

/**将Date格式的日期,增,减1天
* @param addDay
* @return
*/
public static Date addFirstDay(Date strDate, int addDay){
Calendar t = Calendar.getInstance();
t.setTime(strDate);
t.add(Calendar.DATE, addDay);
return t.getTime();
}

/**将字符yyyy-mm-dd格式,增加1天
* @param addDay
* @return
*/
public static String addForCharacterDay(String strDate,int addDay){
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(strDate);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, addDay);
return format.format(cal.getTime());
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值