📄 dutilbean.java
字号:
/*
* Name: DUtilBean.java
* Version: 1.0
* Copyright(c) HitachiInformation Systems,Ltd.2005 All right reserved.
* Created: 2006/3/21
* Author: jackie
* Updated:
*/
package com.hbnu.common;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
/**
* 关于bean的描述
*
* @author 王雄
*
*/
public class DUtilBean {
public static String sqlEscape(String str) {
if (str == null) {
str = "";
}
if (str.indexOf("'") >= 0) {
str = str.replaceAll("'", "''");
}
if (str.indexOf("\\") >= 0) {
str = str.replaceAll("\\\\", "\\\\\\\\");
}
if (str.indexOf("%") >= 0) {
str = str.replaceAll("%", "%%");
}
str = " '" + str + "' ";
return str;
}
public static String formatDate6(String date6) {
try {
if (date6.length() != 6) {
return "";
}
int intDate = Integer.parseInt(date6);
String formatDate =
date6.substring(0,4) + "/" + date6.substring(4,6);
return formatDate;
}catch(Exception e){
return "";
}
}
public static String formatDate8(String date8) {
try {
if (date8.length() != 8) {
return "";
}
int intDate = Integer.parseInt(date8);
String formatDate =
date8.substring(0,4) + "/" + date8.substring(4,6) + "/" + date8.substring(6,8);
return formatDate;
}catch(Exception e){
return "";
}
}
public static String formatDate7(String date7) {
try {
if (date7.length() != 7) {
return "";
}
String formatDate =
date7.substring(0,4) + date7.substring(5,7);
return formatDate;
}catch(Exception e){
return "";
}
}
public static String formatDate10(String date10) {
try {
if (date10.length() != 10) {
return "";
}
String formatDate =
date10.substring(0,4) + date10.substring(5,7) + date10.substring(8,10);
return formatDate;
}catch(Exception e){
return "";
}
}
public static String getSystemDate() {
GregorianCalendar calendar = new GregorianCalendar();
String year = String.valueOf(calendar.get(Calendar.YEAR));
String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
if (month.length() == 1) {
month = "0" + month;
}
String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
if (day.length() == 1) {
day = "0" + day;
}
String systemDate = year + "/" + month + "/" + day;
return systemDate;
}
public static String addComma(String strNum) throws Exception {
String strValue = null;
if (strNum == null || strNum.equals("")) {
return strNum;
}
strValue = strNum.replaceAll(",", "");
strValue = strValue.replace('.', ',');
String strList[] = strValue.split(",");
try {
DecimalFormat dmfFormat = new DecimalFormat("###,###,###,###,###");
if (strList[0].equals("")) {
strList[0] = "0";
}
strValue = dmfFormat.format(Long.parseLong(strList[0]));
//if (strList.length == 2) {
// strValue = strValue + "." + strList[1];
//}
} catch (Exception ex) {
throw new Exception(ex);
}
return strValue;
}
public static String removePercent(String strNum) throws Exception {
if (strNum == null || "".equals(strNum)) {
return strNum;
}
int sizeNum = strNum.length();
if (strNum.charAt(sizeNum-1) == '%') {
String strReturn = "0.";
for(int j=0; j<sizeNum-1; j++) {
strReturn = strReturn + strNum.charAt(j);
}
strNum = strReturn;
}
return strNum;
}
public static String removeComma(String strNum) throws Exception {
String strValue = null;
if (strNum == null || strNum.equals("")) {
return strNum;
}
// try {
strValue = strNum.replaceAll(",", "");
// Double dblValue = Double.valueOf(strValue);
// } catch (Exception ex) {
// throw new Exception(ex);
// }
return strValue;
}
public static String removeMh(String strNum) throws Exception {
String strValue = null;
if (strNum == null || "".equals(strNum)) {
return strNum;
}
strValue = strNum.replaceAll(":", "");
return strValue;
}
public static String addMh(String strNum) throws Exception {
String strValue = null;
if ("".equals(strNum) || strNum == null) {
strValue = "";
}else if(strNum.length()==5){
return strNum;
}else{
strValue = strNum.substring(0,2)+":"+strNum.substring(2,4);
}
return strValue;
}
public static boolean isHalfDecimal(String str) {
if (str.equals("-")) {
return false;
}
int cnt = 0;
if (str.length() != str.getBytes().length) {
return false;
}
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (!Character.isDigit(ch) && (i != 0 || ch != '-')
&& (i <= 0 || i >= str.length() - 1 || ch != '.')
|| Character.UnicodeBlock.of(ch) != Character.UnicodeBlock.BASIC_LATIN) {
return false;
}
if (ch == '.') {
cnt++;
}
}
return cnt <= 1;
}
public static boolean checkHankakuSyuji(String s) {
String standandString = "0123456789";
if (s == null) {
return true;
}
for (int i = 0; i < s.length(); i++) {
if (standandString.indexOf(s.charAt(i)) == -1) {
return false;
}
}
return true;
}
public static boolean checkYMD(String date) {
if(date==null || "".equals(date)){
return true;
}
if((date != null && date.length()!=8)||((!"".equals(date) && date.length() != 8))){
return false;
}
try{
int year = new Integer(date.substring(0,4)).intValue();
int month = new Integer(date.substring(4,6)).intValue();
int day = new Integer(date.substring(6,8)).intValue();
Calendar cal = Calendar.getInstance();
cal.setLenient( false );
cal.set(year, month-1, day);
cal.getTime();
}catch(Exception e){
return false;
}
return true;
}
public static boolean checkCode(String s) {
String standandString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
if (s == null) {
return true;
}
for (int i = 0; i < s.length(); i++) {
if (standandString.indexOf(s.charAt(i)) == -1) {
return false;
}
}
return true;
}
public static String length(String strTarget, int length){
StringBuffer strbf = null;
StringBuffer strbfResult = new StringBuffer();
try {
int lengthFn = length - 1;
for (int i=0; i<length; i++) {
strbf = new StringBuffer();
strbf.append(strTarget.charAt(i));
byte[] bytes = strbf.toString().getBytes();
if(bytes.length == 2) {
lengthFn = lengthFn - 1;
}
if (i > lengthFn) {
return strbfResult.toString();
}
strbfResult.append(strbf);
}
return strbfResult.toString();
} catch (Exception e) {
return strTarget;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -