📄 util.java
字号:
package com.wykj.util;
import java.util.GregorianCalendar;
import java.sql.Timestamp;
public class Util {
public Util() {
}
public static String trimNull(String str) {
if (str == null) {
return "";
}
return str.trim();
}
public static String getToday() {
GregorianCalendar g = new GregorianCalendar();
Timestamp today = new Timestamp(g.getTime().getTime());
return today.toString().substring(0, 10);
}
/**
* 实现Unicode-->中文,即Unicode转换成中文<br>
* //用法=================<br>
* 当你从数据库中读出的中文不能正确显示时,
* 可以使用这个方法:UnicodeToCh.unicodeToCh("读出的中文")<br>
* =================
* @param inStr--输入要转换的字符串
* @return java.lang.String
*/
public static String unicodeToCh(String inStr) {
if (inStr == null)return "";
String out = "";
// String databaseEncoding = AppinitWebBean.getInitKeyValue(
// "databaseEncoding");
// if (databaseEncoding == null || databaseEncoding.length() == 0) {
// databaseEncoding = "ISO-8859-1";
// }
try {
byte[] dbbyte1 = inStr.getBytes("iso-8859-1");
out = new String(dbbyte1, "gb2312");
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
/**
* 实现中文-->Unicode,即中文转换成Unicode<br>
* //用法=================<br>
* 当你将中文写入数据库中,发现数据库中没能正确保存
* 时,可以使用这个方法将这些中文转换后再写入:
* UnicodeToCh.chToUnicode("要写入的中文")<br>
* =================
* @param inStr--输入要转换的字符串
* @return java.lang.String
*/
public static String chToUnicode(String inStr) {
if (inStr == null)return "";
String out = "";
try {
byte[] bs = inStr.getBytes();
out = new String(bs, "iso-8859-1");
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
/**
* 数据库相关操作专用
* 实现Unicode-->中文,即Unicode转换成中文<br>
* //用法=================<br>
* 当你从数据库中读出的中文不能正确显示时,
* 可以使用这个方法:UnicodeToCh.unicodeToCh("读出的中文")<br>
* =================
* @param inStr--输入要转换的字符串
* @return java.lang.String
*/
public static String dbUnicodeToCh(String inStr) {
if (inStr == null)return "";
String out = "";
try {
byte[] dbbyte1 = inStr.getBytes("iso-8859-1");
out = new String(dbbyte1, "gb2312");
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
/**
* 数据库相关操作专用
* 实现中文-->Unicode,即中文转换成Unicode<br>
* //用法=================<br>
* 当你将中文写入数据库中,发现数据库中没能正确保存
* 时,可以使用这个方法将这些中文转换后再写入:
* UnicodeToCh.chToUnicode("要写入的中文")<br>
* =================
* @param inStr--输入要转换的字符串
* @return java.lang.String
*/
public static String dbChToUnicode(String inStr) {
if (inStr == null)return "";
String out = "";
try {
byte[] bs = inStr.getBytes("gb2312");
out = new String(bs, "iso-8859-1");
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
/**
*
* @param instr:用户输入的字符串
* @param key:
* 1表示从数据库中读出汉字,要求显示于页面
* 2表示要将汉字写入数据库
*/
//---------------------------------------------------------------------------
void setOutputString(String instr, int key) {
//instr:用户输入的字符串
//key:
// 1表示从数据库中读出汉字,要求显示于页面
// 2表示要将汉字写入数据库
String setStr = ""; //转换后的字符串
byte[] bs;
if (key == 1) { //读出汉字
try {
bs = instr.getBytes("iso-8859-1");
setStr = new String(bs, "gb2312");
}
catch (Exception e) {
setStr = "system is busy now!";
}
}
else if (key == 2) { //将汉字写入数据库时需要的转换
try {
bs = instr.getBytes();
setStr = new String(bs, "iso-8859-1");
}
catch (Exception e) {
setStr = "system is busy now!";
}
} //end if
this.outputString = setStr;
}
//---------------------------------------------------------------------------
String outputString = "";
/**
* 返回设置好的字符串
* @param instr
* @param key
* @return
*/
public String getOutputString(String instr, int key) {
setOutputString(instr, key);
return this.outputString;
}
/**
* 文件系统相关操作专用
* 实现Unicode-->中文,即Unicode转换成中文<br>
* //用法=================<br>
* 当你从数据库中读出的中文不能正确显示时,
* 可以使用这个方法:UnicodeToCh.unicodeToCh("读出的中文")<br>
* =================
* @param inStr--输入要转换的字符串
* @return java.lang.String
*/
public static String fileUnicodeToCh(String inStr) {
if (inStr == null)return "";
String out = "";
try {
byte[] dbbyte1 = inStr.getBytes("iso-8859-1");
out = new String(dbbyte1, "gb2312");
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
/**
* 文件系统相关操作专用
* 实现中文-->Unicode,即中文转换成Unicode<br>
* //用法=================<br>
* 当你将中文写入数据库中,发现数据库中没能正确保存
* 时,可以使用这个方法将这些中文转换后再写入:
* UnicodeToCh.chToUnicode("要写入的中文")<br>
* =================
* @param inStr--输入要转换的字符串
* @return java.lang.String
*/
public static String fileChToUnicode(String inStr) {
if (inStr == null)return "";
String out = "";
try {
byte[] bs = inStr.getBytes("gb2312");
out = new String(bs, "iso-8859-1");
}
catch (Exception e) {
e.printStackTrace();
}
return out;
}
///////////////////////
public static String getCHString(String instr) {
if (instr == null || instr.equals(""))
return "";
String outstr = "";
try {
outstr = new String(instr.getBytes("gb2312"), "iso8859-1");
}
catch (Exception e) {
e.printStackTrace();
}
return outstr;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -