📄 pubfunc.java
字号:
//更改文件名
public static boolean moveFile(String sfilename,String dfilename){
File fs = new File(sfilename);
File fd = new File(dfilename);
return fs.renameTo(fd);
}
//取文件全称不含路径的文件名
public static String getFileName(String filePathName){
String token = new String();
String value = new String();
int pos = 0;
int i = 0;
int start = 0;
int end = 0;
pos = filePathName.lastIndexOf(47);
if(pos != -1){
return filePathName.substring(pos + 1,filePathName.length());
}
pos = filePathName.lastIndexOf(92);
if(pos != -1){
return filePathName.substring(pos + 1,filePathName.length());
} else{
return filePathName;
}
}
//判断字串desc是否存在于source,存在则返回true,不存在返回false
public static boolean inStr(String source,String desc){
if(source.indexOf(desc) >= 0){
return true;
} else{
return false;
}
}
//取两整数相除最大值
public static int celling(int a,int b){
int c = a / b;
if(a != b * c){
c = c + 1;
}
return c;
}
//取余数
public static int mod(int a,int b){
int c = a / b;
return a - b * c;
}
public static String setMenuTitle(String menulist,int no,String value){
if(getMenuTitle(menulist,no).equals("")){
return menulist.substring(0,menulist.indexOf("}}")) + "} {" +
String.valueOf(no) + " " + value + "}}";
} else{
String t = menulist.substring(menulist.indexOf("{" +
String.valueOf(no) + " "));
return menulist.substring(0,
menulist.indexOf("{" + String.valueOf(no) +
" ")) + "{" + String.valueOf(no) + " " + value +
t.substring(t.indexOf("}"));
}
}
public static String getMenuTitle(String menulist,int no){
String title = "";
int start = menulist.indexOf("{" + String.valueOf(no));
if(start >= 0){
title = menulist.substring(start + 3);
title = title.substring(0,title.indexOf("}"));
}
return title;
}
public static int getMenuId(String menulist,int idx){
String s1 = "{{";
String s2 = " {";
String mid = "-1";
for(int i = 0;i < menulist.length();i++){
if(menulist.startsWith(s1,i) || (menulist.startsWith(s2,i))){
idx--;
}
if(idx < 0){
mid = menulist.substring(i + 2);
mid = mid.substring(0,menulist.indexOf(" ") - 1).trim();
break;
}
}
if((mid == null) || mid.trim().equals("")){
mid = "-1";
}
return Integer.parseInt(mid);
}
//字符串内码转换--用于写入数据库时
public static String encodeGB(String source,String charset){
try{
return(new String(source.getBytes("GB2312"),charset));
} catch(Exception e){
return source;
}
}
public static String encodeGB(String source){
try{
return(new String(source.getBytes("GBK"),"cp850"));
} catch(Exception E){
return source;
}
}
public static String decodeISO(String source){
try{
if(source == null){
return "";
}
// return source;
return new String(source.getBytes("ISO-8859-1"),"GBK");
} catch(Exception e){
return "";
}
}
//字符串内码转换--用于从数据库读取数据时
public static String decodeGB(String source,String charset){
try{
return(new String(source.getBytes(charset),"GBK"));
} catch(Exception E){
return source;
}
}
public static String decodeGB(String source){
try{
//return new String(source.getBytes("cp850"),"GBK");
return new String(source.getBytes("GBK"),"GBK");
} catch(Exception E){
return source;
}
}
//字符串转换为HTML格式用来显示,即空串转为空格
public static String nullToHtmlStr(String s){
if(s == null || s.trim().length() <= 0){
return " ";
} else{
return s;
}
}
//字符串转换,即NULL转为"",非空则不变
public static String nullToStr(String s){
if(s == null || s.trim().length() < 0){
return "";
} else{
return s.trim();
}
}
//修改企业查重结果中乱码问题lxh20031107
//字符串转换,即NULL转为"",非空则不变
public static String nullToString(String s){
if(s == null || s.trim().length() < 0){
return "";
} else{
try{
s = new String(s.getBytes("ISO-8859-1"),"GBK");
} catch(Exception e){
}
return s.trim();
}
}
//替换source中的str1为str2
public static String replace(String source,char str1,String str2){
if(source == null){
return source;
}
String desc = "";
for(int i = 0;i < source.length();i++){
if(source.charAt(i) == str1){
desc = desc + str2;
} else{
desc = desc + String.valueOf(source.charAt(i));
}
}
return desc;
}
//替换source中的str1为str2
public static String replace(String source,String str1,String str2){
if(source == null){
return source;
}
String desc = "";
int i = 0;
while(i < source.length()){
if(source.startsWith(str1,i)){
desc = desc + str2;
i = i + str1.length();
} else{
desc = desc + String.valueOf(source.charAt(i));
i++;
}
}
return desc;
}
//转换字符串用于SQL串中(把'=>")
public static String toSqlStr(String source){
if(source == null){
return "";
}
return replace(source,'\'',"\'\'");
}
//转换字符串用于SQL串中(把'=>")
public static String toSqlStr(String source,int flag){
source = toSqlStr(source);
source = "'" + source + "'";
if(flag == 0){
return "," + source;
} else{
return source;
}
}
//判断字符串是否为空
public static boolean isNullStr(String s){
if(s == null || s.trim().length() <= 0){
return true;
} else{
return false;
}
}
//判断字符串数组是否为空
public static boolean isNullStr(String[] s){
if((s == null) || (s.length <= 0)){
return true;
} else{
return false;
}
}
//按字段的字段查询值加条件(加LIKE)
public static String strLike(String fieldValue,String field){
if(isNullStr(fieldValue)){
return " ";
} else{
return " and " + field + " like '%" + toSqlStr(fieldValue.trim()) +
"%' ";
}
}
//按字段的字段查询值加条件(加LIKE)
public static String strOrLike(String fieldValue,String field){
if(isNullStr(fieldValue)){
return " ";
} else{
return " or " + field + " like '%" + toSqlStr(fieldValue.trim()) +
"%' or '" + toSqlStr(fieldValue.trim()) + "' like '%'+" + field +
"+'%' ";
}
}
//按长度把字符串前补0
public static String strLen1(String s,int len){
if(isNullStr(s)){
s = "";
}
int strLen = s.length();
for(int i = 0;i < len - strLen;i++){
s = "0" + s;
}
return s;
}
public static String strLen(String s,int len){
if(isNullStr(s)){
s = "";
}
if(s.length() == 8){
return s;
}
for(int i = 0;i < len - s.length();i++){
s = "0" + s;
if(s.length() == 8){
break;
}
}
return s;
}
//返回日历的年字符串
public static String getYear(Calendar cal){
return String.valueOf(cal.get(cal.YEAR));
}
//返回日历的月字符串(两位)
public static String getMonth(Calendar cal){
return strLen(String.valueOf(cal.get(cal.MONTH) + 1),2);
}
//返回日历的日字符串(两位)
public static String getDay(Calendar cal){
return strLen(String.valueOf(cal.get(cal.DAY_OF_MONTH)),2);
}
//返回日历的时字符串(两位)
public static String getHour(Calendar cal){
return strLen(String.valueOf(cal.get(cal.HOUR_OF_DAY)),2);
}
//返回日历的分字符串(两位)
public static String getMinute(Calendar cal){
return strLen(String.valueOf(cal.get(cal.MINUTE)),2);
}
//返回日历的秒字符串(两位)
public static String getSecond(Calendar cal){
return strLen(String.valueOf(cal.get(cal.SECOND)),2);
}
//返回日历的日期字符串(格式:"yyyy-mm-dd")
public static String getDateStr(Calendar cal){
return getYear(cal) + "-" + getMonth(cal) + "-" + getDay(cal);
}
//返回日期串的年月日串(格式:"yyyy年mm月dd日")
public static String getDateStr(String date){
return getYear(date) + "年" + getMonth(date) + "月" + getDay(date) + "日";
}
//返回日历的日期字符串(格式:"yyyy年mm月dd日")
public static String getDateStrZ(Calendar cal){
return getYear(cal) + "年" + getMonth(cal) + "月" + getDay(cal) + "日";
}
//返回日历的时间字符串(格式:"hh:ss") mod by ljc 返回(格式:"hh:mm")
public static String getTimeStr(Calendar cal){
return getHour(cal) + ":" + getMinute(cal) + ":" + getSecond(cal);
// return getHour(cal) + "时";
}
//返回日历的日期时间字符串(格式:"yyyy-mm-dd hh:ss")
public static String getDate(Calendar cal){
return getDateStr(cal) + " " + getTimeStr(cal);
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的年
public static int getYear(String s){
if(s == null || s.length() < 10){
return 1970;
}
return Integer.parseInt(s.substring(0,4));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的月
public static int getMonth(String s){
if(s == null || s.length() < 10){
return 1;
}
return Integer.parseInt(s.substring(5,7));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的日
public static int getDay(String s){
if(s == null || s.length() < 10){
return 1;
}
return Integer.parseInt(s.substring(8,10));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的时
public static int getHour(String s){
if(s == null || s.length() < 16){
return 0;
}
return Integer.parseInt(s.substring(11,13));
}
//返回日期字符串("yyyy-mm-dd hh:ss:mm")的分
public static int getMinute(String s){
if(s == null || s.length() < 16){
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -