Java小知识
1.转换编码:
str = new String(str.getBytes("ISO-8859-1"),"utf-8");
**************************************************************
2.数据类型之间的转换:
int a=(int)b;
int a=b.intValue();
int a=Ingtger.parseInt(b);
Integer a= new Integer(b);
int i=Integer.valueOf(b).intValue();
String类型:
String a=b.toString();
String a=b+"";
a.startsWith(b);//前包含
a.endsWith(b);//后包含
a.equals(b);//等于
a.equalsIgnoreCase (b);//忽略大小写
a.contains(b);//包含
****************************************************************
3.double数据类型的大小比较
double a = 0.01;
double b = 0.001;
BigDecimal data1 = new BigDecimal(a);
BigDecimal data2 = new BigDecimal(b);
double result=data1.compareTo(data2);
data1>data2 result=1;
data1<data2 result=-1;
data1=data2 result=0;
**************************************************************************
4.将IP转化为Long型的公式:
String[] oraginal = this.value.split("\\.");
long o = 0l;//原值
o += Long.parseLong(oraginal[i]) << (24 - i * 8);
************************************************************************************
5.得到当前方法的名字
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
**********************************************************************************************
6.单实例Singleton 示例
public class SimpleSingleton {
private static SimpleSingleton singleInstance = new SimpleSingleton();
//Marking default constructor private
//to avoid direct instantiation.
private SimpleSingleton() {
}
//Get instance for class SimpleSingleton
public static SimpleSingleton getInstance() {
return singleInstance;
}
}
*******************************************************************************************************
7.加密解密
Encrypt.encode("admin");
Encrypt.decode(str1),"utf-8")
***************************************************************************************************************
8.mongodb的一些用法
DBCursor result1 = mongoTemplates.firstMongoTemplate().getCollection("alarm").find(bdb, keys).sort(new BasicDBObject("_id", -1)).limit(1000);
Gson gson = new Gson();
while (result1.hasNext()) {
DBObject obj = result1.next();
Map<String, Object> mapp = obj.toMap();
Iterator<Map.Entry<String, Object>> entries = mapp.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, Object> entry = entries.next();
}
}
***************************************************************************************************************************
9.js全选
// 全选/ 全不选
$("#checkallbox").click(function(){
var isChecked = this.checked;
$("input[name='hobby']").each(function(){
this.checked = isChecked;
});
});
// 全选
$("#checkAllBtn").click(function(){
$("input[name='hobby']").attr("checked","checked");
});
// 全不选
$("#checkAllNotBtn").click(function(){
$("input[name='hobby']").removeAttr("checked");
});
// 反选
$("#checkOppoBtn").click(function(){
$("input[name='hobby']").each(function(){
this.checked = !this.checked;
});
});
//选中的去右边
$("#chooseToRight").click(function(){
$("#right").append($("#left option:selected"));
});
// 全去右边
$("#allToRight").click(function(){
$("#right").append($("#left option"));
});
// 全去左边
$("#allToLeft").click(function(){
$("#left").append($("#right option"));
});
//选中的去左边
$("#chooseToLeft").click(function(){
$("#left").append($("#right option:selected"));
});