学生管理系统CommonUtil类

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) {
  
 }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值