学生管理系统CommonUtil类

来源:互联网 发布:建筑光照分析软件 编辑:程序博客网 时间:2024/06/05 19:06
package jsp.utils;import java.io.UnsupportedEncodingException;import java.sql.SQLException;/** * Description:通用工具类 *  * Copyright: Copyright (c) 2010 *  * Company: JSP课程开发组 *  * @author zhang_jian *  * @checker *  * @version 1.0 *  * Created at 2010-10-12 *  * Modified by */public class CommonUtil {  /**  * 根据当前日期获取学期值,每年8月到次年1月为第1学期,每年2月到当年7月为第2学期  * @return 学期值,格式:yyyy-yyyy-X  */ public static String getCurrentTermValue(){  String str = "";  String currentDate = CommonUtil.getCurrentDate();  int currentYear = Integer.parseInt(currentDate.substring(0,4));  int currentMonth = Integer.parseInt(currentDate.substring(5,7));  if(currentMonth==1)  {   str = String.valueOf(currentYear-1) +"-" + String.valueOf(currentYear) + "-01" ;  }  if(currentMonth>=8)  {   str = String.valueOf(currentYear) +"-" + String.valueOf(currentYear+1) + "-01" ;  }  if(currentMonth<=7&¤tMonth>=2)  {   str = String.valueOf(currentYear-1) +"-" + String.valueOf(currentYear) + "-02" ;  }    return str; }  /**  * 将字符串转换为GBK编码  * @param source 待转换的字符串  * @return   */ public static String chageEncode2GBK(String source){  String str = "";  try {   str = new String(source.getBytes("ISO-8859-1"),"GBK");  } catch (UnsupportedEncodingException e) {   System.out.println("编码转换失败!");   e.printStackTrace();  }  return str; } /**  * 用“0”从左侧补充字符串,使字符串长度为length;若字符串长度大于length,则截取  * @param value  待补充的字符串  * @param length 目标长度  * @return     */ public static String leftFillZero2Lenght(String value,int length){  String str = "";  if(value.length()>length)   str = value.substring(0,length);  else{   for(int i=0;i<length-value.length();i++)    str += "0";   str += value;  }  return str; } /**  * 根据sql查询是否在数据库中存在记录  * @param sql 待查询sql语句  * @return true_记录已存在 false_记录不存在  */ public static boolean isExistInDataBase(String sql){  DBUtil dbUtil = new DBUtil();    try {   java.sql.ResultSet rs = dbUtil.dbQuery(sql);   if(rs.next())    return true;  } catch (SQLException e) {   e.printStackTrace();  }   dbUtil.dbClose();  return false; }  /**  * 获取当前日期  * @return 格式为 yyyy-MM-dd  */ public static String getCurrentDate(){  String currentDate = "";  java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");  currentDate = sdf.format(new java.util.Date());  return currentDate; }  /**  * 获取当前日期时间  * @return 格式为 yyyy-MM-dd HH:mm:ss  */ public static String getCurrentDateTime(){  String currentDateTime = "";  java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  currentDateTime = sdf.format(new java.util.Date());  return currentDateTime; }   public static void main(String[] args) {   }}