您的位置:首页 > 编程语言 > Java开发

常用java bean 模块

2007-09-23 23:57 363 查看
出于实际应用的需要,本人边学习的过程边收集一些,常用的java bean以便日后用到的时候事半功倍。

(一)获取本地IP地址的Java Bean

这个比较简单:

..........................

String getLocalIpAddress()
{
String str = new String();
try
{
InetAddress inet = InetAddress.getLocalHost();//返回本地主机的IP地址
str = inet.getHostAddress();//返回IP地址的字符串
}
catch(UnknownHostException exp)
{
exp.printStackTrace();
}
return str;
}

...............................

这个方法主要用于获取本地IP地址。

(二)时间处理类--DateTimeUtility类

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

public class DateTimeUtility {

public static String getFormatedTime(Timestamp date) {
if (date == null) return null;
return getFormatedDateTime(date).substring(11);
}

public static String getFormatedDateTime(Timestamp date) {
if (date == null) return null;
SimpleDateFormat lFormatTimestamp = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return lFormatTimestamp.format(date);
}

public static Timestamp getDateTime(String date, String time, Timestamp defaultValue){

StringBuffer buf = new StringBuffer();
Timestamp current = defaultValue;
SimpleDateFormat lFormatDate = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat lFormatTime = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat lFormatTimestamp = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

if (isEmpty(date) && isEmpty(time)){
return defaultValue;
}
if (defaultValue == null) current = getCurrentTimeStamp();
if (isEmpty(date)) {
buf.append(lFormatDate.format(current));
} else {
buf.append(date);
}
buf.append(" ");
if (isEmpty(time)) {
buf.append(lFormatTime.format(current));
} else {
buf.append(time);
}
Timestamp rtn = null;
try {
rtn =new Timestamp(lFormatTimestamp.parse(buf.toString()).getTime());
} catch (Exception e) {
}
return rtn;
}

public static String getCurTimeStamp()
{
SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat();
lSimpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
java.sql.Timestamp lTime = (java.sql.Timestamp.valueOf(lSimpleDateFormat.format(new java.util.Date())));
return getFormatedDateTime(lTime);
}

public static Timestamp getCurrentTimeStamp()
{
SimpleDateFormat lSimpleDateFormat = new SimpleDateFormat();
lSimpleDateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
return (java.sql.Timestamp.valueOf(lSimpleDateFormat.format(new java.util.Date())));
}

public static boolean isEmpty(String string)
{
boolean isEmpty = true;
if (string != null && !"".equals(string) && string.trim().length() > 0)
isEmpty = false;
return isEmpty;
}
}

其中,getCurTimesStamp() 用来获取当前时间,输出类型是字串。SimpleDateFormate是用来日期时间格式化

的类,函数中applyPattern()用来表示返回的格式,这里是"yyyy-MM-dd HH:mm:ss"格。java.util.Date()得到当前时

间。GetFormateDateTime函数将Timestamp类型转化为 String类型。

知识点:

1、timestamp:时间戳

TIMESTAMP(数字时间戳)。服务开展成功的电子商务应用,要求交易结果对于参与双方应该是有约束力的,参与方不能否认其行为。这其中需要在经过数字签名的交易上打上一个可信赖的时间戳,从而解决一系列的实际和法律问题。

2、sql.Date //只有日期,不包含时间
sql.Time //只有时间,不包含日期
sql.Timestamp //包括日期和时间

这三个主要是在Statement ResultSet之中使用的,

util.Date也是包含日期和时间,但是这个并不是为数据库操作使用的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: