邮件系统中的CommonUtil

/*
 * 首都之窗————邮件订阅系统
 * 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"+"/">&lt;根&gt;"+"</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("&", "&amp;");
      text = text.replaceAll("/"", "&quot;");
      text = text.replaceAll("<", "&lt;");
      text = text.replaceAll(">", "&gt;");
     }
     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);
 }

}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、 4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.m或d论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值