📄 operate.java
字号:
package com.idoc.util;
/**
* <p>Title: IDOC Interface</p>
* <p>Description: IDOC 转换器</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: SiChuan XinHua</p>
* @author Richary
* @version 1.0
*/
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Properties;
public class Operate {
public Operate() {
}
// 将ISO-8859-1字符集转换成GB2312
public String uni2GB(String uni) {
String sResult = null;
if (uni == null) {
uni = "";
}
if (uni.trim().equals("")) {
return uni;
}
try {
byte[] b = uni.getBytes("ISO-8859-1");
sResult = new String(b, "GB2312");
}
catch (Exception e) {
System.out.println("Operate's uni2GB!");
e.printStackTrace();
}
return sResult;
}
// 将字符集GB2312转换成ISO-8859-1
public String gb2Uni(String gb) {
String sResult = null;
if (gb == null) {
gb = "";
}
if (gb.trim().equals("")) {
return gb;
}
try {
byte[] b = gb.getBytes("GB2312");
sResult = new String(b, "ISO-8859-1");
}
catch (Exception e) {
System.out.println("Operate's gb2Uni!");
e.printStackTrace();
}
return sResult;
}
public String gb2CP850(String gb) {
String sResult = null;
if (gb == null) {
gb = "";
}
if (gb.trim().equals("")) {
return gb;
}
try {
byte[] b = gb.getBytes("GB2312");
sResult = new String(b, "cp850");
}
catch (Exception e) {
System.out.println("Operate's gb2CP850!");
e.printStackTrace();
}
return sResult;
}
public String cp8502GB(String cp) {
String sResult = null;
if (cp == null) {
cp = "";
}
if (cp.trim().equals("")) {
return cp;
}
try {
byte[] b = cp.getBytes("cp850");
sResult = new String(b, "GB2312");
}
catch (Exception e) {
System.out.println("Operate's cp8502GB!");
e.printStackTrace();
}
return sResult;
}
// uni2CP850
public String uni2CP850(String uni) {
return gb2CP850(uni2GB(uni));
}
// CP8502uni
public String cp8502Uni(String cp) {
return gb2Uni(cp8502GB(cp));
}
public Date str2Date(String sDate) { // 字符型转换成日期型
Date rDate = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
rDate = sdf.parse(sDate);
}
catch (ParseException e) {
System.out.println("Operate's str2Date!");
e.printStackTrace();
}
return rDate;
}
//字符串转换成float型
public float str2float(String sfloat) {
float ret = 0;
try {
ret = Float.parseFloat(sfloat);
}
catch (NumberFormatException e) {
System.out.println("Operate's str2float!");
e.printStackTrace();
}
finally {
return ret;
}
}
//取字符串中的一个子字符串
//相当与切取字符串后得到新的字符串
public String substring(String src, int fromIndex, int toIndex) {
String ret = "";
try {
ret = src.substring(fromIndex, toIndex);
}
catch (Exception e) {
System.out.println("Operate's substring A!");
e.printStackTrace();
}
finally {
return ret;
}
}
//取字符串中的一个子字符串
//相当与切取字符串后得到新的字符串
public String substring(String src, int fromIndex) {
String ret = "";
try {
ret = src.substring(fromIndex);
}
catch (Exception e) {
System.out.println("Operate's substring B!");
e.printStackTrace();
}
finally {
return ret;
}
}
/**
* 返回日期时间
* @param date
* @return
*/
public static Date getStarDate(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
//System.out.println(format.format(format.parse(date)));
return format.parse(date);
}
catch (Exception ex) {
return new Date();
}
}
public String getDateString(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
//System.out.println(format.format(format.parse(date)));
return format.format(format.parse(date));
}
catch (Exception ex) {
return "";
}
}
// 格式化时间
public String formatDate(String date) {
if (date == null || date.equalsIgnoreCase("null") || date.equalsIgnoreCase("")) {
return "";
}
try {
date = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6);
SimpleDateFormat now_date_sdf = new SimpleDateFormat("yyyy-MM-dd");
//System.out.println(": " + now_date_sdf.parse(date));
date = now_date_sdf.format(now_date_sdf.parse(date));
}
catch (Exception ex) {
//System.out.println("AAA : " + date);
return " ";
}
return date;
}
/**
* 根据日期型字符返回一个日期
* @param date
* @return
*/
public Date getDate(String date) {
Date date1 = null;
if (date == null || date.equalsIgnoreCase("null")) {
return date1;
}
try {
SimpleDateFormat now_date_sdf = new SimpleDateFormat("yyyy-MM-dd");
date1 = now_date_sdf.parse(date);
}
catch (Exception ex) {
javax.swing.JOptionPane.showMessageDialog(null, "请输入有效的日期,确认你的格式!",
"日期错误", 0, null);
return null;
}
return date1;
}
/**
* display
* @param date
* @return
*/
public String parseDate(Date date) {
String date1 = null;
try {
if (date == null) {
return "";
}
SimpleDateFormat now_date_sdf = new SimpleDateFormat("yyyy-MM-dd");
date1 = now_date_sdf.format(date);
}
catch (Exception ex) {
javax.swing.JOptionPane.showMessageDialog(null, "请输入有效的日期,确认你的格式!",
"日期错误", 0, null);
//System.out.println("AAA : " + date);
return "";
}
return date1;
}
/**
* display
* @param date
* @return
*/
public String parseDate(String date) {
try {
if (date == null || date.equalsIgnoreCase("null")) {
return "";
}
Date date1 = null;
SimpleDateFormat now_date_sdf = new SimpleDateFormat("yyyy-MM-dd");
date1 = now_date_sdf.parse(date);
date = now_date_sdf.format(date1);
}
catch (Exception ex) {
//System.out.println("AAA : " + date);
return "";
}
return date;
}
// 系统当前日期
public static String getNowDate() {
Date now_date = new Date();
SimpleDateFormat now_date_sdf = new SimpleDateFormat("yyyy-MM-dd");
String now_date_s = now_date_sdf.format(now_date).toString();
return now_date_s;
}
// 得到当前时间相差N天的日期
public String getDelayDate(int n) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, n);
Date date = cal.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date).toString();
}
// 将TIMESTAMP转换成日期的STRING形式
public String getDateString(Timestamp timeStamp) {
String dateS = "";
if (timeStamp == null) {
return dateS;
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
dateS = sdf.format( (Date) timeStamp);
}
catch (Exception e) {
System.out.println("Operate's getDateString!");
e.printStackTrace();
}
finally {
return dateS;
}
}
/**
* 得到配置文件的值
* @param key
* @return
*/
public Properties getProperties(String filename) {
Properties properties = null;
URL url = null;
try {
url = com.idoc.util.Operate.class.getResource("");
}
catch (Exception ex) {
System.out.println("Operate's getProperties url");
}
File file = null;
try {
file = new File(url.toString().substring(6) + "/" + filename);
//System.out.println(file.toURI() + file.toString());
InputStream reader = new FileInputStream(file);
properties = new Properties();
properties.load(reader);
}
catch (IOException e) {
try {
System.out.println("file.URL " + file.toURL());
e.printStackTrace();
}
catch (Exception ex11) {
//
}
}
//System.out.println("properties : " + properties);
return properties;
}
/**
*
* @param str is value
* @param len field's length
* @return
*/
public static String addEmpty(String src, int len)
{
StringBuffer result = new StringBuffer();
if (src == null)
{
src = " ";
}
result.append(src);
try
{
if (src != null && src.length() >= 0)
{
byte[] bb = src.getBytes("GBK");
for (int i = len; i > bb.length; i--)
{
result.append(" ");
}
}
}
catch (Exception e)
{
return result.toString();
}
return result.toString();
}
/* public static String addEmpty(String str, int len) {
String t = null;
if (str == null) {
for (int i = 0; i < len; i++) {
if (t == null) {
t = " ";
}
else {
t = t + " ";
}
}
}
else {
if (str.trim().length() == len) {
return str;
}
if (str.trim().length() < len) {
for (int i = 0; i < (len - str.trim().length()); i++) {
if (t == null) {
t = str.trim() + " ";
}
else {
t = t + " ";
}
}
}
}
return t;
}*/
/**
* 得到字符串str从l到r的字符串
* @param str
* @param l
* @param r
* @return
*/
public String getsubString(String str, int l, int r) {
if (str == null || str.equalsIgnoreCase("")) {
return "";
}
String t1 = null;
try {
t1 = str.substring(l, r).trim();
}
catch (Exception ex) {
return "";
}
if (l == 69 && r == 73 && t1.equalsIgnoreCase("")) {
return "RFST";
}
return t1;
}
public String getsubString(String str, int l) {
if (str == null || str.equalsIgnoreCase("")) {
return "";
}
String t1 = null;
try {
t1 = str.substring(l).trim();
}
catch (Exception ex) {
return "";
}
return t1;
}
/**
* if str is null,then str convert empty.
* @param str
* @return
*/
public String convertNull(String str) {
if (str == null || str.equalsIgnoreCase("null")) {
str = "";
}
return str;
}
/**
* 把v_vec 添加到 r_vec上去
* @param r_vec
* @param v_vec
* @return
*/
public static Vector uniteVector(Vector r_vec, Vector v_vec) {
Vector vec = new Vector();
try {
if (r_vec == null || r_vec.size() < 1) {
if (v_vec == null || v_vec.size() < 1) {
return new Vector();
}
else {
return v_vec;
}
}
else {
for (Enumeration en = r_vec.elements(); en.hasMoreElements(); ) {
vec.add(en.nextElement());
}
for (Enumeration en = v_vec.elements(); en.hasMoreElements(); ) {
vec.add(en.nextElement());
}
//是否清出r_vec,v_vec
return vec;
}
}
catch (Exception ex) {
return new Vector();
}
}
/**
* 数据库单引号转义
* @param source
* @return
*/
public String escape(String source) {
char escapechar = '\'';
StringBuffer buff = new StringBuffer();
int time = 0;
int pos = 0, from = 0;
for (; (pos = source.indexOf(escapechar, from)) > -1; time++) {
buff.append("''").append(source.substring(from, pos));
from = pos + 1;
}
if (time == 0) {
return source;
}
else {
buff.append(source.substring(from));
return buff.toString();
}
}
/**
* 转义
*
* @param source
* @return
*/
public static String descape(String source){
String escapechar="''";
StringBuffer buff=new StringBuffer();
int time=0;
int pos=0,from=0;
for(;(pos=source.indexOf(escapechar,from))>-1;time++){
buff.append("'").append(source.substring(from,pos));
from=pos+2;
}
if (time==0){
return source;
}else{
buff.append(source.substring(from));
return buff.toString();
}
}
public static void main(String[] args){
Operate oo=new Operate();
String src = "2008-11-2";
String oo2 = oo.substring(src, 1, 2);
System.out.println(oo2);
// String ss="中国";
//System.out.println(ss.length());
//
// String ss2=oo.addEmpty(ss,10);
// System.out.println(ss2.length());
// System.out.println(ss2);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -