/*
* StringUtil.java
*
* Created on 2007-8-24, 23:51:57
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.common.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
*
* File Name:StringUtil.java
*
*
* Title:字符数字的操作的公用方法
*
*
* Description:
*
*
* Company:
*
*
* @author 徐建协
* @version 1.0 2006-7-10
*/
/**
*
* 作者:徐建协
*/
public class StringUtil {
/**
* 判断是否为闰年
*
* @param year
* int 年份的数字
* @return true 闰年 false 不为闰年
*/
public static boolean isLeapYear(int Year) {
boolean isLeapYear = false;
if (Year % 4 == 0 && Year % 100 != 0) {
isLeapYear = true;
return isLeapYear;// 闰年
}
if (Year % 400 == 0) { // 闰年
isLeapYear = true;
return isLeapYear;
}
return isLeapYear;
}
/**
* 获取当月的天数
*
* @param Year
* int 年份的数值 如2005
* @param Month
* int 月份的数值 从1开始,一月份为1,十二月份为12
* @return int 返回要查询的月份的天数
*/
public static int getDaysOfMonth(int Year, int Month) {
int days = 0;
switch (Month) {
case 1:
days = 31;
break;
case 2:
if (StringUtil.isLeapYear(Year)) {
days = 29;
} else {
days = 28;
}
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
}
return days;
}
/***************************************************************************
* 获取月份的开始日期
*
* @param Year
* String 年份的字符串
* @param Month
* String 月份的字符串
* @return String 月份的开始日期 如2005-12-01
*/
public static String getBeginDateOfMonth(String Year, String Month) {
String yearStr = "0000" + Year;
String monthStr = "00" + Month;
String dayStr = "01";
yearStr = yearStr.substring(yearStr.length() - 4, yearStr.length());
monthStr = monthStr.substring(monthStr.length() - 2, monthStr.length());
dayStr = dayStr.substring(dayStr.length() - 2, dayStr.length());
return yearStr + "-" + monthStr + "-" + dayStr;
}
/***************************************************************************
* 获取月份的开始日期
*
* @param Year
* int 年份的数字
* @param Month
* int 月份的数字
* @return String 月份的开始日期 如2005-12-01
*/
public static String getBeginDateOfMonth(int Year, int Month) {
String yearStr = "0000" + String.valueOf(Year);
String monthStr = "00" + String.valueOf(Month);
String dayStr = "01";
yearStr = yearStr.substring(yearStr.length() - 4, yearStr.length());
monthStr = monthStr.substring(monthStr.length() - 2, monthStr.length());
dayStr = dayStr.substring(dayStr.length() - 2, dayStr.length());
return yearStr + "-" + monthStr + "-" + dayStr;
}
/***************************************************************************
* 获取月底日期
*
* @param Year
* String 年份的字符串
* @param Month
* String 月份的字符串
* @return String 当月的月底日期 如2005-12-31
*/
public static String getEndDateOfMonth(String Year, String Month) {
String yearStr = "0000" + Year;
String monthStr = "00" + Month;
int day = StringUtil.getDaysOfMonth(Integer.parseInt(Year), Integer
.parseInt(Month));
String dayStr = "00" + String.valueOf(day);
yearStr = yearStr.substring(yearStr.length() - 4, yearStr.length());
monthStr = monthStr.substring(monthStr.length() - 2, monthStr.length());
dayStr = dayStr.substring(dayStr.length() - 2, dayStr.length());
return yearStr + "-" + monthStr + "-" + dayStr;
}
/***************************************************************************
* 获取月底日期
*
* @param Year
* int 年份的数字
* @param Month
* int 月份的数字
* @return String 月底日期 如2005-12-31
*/
public static String getEndDateOfMonth(int Year, int Month) {
String yearStr = "0000" + String.valueOf(Year);
String monthStr = "00" + String.valueOf(Month);
int day = StringUtil.getDaysOfMonth(Year, Month);
String dayStr = "00" + String.valueOf(day);
yearStr = yearStr.substring(yearStr.length() - 4, yearStr.length());
monthStr = monthStr.substring(monthStr.length() - 2, monthStr.length());
dayStr = dayStr.substring(dayStr.length() - 2, dayStr.length());
return yearStr + "-" + monthStr + "-" + dayStr;
}
/***************************************************************************
* 格式化数字
*
* @param numStr
* String 要格式话的数字字符串
* @param len
* int 小数点的位数
* @return String 格式化后的字符串
*/
public static String formatNumer(String numStr, int len) {
if (numStr.length()==0){
numStr="0";
}
BigDecimal bigDecimal=new BigDecimal(numStr);
bigDecimal=bigDecimal.setScale(len,BigDecimal.ROUND_HALF_UP);
return bigDecimal.toString();
}
/***************************************************************************
* 格式化数字
*
* @param num
* double 要格式化的数字
* @param len
* int 小数点的位数
* @return String 格式化后的字符串
*/
public static String formatNumer(double num, int len) {
return StringUtil.formatNumer(Double.toString(num), len);
}
/***************************************************************************
* 格式化数字
*
* @param num
* float 要格式化的数字
* @param len
* int 小数点的位数
* @return String 格式化后的字符串
*/
public static String formatNumer(float num, int len) {
return StringUtil.formatNumer(Double.toString(num), len);
}
/**
* 获取上个月份的日期
*
* @return int[0] 为年份 int[1]为月份
*/
public static int[] getPreMonthDate() {
int[] tmp = new int[2];
Calendar now = Calendar.getInstance();
now.add(Calendar.MONTH, -1);
tmp[0] = now.get(Calendar.YEAR)+1990;// 获取年份
tmp[1] = now.get(Calendar.MONTH)+1;// 获取月份
return tmp;
}
/**
* 获取当前年度
*
* @return String 4位的数字字符串
*/
public static String getCurYear() {
SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
Date date = new Date();
return formatYear.format(date);
}
/**
* 获取当前的月份
*
* @return String 2位的数字字符串
*/
public static String getCurMonth() {
SimpleDateFormat formatMonth = new SimpleDateFormat("MM");
Date date = new Date();
return formatMonth.format(date);
}
/**
* 获取当天所属月份的日期
*
* @return
*/
public static String getCurDay() {
SimpleDateFormat formatMonth = new SimpleDateFormat("dd");
Date date = new Date();
return formatMonth.format(date);
}
public static String getCurDate() {
SimpleDateFormat formatMonth = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
return formatMonth.format(date);
}
public static String formatDate(String src,String regex){
SimpleDateFormat df =new SimpleDateFormat("yyyyMMdd");
Date date=null;
try{
date=df.parse(src);
}catch(Exception ex){
}
df.applyPattern(regex);
return df.format(date);
}
public static String formatDate(Date date,String regex){
SimpleDateFormat df =new SimpleDateFormat("yyyyMMdd");
df.applyPattern(regex);
return df.format(date);
}
public static String formatDate(int src,String regex){
SimpleDateFormat df =new SimpleDateFormat("yyyyMMdd");
Date date=null;
try{
date=df.parse(String.valueOf(src));
}catch(Exception ex){
}
df.applyPattern(regex);
return df.format(date);
}
public static String formatDate(String src,String srcRegex,String destRegex){
SimpleDateFormat df =new SimpleDateFormat(srcRegex);
Date date=null;
try{
date=df.parse(src);
}catch(Exception ex){
}
df.applyPattern(destRegex);
return df.format(date);
}
public static String formatDate(int src,String srcRegex,String destRegex){
SimpleDateFormat df =new SimpleDateFormat(srcRegex);
Date date=null;
try{
date=df.parse(String.valueOf(src));
}catch(Exception ex){
}
df.applyPattern(destRegex);
return df.format(date);
}
public static String getCurDate(String regex){
SimpleDateFormat formatMonth = new SimpleDateFormat(regex);
Date date = new Date();
return formatMonth.format(date);
}
/**
* 从地址中获取文件类型
* @param url
* @return 返回文件类型的小写字母
*/
public static String getExt(String url){
String[] arrays=url.split(".");
if (arrays.length return null;
}else{
return arrays[arrays.length-1].toLowerCase();
}
}
public static String getSubDay(int days,String destRegex){
Calendar srcTime=Calendar.getInstance();
srcTime.add(Calendar.DATE, days);
SimpleDateFormat df =new SimpleDateFormat(destRegex);
df.applyPattern(destRegex);
return df.format(srcTime.getTime());
}
public static String Encode64System(String src ){
BigInteger i=new BigInteger(src);
String sEncodeCode="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
String sTel="";
int seq=0;
BigInteger i64=new BigInteger("64");
double iTelValue=0;
while (i.longValue()>=64){
seq=i.mod(i64).intValue();
sTel=sEncodeCode.substring(seq,seq+1)+sTel;
i=i.divide(i64);
}
seq=i.mod(i64).intValue();
sTel=sEncodeCode.substring(seq,seq+1)+sTel;
return sTel;
}
public static String getLimitLengthString(String src,int len, String symbol){
int counterOfDoubleByte;
byte b[];
counterOfDoubleByte = 0;
String res="";
try{
b = src.getBytes("GBK");
if(b.length return src;
for(int i = 0; i < len; i++){
if(b[i]< 0) counterOfDoubleByte++;
}
if(counterOfDoubleByte % 2 == 0)
res=new String(b, 0, len, "GBK") + symbol;
else
res=new String(b, 0, len - 1, "GBK") + symbol;
}catch(Exception ex ){
}
return res;
}
public static final String C15ToC18(String id){
final int [] W ={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
final String [] A ={"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
int i,j,s=0;
String newid;
newid = id;
newid =newid.substring(0,6)+"19"+newid.substring(6,id.length());
for ( i=0;i j=Integer.parseInt(newid.substring(i,i+1))*W[i];
s=s+j;
}
s = s % 11;
newid=newid+A[s];
return newid;
}
/***
* 随机生成GUID字符串
* @return
*/
public static String GetGUID(){
RandomGUID guid=new RandomGUID();
return guid.toString();
}
public static String addCharactar(String src,char defaultChar,int length){
String res="";
if( src.length()>=length){
res=src;
return res;
}
for(int i=0;i res+=defaultChar;
}
res+=src;
return res;
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// System.out.println(StringUtil.getBeginDateOfMonth(2005,3)+StringUtil.getEndDateOfMonth(2005,3));
// System.out.println(StringUtil.formatNumer("5555.44444",2));
// Date date=new Date();
// System.out.println(date.getMonth());
/*
* SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date
* date=formatter.parse("2005-01-01"); Calendar now =
* Calendar.getInstance(); now.setTime(date); System.out.println("It is
* now " + formatter.format(now.getTime())); now.add(Calendar.MONTH,-1);
* System.out.println(""); System.out.println("Two years ago was " +
* formatter.format(now.getTime()));
/*
* int[] tmp=StringUtil.getPreMonthDate(); System.out.println(tmp[0]+"
* "+tmp[1]);
*/
// System.out.println(Double.parseDouble(StringUtil.formatNumer(0.5499999,
// 2)));
// System.outprintln(StringUtil.formatDate("20061209","yyyy-MM-dd"));
// Date date=new Date();
/*System.out.println("begin...");
System.out.println(StringUtil.getExt("ddd.txt"));
int[] test=StringUtil.getPreMonthDate();
System.out.println(test[0]+","+test[1]);*/
/* double d=0.0015*0.0203;
double e=MathEx.mul(0.0015, 0.0203, 10);
System.out.println(Double.toString(d)+","+d+","+StringUtil.formatNumer(d, 9));
System.out.println(Double.toString(e)+","+e+","+StringUtil.formatNumer(e, 9));*/
//java.sql.Date date=java.sql.Date.valueOf("2006-09-10");
//System.out.println(StringUtil.formatDate("2006.09.12","yyyy.MM.dd","yyyy-MM-dd"));
//String src="dddd'''',,,''";
//System.out.print(src.replaceAll("[,']", ""));//
System.out.println(StringUtil.getLimitLengthString("1244测试的",7,"..."));
}
}