📄 commen.java
字号:
package com.strong.ims.comutil;
/*
* Created on 2005/3/12
*
* TODO To change the template for this generated file go to Window -
* Preferences - Java - Code Style - Code Templates
*/
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 一些通用方法
* @author jqg
*
*/
public class Commen {
/**
*
*
* @param serial
* @param length
* @return String
*/
public String adjustNumber(String serial, int length) throws Exception {
String res = "";
try{
for(int i = 0; i < (length - String.valueOf(serial).length()); i++){
res += "0";
}
res += serial;
}
catch (Exception e){
System.out.println("Commen:adjustNumber:error");
throw e;
}
return res;
}
/**
*
* @param str
* @return String
*/
public String adjustStrig(String str) throws Exception {
try{
if(str == null || str.equals("null")){
str = "";
}
}
catch (Exception e){
System.out.println("Commen:adjustStrig:error");
throw e;
}
return str;
}
/**
*
*
* @param transDate
* @return String
*/
public String transChinese(String transDate) throws Exception {
String[] d = transDate.split("/");
int year = Integer.parseInt(d[0]);
try{
year = year + 1911;
transDate = year + "/" + d[1] + "/" + d[2];
}
catch (Exception e){
System.out.println("Commen:transChinese:error");
throw e;
}
return transDate;
}
/**
*
*
* @return String
*/
public String getChineseDate() throws Exception {
try{
DecimalFormat df = new DecimalFormat("00");
Calendar c = Calendar.getInstance();
int nYear = c.get(Calendar.YEAR) - 1911;
int nMonth = c.get(Calendar.MONTH);
int nDay = c.get(Calendar.DAY_OF_MONTH);
return nYear + "/" + df.format(nMonth + 1) + "/" + df.format(nDay);
}
catch (Exception e){
System.out.println("Commen:getChineseDate:error");
throw e;
}
}
/**
*
*
* @return String
*/
public String getChineseDate2() throws Exception {
try{
DecimalFormat df2 = new DecimalFormat("00");
DecimalFormat df3 = new DecimalFormat("000");
Calendar c = Calendar.getInstance();
int nYear = c.get(Calendar.YEAR) - 1911;
int nMonth = c.get(Calendar.MONTH);
int nDay = c.get(Calendar.DAY_OF_MONTH);
return df3.format(nYear) + "/" + df2.format(nMonth + 1) + "/"
+ df2.format(nDay);
}
catch (Exception e){
System.out.println("Commen:getChineseDate2:error");
throw e;
}
}
/**
*
*
* @param date
* @return String
*/
public String getChineseDate(java.util.Date date) throws Exception {
String res = "";
try{
if(date != null){
DateFormat dateformat = DateFormat.getDateInstance(
DateFormat.FULL, Locale.TAIWAN);
dateformat = new SimpleDateFormat("/MM/dd");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int nYear = cal.get(Calendar.YEAR) - 1911;
res = nYear + dateformat.format(date);
}
}
catch (Exception e){
System.out.println("Commen:getChineseDate:error");
throw e;
}
return res;
}
/**
*
*
* @param date
* @return String
*/
public String getChineseDateTime(java.util.Date date) throws Exception {
String res = "";
try{
if(date != null){
DateFormat dateformat = DateFormat.getDateInstance(
DateFormat.FULL, Locale.TAIWAN);
dateformat = new SimpleDateFormat("/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int nYear = cal.get(Calendar.YEAR) - 1911;
res = nYear + dateformat.format(date);
}
}
catch (Exception e){
System.out.println("Commen:getChineseDateTime:error");
throw e;
}
return res;
}
/**
*
*
* @return String
*/
public String getDate() throws Exception {
try{
DecimalFormat df = new DecimalFormat("00");
Calendar c = Calendar.getInstance();
int nYear = c.get(Calendar.YEAR);
int nMonth = c.get(Calendar.MONTH);
int nDay = c.get(Calendar.DAY_OF_MONTH);
return nYear + "/" + df.format(nMonth + 1) + "/" + df.format(nDay);
}
catch (Exception e){
System.out.println("Commen:getDate:error");
throw e;
}
}
/**
*
*
* @return String
*/
public String getDate(String pattern) throws Exception {
DecimalFormat df = new DecimalFormat("00");
Calendar c = Calendar.getInstance();
int nYear = c.get(Calendar.YEAR);
int nMonth = c.get(Calendar.MONTH);
int nDay = c.get(Calendar.DAY_OF_MONTH);
try{
if(pattern.equals("MM/DD")){
return df.format(nMonth + 1) + "/" + df.format(nDay);
}
else if(pattern.equals("-")){
return nYear + "-" + df.format(nMonth + 1) + "-" + df.format(nDay);
}
else{
return nYear + "/" + df.format(nMonth + 1) + "/" + df.format(nDay);
}
}
catch (Exception e){
System.out.println("Commen:getDate:error");
throw e;
}
}
/**
*
*
* @return String
*/
public String getTime() throws Exception {
try{
DecimalFormat df = new DecimalFormat("00");
Calendar c = Calendar.getInstance();
int nHour = c.get(Calendar.HOUR_OF_DAY);
int nMin = c.get(Calendar.MINUTE);
int nSec = c.get(Calendar.SECOND);
return df.format(nHour) + ":" + df.format(nMin) + ":" + df.format(nSec);
}
catch (Exception e){
System.out.println("Commen:getTime:error");
throw e;
}
}
/**
*
*
* @param src
* @param originalString
* @param newString
* @return String
*/
public String replaceAll(String src, String originalString, String newString) throws Exception {
try{
if(!src.trim().equals("")){
StringBuffer sb = new StringBuffer();
int index = 0;
do{
index = src.indexOf(originalString);
if(index == -1){
sb.append(src);
}
else{
sb.append(src.substring(0, index));
sb.append(newString);
src = src.substring(index + originalString.length());
}
}while (index != -1);
return sb.toString();
}
}
catch (Exception e){
System.out.println("Commen:replaceAll:error");
throw e;
}
return "";
}
/**
*
*
* @param dateA
* @param dateB
* @return String
*/
public String compareDate(String dateA, String dateB) throws Exception {
String desc = "";
String[] date1 = dateA.split("/");
String[] date2 = dateB.split("/");
Calendar a = Calendar.getInstance();
Calendar b = Calendar.getInstance();
try{
if(date1.length == 3 && date2.length == 3){
a.set(
Integer.parseInt(date1[0]), Integer.parseInt(date1[1]) - 1,
Integer.parseInt(date1[2]));
b.set(
Integer.parseInt(date2[0]), Integer.parseInt(date2[1]) - 1,
Integer.parseInt(date2[2]));
if(a.before(b)) desc = "before";
if(a.after(b)) desc = "after";
if(!a.before(b) && !a.after(b)) desc = "equal";
}
}
catch (Exception e){
System.out.println("Commen:compareDate:error");
throw e;
}
return desc;
}
/**
*
*
* @param val
* @param scale
* @return String
*/
public double round(double val, int scale) throws Exception {
// 濡傛灉鐐洪浂,鏈冨偝鍥炲師鏈
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -