/*
* 首都之窗————邮件订阅系统
* Created on 2005-12-2
* Author fhp
* 功能描述————通用的方法
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.trs.sdzc.mail.util;
/**
* <p>Title: 通用方法类</p>
* <p>Description: 常用的字符串及数字处理方法</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Author: fhp</p>
* @author not attributable
* @version 1.0
*/
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommonUtil {
public static SimpleDateFormat m_sdf = new SimpleDateFormat("yyyy-MM-dd");
/**
* 不能实例化
*/
private CommonUtil() {}
/**
* 生成随机数
* @param sLen
* @return String
*/
public static String randomKey(int sLen) {
String base;
String temp;
int i;
int p;
base = "1234567890";
temp = "";
for (i = 1; i < sLen; i++)
{
p = (int) (Math.random() * 10);
temp += base.substring(p, p + 1);
}
return (temp);
}
/**
* 生成随机号
* @param sLen
* @return String
*/
public static String ConfirmId(int sLen) {
String base;
String temp;
int i;
int p;
base = "1234567890abcdefghijklmnopqrstuvwxyz";
temp = "";
for (i = 0; i < sLen; i++)
{
p = (int) (Math.random() * 37);
if ( p > 35 ) p = 35;
temp += base.substring(p, p + 1);
}
return (temp);
}
/**
*
* @return
*/
public static String generateBookingId(){
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DATE);
StringBuffer s = new StringBuffer().append(String.valueOf(year).substring(2));
s = month<10?s.append("0").append(String.valueOf(month)):s.append(String.valueOf(month));
s = s.append(String.valueOf(day));
s = s.append(randomKey(5));
String sBookingId = s.toString();
return sBookingId;
}
/**
* 字符串转化成日期类型
* @param _Date
* @return
* @throws Exception
*/
public static Date StringToDate(String _Date) throws Exception{
try{
return m_sdf.parse(_Date);
}catch(Exception e){
throw(e);
}
}
/**
* 替换SQL语句中的'
* @param s
* @return
*/
public static String quote(String s) {
return "'" + s.replaceAll("'", "''") + "'";
}
/**
* 根据传入起止时间返回包含时间的list,周六周日除外
* @param _startDate
* @param _endDate
* @return
*/
public static List getDateList(GregorianCalendar _startDate, GregorianCalendar _endDate) {
if ( _startDate == null || _endDate == null ) {
return null;
}
if ( _startDate.after(_endDate) ) {
return null;
}
List retList = new ArrayList();
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
for (; !_startDate.after(_endDate);) {
//if ( getDayNumOfWeek( df.format(_startDate.getTime()) ) != 1 && getDayNumOfWeek( df.format(_startDate.getTime()) ) != 7 ) {
retList.add(df.format(_startDate.getTime()));
//}
_startDate.add(GregorianCalendar.DATE, 1);
/**
if ( !(_startDate.before(_endDate)) ) {
if ( getDayNumOfWeek( df.format(_startDate.getTime()) ) != 1 && getDayNumOfWeek( df.format(_startDate.getTime()) ) != 7 ) {
retList.add(df.format(_startDate.getTime()));
}
}
**/
}
return retList;
}
/**
* 计算星期几的函数
* @param _sYMD
* @return int 1~7代表星期日到星期六
*/
public static int getDayNumOfWeek(String _sYMD){
int nYear = parseInt( _sYMD.substring( 0, 4 ) );
int nMonth = parseInt( _sYMD.substring(5, 7 ) );
int nDay = parseInt( _sYMD.substring( 8, 10) );
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getDefault());
cal.clear();
cal.set(nYear, nMonth - 1, nDay);
return cal.get(cal.DAY_OF_WEEK);
}
/**
* 计算星期几的函数
* @param year int
* @param month int
* @param date int
* @return int 1~7代表星期日到星期六
*/
public static int getDayNumOfWeek(int year, int month, int date){
Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getDefault());
cal.clear();
cal.set(year, month - 1, date);
return cal.get(cal.DAY_OF_WEEK);
}
/**
* 根据当前日期及增加天数得到相应日期
* @param s
* @param n
* @return
* @throws Exception
*/
public static String addDay(String s,int n) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cd = Calendar.getInstance();
cd.setTime(sdf.parse(s));
cd.add(Calendar.DATE, n);
return sdf.format(cd.getTime());
}
/**
* 格式化日期
* @param pattern 日期格式
* @param date 日期对象
* @return
*/
public static String dateFormat(String pattern, java.util.Date date) {
SimpleDateFormat simpledateformat = new SimpleDateFormat(pattern);
return simpledateformat.format(date);
}
/**
* 是否合法日期
* @param strDate
* @return
*/
public static boolean isValdateDate(String strDate) {
boolean isPassed = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.setLenient( false );
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//format.setLenient(false);
if (strDate != null && strDate.length() > 0) {
try {
Date dtCheck = (Date)(sdf.parse(strDate));
String strCheck = sdf.format(dtCheck);
if(strDate.equals(strCheck)) {
isPassed = true;
} else {
isPassed = false;
}
} catch (Exception e) {
isPassed = false;
}
}
return isPassed;
}
/**
* 无异常的parseInt方法
* @param s
* @return
*/
public static int parseInt(String s) {
if (s == null) {
return 0;
}
try {
return Integer.parseInt(s);
}
catch (NumberFormatException nfe) {
return 0;
}
}
/**
* 无异常的parseInt方法
* @param n
* @return
*/
public static int parseInt(Integer n) {
if (n == null) {
return 0;
}
try {
return Integer.parseInt(n.toString());
} catch (NumberFormatException nfe) {
return 0;
}
}
/**
* 无异常的parseLong方法
* @param s
* @return
*/
public static long parseLong(String s) {
if (s == null) {
return 0;
}
try {
return Long.parseLong(s);
} catch (NumberFormatException nfe) {
return 0;
}
}
/**
* 无异常的parseFloat方法
* @param s
* @return
*/
public static float parseFloat(String s) {
if (s == null) {
return 0;
}
try {
return Float.parseFloat(s);
} catch (NumberFormatException nfe) {
return 0;
}
}
/**
* 回车符转换为<br>
* @param text
* @return
*/
public static String nb2br(String text) {
return text.replaceAll("/n", "<br>");
}
/**
* 根据List对象生成<select>控件
* @param l list
* @param formFieldName form中的名称
* @param defaultValue 默认值
* @return
*/
public static String genSelect(List lis, String formFieldName,
Object defaultValue) {
StringBuffer buf = new StringBuffer();
buf.append("<select name=/"" + formFieldName + "/">");
for (int i = 0; i < lis.size(); i++) {
boolean selected = false;
HashMap row = (HashMap) lis.get(i);
Object key = row.keySet().iterator().next();
Object value = row.get(key);
if (key.toString().equals(defaultValue)) {
selected = true;
}
buf.append("<option value=/"" + key + "/"");
if (selected) {
buf.append(" selected");
}
buf.append(">" + value + "</option>");
}
buf.append("</select>");
return buf.toString();
}
/**
* 根据List对象生成<select>控件
* @param l list
* @param formFieldName form中的名称
* @param defaultValue 默认值
* @return
*/
public static String rootSelect(List lis, String formFieldName,
Object defaultValue) {
StringBuffer buf = new StringBuffer();
buf.append("<select name=/"" + formFieldName + "/">");
buf.append("<option value=/"0"+"/"><根>"+"</option>");
for (int i = 0; i < lis.size(); i++) {
boolean selected = false;
HashMap row = (HashMap) lis.get(i);
Object key = row.keySet().iterator().next();
Object value = row.get(key);
if (key.equals(defaultValue)) {
selected = true;
}
buf.append("<option value=/"" + key + "/"");
if (selected) {
buf.append(" selected");
}
buf.append(">" + value + "</option>");
}
buf.append("</select>");
return buf.toString();
}
/**
* 将首字母改成大写
* @param str
* @return
*/
public static String uppercaseFirstChar(String str) {
str = str.substring(0, 1).toUpperCase() + str.substring(1);
return str;
}
/**
* 替换html中的特殊字符
* @param text
* @return
*/
public static String htmlSpecialChars(String text) {
if (text != null) {
text = text.replaceAll("&", "&");
text = text.replaceAll("/"", """);
text = text.replaceAll("<", "<");
text = text.replaceAll(">", ">");
}
return text;
}
/**
* 替换字符串中的/
* @param text
* @return
*/
public static String addSlash(String text) {
if (text != null) {
text = text.replaceAll("///"", "/"")
.replaceAll("///r///n", "rn");
}
return text;
}
/**
* 将回车转换为<br>
* @param text
* @return
*/
public static String nl2br(String text) {
return text.replaceAll("/r/n", "<br>");
}
/**
* 电子邮件地址格式验证
* @param mail
* @return true验证成功 false验证失败
*/
public static boolean validateEmail ( String mail ) {
Pattern p = null;
if( mail == null ) {
return false;
}
if ( mail.length() > 50 ) {
return false;
}
if( p == null ) {
p = Pattern.compile("^[_a-zA-Z0-9]+(//.[_a-zA-Z0-9]+)*@[a-zA-Z0-9_-]+(//.[a-z0-9A-Z-_]+)+$");
}
Matcher m= p.matcher(mail);
return m.matches();
}
/**
* 数字验证
* @param number
* @return true验证成功 false验证失败
*/
public static boolean validateNumber ( String number ) {
Pattern p = null;
if( number == null ) {
return false;
}
if( p == null ) {
p = Pattern.compile("[0-9]{1,10}");
}
Matcher m= p.matcher(number);
return m.matches();
}
/**
* 数字验证
* @param str
* @param num
* @return true验证成功 false验证失败
*/
public static boolean validateStr ( String str, int num ) {
Pattern p = null;
if( str == null ) {
return false;
}
if( p == null ) {
p = Pattern.compile("[a-z0-9]{"+num+"}");
}
Matcher m= p.matcher(str);
return m.matches();
}
/**
* 是否订阅期刊验证(0不订阅1订阅)/频道是否可订阅(0不可订阅1可以订阅)
* @param IsBooking
* @return true验证成功 false验证失败
*/
public static boolean validateIsBooking ( String IsBooking ) {
if( IsBooking == null ) {
return false;
}
if ( !"0".equals(IsBooking) && !"1".equals(IsBooking) ) {
return false;
}
return true;
}
/**
* 频道ID号/站点ID号合法性验证
* @param IsBooking
* @return true验证成功 false验证失败
*/
public static boolean validateNo ( String[] checkedChn ) {
if( checkedChn == null ) {
return false;
}
if ( (checkedChn.length < 1) ) {
return false;
}
for ( int i = 0; i < checkedChn.length; i ++ ) {
if ( !validateNumber(checkedChn[i]) ) {
return false;
}
}
return true;
}
/**
* 字符串长度验证
* @param str
* @param minLen
* @param maxLen
* @return true验证成功 false验证失败
*/
public static boolean validateLen ( String str, int minLen, int maxLen ) {
if ( str == null ) {
return false;
}
if ( str.length() < minLen ) {
return false;
}
if ( str.length() > maxLen ) {
return false;
}
return true;
}
/**
*
* @param strKey
* @return
*/
public static String getPropertiesPath() {
java.util.ResourceBundle rb = java.util.ResourceBundle.getBundle("com.trs.sdzc.mail.util.message");
return rb.getString("PropertiesPath");
}
/**
* 字符串转换成日期(date)
* @param str
* @return
* @throws Exception
*/
public static Calendar strToCalendar ( String str ) throws Exception {
Date d = new Date();
try{
d = formatter.parse(str);
}catch(Exception e){
throw(e);
}
Calendar c = Calendar.getInstance();
c.setTime( d );
return c;
}
/**
* Calendar转换成"yyyy-MM-dd HH:mm:ss"
* @param c
* @return
* @throws Exception
*/
public static String clendarToStr ( Calendar c ) throws Exception {
String str = "";
Date d = c.getTime();
try {
str = formatter.format( d );
} catch ( Exception ex ) {
throw (ex);
}
return str;
}
/**
* 测试
* @param args
* @throws Exception
*/
public static void main(String[] args)throws Exception{
//System.out.println(generateBookingId());
//CommonUtil.isValdateDate("2005-11-31");
//CommonUtil.ConfirmId( 20);
//String temp = "2005-12";
//System.out.println(temp.substring( 5));
float f = (float)0.5;
float ff = 0.5f;
float fff = (float)0.50;
float f1 = 0.0f;
f1 += 1/4 + 2/4 + 3/4 + 6.0 + f;
System.out.println(f1);
}
}