常用方法

——(字符、日期、数据转换、日期比较等等)

作者:ninjar 转自:JR


/**
* 比较当前日期和指定日期 return boolean
* 如果当前日期在指定日期之前返回true否则返回flase
*/
public static boolean dateCompare(String str){
boolean bea = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String isDate = sdf.format(new java.util.Date());
java.util.Date date1;
java.util.Date date0;
try {
date1 = sdf.parse(str);
date0= sdf.parse(isDate);
if(date0.after(date1)){
bea = true;
}
} catch (ParseException e) {
bea = false;
}
/**
* 将字符串转换成日期
* @param str
* @return
*/
public static Timestamp getStrToDate(String str){
Date date = null;
try{
date = java.sql.Date.valueOf(str);
}catch(Exception e){

}
return new Timestamp(date.getTime());
}
/**
* 输出字符串
* @param obj
* @return
*/
public static String getStr(Object obj){
if(obj==null) return "";
return obj.toString();
}

/**
* 将日期转换成字符串
* @param date
* @return
*/
public static String getDateToStr(Date date)
{
if(date == null){
return "";
}
SimpleDateFormat sdate= new SimpleDateFormat ("yyyy-MM-dd");
return sdate.format(date);
}
/**
* @return Timestamp
*/
public static Timestamp now()
{
java.util.Date now = new java.util.Date();

String year = ""+(now.getYear()+1900);
String month = "0"+(now.getMonth()+1);
String day = "0"+now.getDate();
String hour = "0"+now.getHours();
String minute= "0"+now.getMinutes();
String second = "0"+now.getSeconds();

month = month.substring(month.length()-2,month.length());
day = day.substring(day.length()-2,day.length());
hour = hour.substring(hour.length()-2,hour.length());
minute = minute.substring(minute.length()-2,minute.length());
second = second.substring(second.length()-2,second.length());
try
{
return datetimeToTimestamp(""+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second);
}
catch(GeneralException e)
{
e.printError();
return null;
}
}
/**
* 将字符串转换成日期
* @param str
* @return
*/
public static Timestamp getStrToDate(String str){
Date date = null;
try{
date = java.sql.Date.valueOf(str);
}catch(Exception e){

}
return new Timestamp(date.getTime());
}

/**
* 比较当前日期和指定日期 return boolean
* 如果当前日期在指定日期之前返回true否则返回flase
*/
public static boolean dateCompare(String str){
boolean bea = false;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String isDate = sdf.format(new java.util.Date());
java.util.Date date1;
java.util.Date date0;
try {
date1 = sdf.parse(str);
date0= sdf.parse(isDate);
if(date0.after(date1)){
bea = true;
}
} catch (ParseException e) {
bea = false;
}
return bea;
}
























































































































你可能感兴趣的:(java,sql)