📄 data.java
字号:
java.math.BigDecimal bd = new java.math.BigDecimal(num);
value = bd + "";
return value;
}
/**
* 将传入的时间转化成秒数
* @param value
* @return
*/
public static int returnSec(String value){
int result = 0;
int type = getCount(value,":");
Pattern p = Pattern.compile(":");
if(type==0)//ss
result = Integer.parseInt(value);
if(type==1){//mm:ss
String strValue[] = p.split(value);
result = Integer.parseInt(strValue[0])*60 + Integer.parseInt(strValue[1]);
}
if(type==2){//hh:mm:ss
String strValue[] = p.split(value);
result = Integer.parseInt(strValue[0])*3600 + Integer.parseInt(strValue[1])*60 + Integer.parseInt(strValue[2]);
}
return result;
}
/**
* 计算日期加上一个时间段(秒数)后的时间(未完成)
* @param datetime
* @param sec
* @return
*/
public static String timeAdd(String datetime,String value){
try{
int sec = returnSec(value);
String result = datetime;
datetime = convertDateToString(convertStringToDate(datetime),"datetime");
String date = datetime.substring(0,11).trim();
String time = datetime.trim().substring(11);
Pattern p = Pattern.compile(":");
String strTime[] = p.split(time);
int h = Integer.parseInt(strTime[0]);
int m = Integer.parseInt(strTime[1]);
int s = Integer.parseInt(strTime[2]);
if((s+sec)<60)
s = s+sec;
else{
int ms = (s+sec)%60;
int cs = (s+sec)/60;
s = ms;
if((m+cs)<60)
m = m +cs;
else{
int mm = (m+cs)%60;
int cm = (m+cs)/60;
m = mm;
h = h + cm;
}
}
String sh = h + "";
String sm = m + "";
String ss = s + "";
String newTime = sh + ":" + sm + ":" + ss;
result = datetime.replaceAll(time,newTime);
return formatDate(result);
}catch(Exception e){
System.out.println("Data.timeAdd():" + e.getMessage());
return "";
}
}
/**
* 计算一个字符串在另外一个字符串中出现的次数
* @param str
* @param sign
* @return
*/
public static int getCount(String str,String sign){
if(str==null)
return 0;
StringTokenizer s = new StringTokenizer(str,sign);
return s.countTokens()-1;
}
/**
* 得到当前系统日期
*
* @return 当前日期,格式为"yyyy-MM-dd"
*/
public static String getCurrentDate() {
String type = "yyyy-MM-dd";
SimpleDateFormat df = new SimpleDateFormat(type);
Date today = new Date();
String tString = df.format(today);
return tString;
}
/**
* 得到当前系统时间日期,自定义格式
*
* @return 当前日期,参考格式为"yyyy-MM-dd HH:mm:ss E 本月第F个星期"
*/
public static String getCurrentDateTime(String type) {
SimpleDateFormat df = new SimpleDateFormat(type);
Date today = new Date();
String tString = df.format(today);
return tString;
}
/**
* 得到当前系统时间
*
* @return 当前时间,格式为"HH:mm:ss"
*/
public static String getCurrentTime() {
String type = "HH:mm:ss";
SimpleDateFormat df = new SimpleDateFormat(type);
Date today = new Date();
String tString = df.format(today);
return tString;
}
/**
* 格式化日期格式,“yyyy-mm-dd hh:mm:ss”
* @author LiuChang
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public static String formatDate(String date){
if(date==null) date = "";
try{
if(date.equals("")){
System.out.println("传入的日期为空");
return "";
}else
return convertDateToString(convertStringToDate(date),"datetime");
}catch(Exception e){
System.out.println("Data.formatDate():" + e.getMessage());
return "";
}
}
/**
* 替换字符串中的"'"、">"、"<"、"\r\n"字符
* @param content 输入字符串
* @return 返回替换后的字符串
*/
public static String convertContent(String content){
content=content.trim();
content=content.replaceAll("'", "''");
content=content.replaceAll(">", ">").replaceAll("<", "<").replaceAll("\r\n","<br>");
return content;
}
/**
* 替换字符串中的"''"、">"、"<"、"<br>"字符
* @param content 输入字符串
* @return 返回替换后的字符串
*/
public static String showContent(String content){
content=content.trim();
content=content.replaceAll("''", "'");
content=content.replaceAll(">",">").replaceAll("<","<").replaceAll("<br>","\r\n");
return content;
}
/**
* 将钱数转换成大写
* @param input 输入钱数
* @return 返回转换后的钱数
*/public static String numtochinese(String input){
String s1="零壹贰叁肆伍陆柒捌玖";
String s4="分角整元拾佰仟万拾佰仟亿拾佰仟";
String temp="";
String result="";
if (input==null) return "输入字串不是数字串只能包括以下字符(?0?~?9?,?.?),输入字串最大只能精确到仟亿,小数点只能两位!";
temp=input.trim();
float f;
try{
f=Float.parseFloat(temp);
}catch(Exception e){return "输入字串不是数字串只能包括以下字符(?0?~?9?,?.?),输入字串最大只能精确到仟亿,小数点只能两位!";}
int len=0;
if (temp.indexOf(".")==-1) len=temp.length();
else len=temp.indexOf(".");
if(len>s4.length()-3) return("输入字串最大只能精确到仟亿,小数点只能两位!");
int n1,n2=0;
String num="";
String unit="";
for(int i=0;i<temp.length();i++){
if(i>len+2){break;}
if(i==len) {continue;}
n1=Integer.parseInt(String.valueOf(temp.charAt(i)));
num=s1.substring(n1,n1+1);
n1=len-i+2;
unit=s4.substring(n1,n1+1);
result=result.concat(num).concat(unit);
}
if ((len==temp.length()) || (len==temp.length()-1)) result=result.concat("整");
if (len==temp.length()-2) result=result.concat("零分");
return result;
}
/**
* 把yyyy-mm-dd hh:mm:ss转成yyyy-mm-dd
* @param date(日期)
* @return 日期
*/
public static String ConvertSmallDatetoDate(String date){
if(date==null) date = "";
try{
if(date.equals("")){
System.out.println("传入的日期为空");
return "";
}else{
int num1=date.indexOf(":");
if (num1>=0){
date=date.substring(0,num1-2);
}
}
return date;
}catch(Exception e){
System.out.println("Data.ConvertSmallDatetoDate():" + e.getMessage());
return "";
}
}
/**
* 去掉数字后的小数点 steven
* @param tmpNum(传入的数字)
* @return 返回数字
*/
public static String RemoveDot(String tmpNum){
if(tmpNum==null) tmpNum = "";
try{
if(tmpNum.equals("")){
System.out.println("传入的数字为空");
return "";
}else{
int num1=tmpNum.indexOf(".");
if (num1>=0){
tmpNum=tmpNum.substring(0,num1);
}
}
return tmpNum;
}catch(Exception e){
System.out.println("Data.RemoveDot():" + e.getMessage());
return "";
}
}
/**
* 将传入的money转化成会计数字
* @param sValue 传入的money
* @return 返回转化成会计数字
*/
public static String formatAccount(String sValue){
if(sValue.equals("")) return "";
String value = "";
String xvalue = "";
String zvalue = "";
if(sValue.indexOf(".")>=0){
xvalue = sValue.substring(sValue.indexOf("."));
zvalue = sValue.substring(0,sValue.indexOf("."));
}else{
zvalue = sValue;
}
boolean sign = false;
if(zvalue.indexOf("-")>=0){
sign = true;
zvalue = zvalue.substring(1);
}
int count = zvalue.length()/3;
for(int i=0;i<count;i++){
zvalue = zvalue.substring(0,zvalue.length()-3*(i+1)-i) + "," + zvalue.substring(zvalue.length()-3*(i+1)-i);
}
value = zvalue + xvalue;
if(value.indexOf(",")==0) value=value.substring(1);
if(sign) value = "-" + value;
return value;
}
/**
* 去掉小数点后面的无效的零(例如:1.000或1.)
* @param value 传入的数字
* @return 返回去掉无效零的数字
*/
public static String formatNum(String value){
if(value.indexOf(".")>=0){
while(value.substring(value.length()-1,value.length()).equals("0")){
value = value.substring(0,value.length()-1);
}
if(value.substring(value.length()-1,value.length()).equals(".")){
value = value.substring(0,value.length()-1);
}
}
return value;
}
/**
* UTF-8编码转换GB2312
* @param strln 传入字符串
* @return 返回转换后的字符串
*/
public static String UnicodeToGB(String strln){
String strOut=null;
if(strln==null||(strln.trim()).equals(""))
return strln;
try{
strOut=new String(strln.getBytes("ISO-8859-1"),"gb2312");
}catch(UnsupportedEncodingException e){}
return strOut;
}
/**
* GB2312转换UTF-8编码
* @param strln 传入字符串
* @return 返回转换后的字符串
*/
public static String GBToUnicode(String strln){
String strOut=null;
if(strln==null||(strln.trim()).equals(""))
return strln;
try{
byte[] b=strln.getBytes("ISO8859_1");
strOut=new String(b,"GB2312");
}catch(Exception e){}
return strOut;
}
public static String GBToUtf(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0)
k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
public static String UtfToGB(String strln){
String strOut="";
byte [] b;
try{
b = strln.getBytes("8859_1"); //中间用ISO-8859-1过渡
strOut = new String(b, "GB2312");
}catch(Exception e){
e.printStackTrace();
}
return strOut;
}
/**
*四舍五入
*传入数字/精度
* @param v 传入的数字
* @param scale 要保留的位数
* @return 返回四舍五入后的数字
*/
public static double round(double v,int scale){
java.math.BigDecimal b = new java.math.BigDecimal(Double.toString(v));
java.math.BigDecimal one = new java.math.BigDecimal("1");
return b.divide(one,scale,java.math.BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 过滤跨站脚本关键字
* @param str 输入字符串
* @return 返回过滤后的字符串
*/
public static String filterStr(String str){
if(str==null)
return "";
str = str.replaceAll("<","");
str = str.replaceAll(">","");
str = str.replaceAll("'","");
str = str.replaceAll("&","&");
str = str.replaceAll("#","#");
str = str.replaceAll("%","%");
str = str.replaceAll("\"","");
return str;
}
///////////////////////////////////////////////////////////////////
// public static void main(String[] args){
// try{
// String a ="%u5728";
// System.out.println(a);
// byte [] b;
// //utf8_value = request.getParameter("NAME");//从HTTP流中取"NAME"的UTF8数据
// b = a.getBytes("8859_1"); //中间用ISO-8859-1过渡
// String name = new String(b, "GB2312"); //转换成GB2312字符
// System.out.println(name);
//String a = Data.UnicodeToGB("\u5B9A\u4E49\u6A21\u677F\u8282\u70B9\u4FE1\u606F");
// System.out.println(Data.formatValue("ss","char"));
// System.out.println();
// }catch(Exception ex){
// }
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -