StringNew.java
资源名称:OA.rar [点击查看]
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:14k
源码类别:
OA系统
开发平台:
Java
- package com.gforce.currency;
- /**
- * <p>Title: 吉力科技办公自动化系统</p>
- * <p>Description: 吉力科技办公自动化系统</p>
- * <p>Copyright: 版权所有 2003 (c) 西安吉力科技发展有限公司 Copyright (c) 2003 GForce Sceince & Technology</p>
- * <p>Company: 西安吉力科技发展有限公司 (GForce Sceince & Technology)</p>
- * @author 马登军
- * @version 1.0
- */
- import java.text.*;
- import java.util.*;
- import java.security.*;
- import javax.crypto.*;
- public class StringNew {
- /**
- * 创建私有构造函数,防止程序创建该对象
- */
- private StringNew() {
- }
- /**
- * 将从request获取的字符串进行解码
- * @param strValue 解码前的原始字符串
- * @return 解码后的字符串
- */
- public static String RequestGetDecode(String strValue) {
- String strReturnValue = "";
- try {
- strReturnValue = new String(strValue.getBytes("iso8859_1"));
- }
- catch (Exception err) {
- SystemOut.ErrOut("从request获取字符串解码时出错!");
- }
- return strReturnValue;
- }
- /**
- * 对向request设置的字符串变量进行编码
- * @param strValue 编码前的原始字符串
- * @return 编码后的字符串
- */
- public static String RequestSetEncode(String strValue) {
- String strReturnValue = strValue;
- return strReturnValue;
- }
- /**
- * 将从session获取的字符串进行解码
- * @param strValue 解码前的原始字符串
- * @return 解码后的字符串
- */
- public static String SessionGetDecode(String strValue) {
- String strReturnValue = "";
- try {
- //strReturnValue = new String(strValue.getBytes("iso8859_1"));
- strReturnValue = strValue;
- }
- catch (Exception err) {
- SystemOut.ErrOut("从session获取字符串解码时出错!");
- }
- return strReturnValue;
- }
- /**
- * 对向session设置的字符串变量进行编码
- * @param strValue 编码前的原始字符串
- * @return 编码后的字符串
- */
- public static String SessionSetEncode(String strValue) {
- String strReturnValue = "";
- try {
- //strReturnValue = new String(strValue.getBytes("iso8859_1"));
- strReturnValue = strValue;
- }
- catch (Exception err) {
- SystemOut.ErrOut("向session设置字符串编码时出错!");
- }
- return strReturnValue;
- }
- /**
- * 将从数据库中获取的字符串进行解码
- * @param strValue 解码前的原始字符串
- * @return 解码后的字符串
- */
- public static String DbGetDecode(String strValue) {
- String strReturnValue = "";
- try {
- strReturnValue = new String(strValue.getBytes("iso8859_1"));
- }
- catch (Exception err) {
- SystemOut.ErrOut("从数据库获取字符串解码时出错!");
- }
- return strReturnValue;
- }
- /**
- * 对向数据库设置的字符串变量进行编码
- * @param strValue 编码前的原始字符串
- * @return 编码后的字符串
- */
- public static String DbSetEncode(String strValue) {
- String strReturnValue = strValue;
- return strReturnValue;
- }
- /**
- * 将字符串日期转换成制定格式的新日期字符串
- * @param strOldDate 旧的字符串日期
- * @param strNewDateFormat 新的日期字符串格式
- * @return 新的日期字符串
- */
- public static String GetDateString(String strOldDate, String strNewDateFormat) {
- try {
- SimpleDateFormat sdfDateFormat = new SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss.S");
- if (strOldDate.length() == 10) {
- strOldDate += " 00:00:00.000";
- }
- else if (strOldDate.length() == 13) {
- strOldDate += ":00:00.000";
- }
- else if (strOldDate.length() == 16) {
- strOldDate += ":00.000";
- }
- else if (strOldDate.length() == 19) {
- strOldDate += ".000";
- }
- Date dateOldDate = sdfDateFormat.parse(strOldDate);
- return GetDateString(dateOldDate, strNewDateFormat);
- }
- catch (Exception err) {
- SystemOut.InfoOut("字符串转换日期格式时失败:" + strOldDate + " to " +
- strNewDateFormat);
- return "";
- }
- }
- /**
- * 将日期转换为制定格式的日期字符串
- * @param dateOldDate 旧的日期
- * @param strNewDateFormat 新的日期字符串格式
- * @return 新的日期字符串
- */
- public static String GetDateString(Date dateOldDate, String strNewDateFormat) {
- try {
- SimpleDateFormat sdDateFormat = new SimpleDateFormat(strNewDateFormat);
- SimpleDateFormat sdfDateFormat1 = new SimpleDateFormat(
- "yyyy-MM-dd HH:mm:ss.S");
- Date dateOldDate1 = sdfDateFormat1.parse("1900-01-01 00:00:00.0");
- String strNewDate = sdfDateFormat1.format(dateOldDate);
- String strOldDate = sdfDateFormat1.format(dateOldDate1);
- if (!strNewDate.equalsIgnoreCase(strOldDate)) {
- return sdDateFormat.format(dateOldDate);
- }
- else {
- return "";
- }
- }
- catch (Exception err) {
- SystemOut.InfoOut("转换日期格式时失败:" + dateOldDate.toString() + " to " +
- strNewDateFormat);
- return "";
- }
- }
- /**
- * 根据记录总数和每页的记录数计算记录页数
- * @param iRecordCount 记录总数
- * @param iPageSize 每页的记录数
- * @return 记录的页数
- */
- public static int getPageCount(int iRecordCount, int iPageSize) {
- try {
- if (iPageSize < 1) {
- iPageSize = 1;
- }
- return (iRecordCount % iPageSize == 0) ? (iRecordCount / iPageSize) :
- ( (iRecordCount / iPageSize) + 1);
- }
- catch (Exception err) {
- SystemOut.ErrOut("获取页面总数时出错,记录条数=" + iRecordCount + ",页面大小=" + iPageSize +
- "");
- return 1;
- }
- }
- /**
- * 根据原始密码生成加密过的密码
- * @param strOldPassword 原始密码
- * @return 经过加密的密码
- */
- public static String getEncodePassword(String strOldPassword) {
- String strNewPassword = "";
- try
- {
- byte[] b = strOldPassword.getBytes();
- String stmp = "";
- for (int n = 0; n < b.length; n++) {
- stmp = Integer.toHexString(b[n] & 0xff);
- if (stmp.length() == 1) {
- strNewPassword = String.valueOf(String.valueOf( (new StringBuffer(
- String.valueOf(
- String.valueOf(strNewPassword)))).append("0").append(stmp)));
- }
- else {
- strNewPassword = String.valueOf(strNewPassword) + String.valueOf(stmp);
- }
- }
- strNewPassword = strNewPassword.toUpperCase();
- b = strNewPassword.getBytes();
- strNewPassword = "";
- for (int n = 0; n < b.length; n++) {
- stmp = Integer.toHexString(b[n] & 0xff);
- if (stmp.length() == 1) {
- strNewPassword = String.valueOf(String.valueOf( (new StringBuffer(
- String.valueOf(
- String.valueOf(strNewPassword)))).append("0").append(stmp)));
- }
- else {
- strNewPassword = String.valueOf(strNewPassword) + String.valueOf(stmp);
- }
- }
- }
- catch(Exception err){}
- return strNewPassword;
- }
- /**
- * 根据加密过的密码生成原始密码
- * @param strOldPassword 加密过的密码
- * @return 原始密码
- */
- public static String getDisencodePassword(String strOldPassword) {
- String stmp = "";
- String strNewPassword = "";
- try
- {
- char c[] = new char[strOldPassword.length() / 2];
- for (int n = 0; n < strOldPassword.length() / 2; n++) {
- stmp = strOldPassword.substring(2 * n, 2 * n + 2);
- c[n] += ( (byte) Integer.parseInt(stmp, 16));
- }
- strNewPassword = String.valueOf(c);
- char cc[] = new char[strNewPassword.length() / 2];
- for (int n = 0; n < strNewPassword.length() / 2; n++) {
- stmp = strNewPassword.substring(2 * n, 2 * n + 2);
- cc[n] += ( (byte) Integer.parseInt(stmp, 16));
- }
- strNewPassword = String.valueOf(cc);
- }
- catch(Exception err){}
- return strNewPassword;
- }
- /**
- * 获取两个IDs字符串的交集
- * @param strIDs1 String IDs1
- * @param strIDs2 String IDs2
- * @return String 两个IDs字符串的交集
- */
- public static String getIntersectionOfIDs(String strIDs1,String strIDs2)
- {
- String strTemp = "";
- if(strIDs1.length()<1||strIDs2.length()<1)
- {
- return "";
- }
- else
- {
- if(strIDs1.length()>strIDs2.length())
- {
- strTemp = strIDs1;
- strIDs1=strIDs2;
- strIDs2=strIDs1;
- strTemp = "";
- }
- else
- {
- }
- if(strIDs1.indexOf(",")>=0 && strIDs2.indexOf(",")>=0)
- {
- String strID1[] = strIDs1.split(",");
- strIDs2="," + strIDs2 + ",";
- for(int i = 0; i < strID1.length; i++)
- {
- if(strIDs2.indexOf("," + strID1[i] + ",")>=0)
- {
- strTemp += "," + strID1[i];
- }
- else
- {
- }
- }
- }
- else
- {
- strIDs1 += "," + strIDs1 + ",";
- strIDs2 += "," + strIDs2 + ",";
- if(strIDs1.indexOf(strIDs2)>=0)
- {
- strTemp = strIDs2.substring(1,strIDs2.length()-1);
- }
- else if(strIDs2.indexOf(strIDs1)>=0)
- {
- strTemp = strIDs1.substring(1,strIDs1.length()-1);
- }
- else
- {
- strTemp = "";
- }
- }
- }
- return strTemp;
- }
- /**
- * 获取两个IDs字符串的并集
- * @param strIDs1 String IDs1
- * @param strIDs2 String IDs2
- * @return String 两个IDs字符串的交集
- */
- public static String getMergeOfIDs(String strIDs1,String strIDs2)
- {
- String strTemp = "";
- if(strIDs1.length()<1)
- {
- return strIDs2;
- }
- else if(strIDs2.length()<1)
- {
- return strIDs1;
- }
- else
- {
- if(strIDs1.length()>strIDs2.length())
- {
- strTemp = strIDs1;
- strIDs1=strIDs2;
- strIDs2=strIDs1;
- strTemp = "";
- }
- else
- {
- }
- if(strIDs1.indexOf(",")>=0 && strIDs2.indexOf(",")>=0)
- {
- String strID1[] = strIDs1.split(",");
- strIDs2="," + strIDs2 + ",";
- for(int i = 0; i < strID1.length; i++)
- {
- if(strIDs2.indexOf("," + strID1[i] + ",")<0)
- {
- strIDs2 += strID1[i] + ",";
- }
- else
- {
- }
- }
- strTemp = strIDs2;
- }
- else
- {
- strIDs1 += "," + strIDs1 + ",";
- strIDs2 += "," + strIDs2 + ",";
- if(strIDs1.indexOf(strIDs2)>=0)
- {
- strTemp = strIDs1.substring(1,strIDs2.length()-1);
- }
- else if(strIDs2.indexOf(strIDs1)>=0)
- {
- strTemp = strIDs2.substring(1,strIDs1.length()-1);
- }
- else
- {
- strTemp = strIDs1.substring(1,strIDs2.length()-1) + strIDs2.substring(0,strIDs1.length()-1);
- }
- }
- }
- return strTemp;
- }
- /**
- * 从原IDs1字符串中去掉在IDs2中包括的在IDs后得到新的IDs
- * @param strIDs1 String IDs1
- * @param strIDs2 String IDs2
- * @return String 从原IDs1字符串中去掉在IDs2中包括的在IDs后得到的新的IDs
- */
- public static String getSubtractOfIDs(String strIDs1,String strIDs2)
- {
- String strResult = "";
- if(strIDs1.length()<1)
- {
- strResult = "";
- }
- else if(strIDs2.length()<1)
- {
- strResult = strIDs1;
- }
- else
- {
- if(strIDs1.indexOf(",")>=0)
- {
- String strID1[] = strIDs1.split(",");
- for(int i = 0; i < strID1.length; i++)
- {
- if(("," + strIDs2 + ",").indexOf("," + strID1[i] + ",")<0)
- {
- if(i>0)strResult += ",";
- strResult += strID1[i];
- }
- else
- {
- }
- }
- }
- else if(("," + strIDs2 + ",").indexOf("," + strIDs1 + ",")>0)
- {
- strResult = strIDs1;
- }
- else
- {
- strResult = "";
- }
- }
- return strResult;
- }
- /**
- * 获取系统还可以试用的天数:<br>
- * -112 表示软件系统试用期限已到<br>
- * 1000 表示正式版软件系统,可以无限期使用<br>
- * 999 表示系统正在试用期限内,且没有到警戒提示日期内<br>
- * 其他 表示系统还可以试用的天数
- * @return int 系统还可以试用的天数
- */
- public static int getTryDays()
- {
- String strNow = StringNew.GetDateString(new java.util.Date(), "yyyy-MM-dd");
- String strExpDate = StringNew.getDisencodePassword(SystemParament.getParament("expdate"));
- String strMaxAlertDays = StringNew.getDisencodePassword(SystemParament.getParament("maxalertdays"));
- int iMaxAlertDays = 10;
- try
- {
- if(strMaxAlertDays.length()>0)
- {
- iMaxAlertDays = Integer.parseInt(strMaxAlertDays,10);
- }
- if(iMaxAlertDays>60||iMaxAlertDays<0)
- {
- iMaxAlertDays = 10;
- }
- }
- catch(Exception err){}
- if(strExpDate.equalsIgnoreCase("2088-08-08"))
- {
- return 1000;
- }
- else if (strNow.compareTo(strExpDate) > 0)
- {
- SystemOut.ErrOut("试用版软件,试用已经过期,请与029-88453031联系!");
- return -112;
- }
- else
- {
- SimpleDateFormat sdDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- try
- {
- Date dateNow = sdDateFormat.parse(strNow);
- Date dateExpDate = sdDateFormat.parse(strExpDate);
- long lLeftDays = (dateExpDate.getTime() - dateNow.getTime()) / 24l *
- 60l * 60l * 1000l;
- if (iMaxAlertDays >= lLeftDays) {
- return (int) lLeftDays;
- }
- else {
- return 999;
- }
- }
- catch(Exception err)
- {
- return -112;
- }
- }
- }
- /**
- * 从字符串中返回Float数值
- * @param strValue String 包含Float数值的字符串
- * @return float Float数值
- */
- public static float getFloatFromString(String strValue)
- {
- return getFloatFromString(strValue,0.0f);
- }
- /**
- * 从字符串中返回Float数值
- * @param strValue String 包含Float数值的字符串
- * @param fDefaultValue float 默认值
- * @return float Float数值
- */
- public static float getFloatFromString(String strValue,float fDefaultValue)
- {
- try
- {
- return Float.parseFloat(strValue);
- }
- catch(Exception err)
- {
- return fDefaultValue;
- }
- }
- }