Java常用工具类
1. jdbcutils2.JedisUtils3. DateUtils4.UuidUtils5.MailUtils(邮件工具类)6. Md5Utils
1. jdbcutils
package cn
.itcast
.util
;
import com
.alibaba
.druid
.pool
.DruidDataSourceFactory
;
import javax
.sql
.DataSource
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.sql
.Connection
;
import java
.sql
.SQLException
;
import java
.util
.Properties
;
public class JDBCUtils {
private static DataSource ds
;
static {
try {
Properties pro
= new Properties();
InputStream is
= JDBCUtils
.class.getClassLoader().getResourceAsStream("druid.properties");
pro
.load(is
);
ds
= DruidDataSourceFactory
.createDataSource(pro
);
} catch (IOException e
) {
e
.printStackTrace();
} catch (Exception e
) {
e
.printStackTrace();
}
}
public static DataSource
getDataSource(){
return ds
;
}
public static Connection
getConnection() throws SQLException
{
return ds
.getConnection();
}
}
2.JedisUtils
jedis.properties配置文件host=127.0.0.1port=6379maxTotal=50maxIdle=10
public class JedisPoolUtil {
private static JedisPool jedisPool
;
private static Jedis jedis
;
static{
InputStream rs
= JedisPoolUtil
.class.getClassLoader().getResourceAsStream("jedis.properties");
Properties pro
= new Properties();
try {
pro
.load(rs
);
} catch (IOException e
) {
e
.printStackTrace();
}
JedisPoolConfig config
= new JedisPoolConfig();
config
.setMaxIdle(Integer
.parseInt(pro
.getProperty("maxIdle")));
config
.setMaxTotal(Integer
.parseInt(pro
.getProperty("maxTotal")));
jedisPool
= new JedisPool(config
,"localhost",6379);
}
public static Jedis
getJedis(){
return jedisPool
.getResource();
}
public static void close(Jedis jedis
) {
if (jedis
!= null
) {
jedis
.close();
}
}
}
3. DateUtils
public class DateParserT {
public static void main(String
[] args
) {
Date dt
=new Date();
System
.out
.println(dt
);
String formatDate
= null
;
formatDate
= DateFormat
.getDateInstance().format(dt
);
System
.out
.println(formatDate
);
formatDate
= DateFormat
.getDateInstance(DateFormat
.FULL
).format(dt
);
System
.out
.println(formatDate
);
DateFormat dFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
formatDate
= dFormat
.format(dt
);
System
.out
.println(formatDate
);
DateFormat dFormat12
= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
formatDate
= dFormat12
.format(dt
);
System
.out
.println(formatDate
);
DateFormat dFormat3
= new SimpleDateFormat("yyyyMMddHHmmss");
formatDate
= dFormat3
.format(dt
);
System
.out
.println(formatDate
);
long lTime
= dt
.getTime() / 1000;
System
.out
.println(lTime
);
long ltime2
= 1464710394;
SimpleDateFormat lsdFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date lDate
= new Date(ltime2
*1000);
String lStrDate
= lsdFormat
.format(lDate
);
System
.out
.println(lStrDate
);
String strDate
= "2016-07-06 10:17:48";
SimpleDateFormat lsdStrFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date strD
= lsdStrFormat
.parse(strDate
);
System
.out
.println(strD
);
} catch (ParseException e
) {
e
.printStackTrace();
}
}
}
4.UuidUtils
public final class UuidUtil {
private UuidUtil(){}
public static String
getUuid(){
return UUID
.randomUUID().toString().replace("-","");
}
public static void main(String
[] args
) {
System
.out
.println(UuidUtil
.getUuid());
System
.out
.println(UuidUtil
.getUuid());
System
.out
.println(UuidUtil
.getUuid());
System
.out
.println(UuidUtil
.getUuid());
}
}
5.MailUtils(邮件工具类)
public final class MailUtils {
private static final String USER
= "";
private static final String PASSWORD
= "";
public static boolean sendMail(String to
, String text
, String title
){
try {
final Properties props
= new Properties();
props
.put("mail.smtp.auth", "true");
props
.put("mail.smtp.host", "smtp.qq.com");
props
.put("mail.user", USER
);
props
.put("mail.password", PASSWORD
);
Authenticator authenticator
= new Authenticator() {
@Override
protected PasswordAuthentication
getPasswordAuthentication() {
String userName
= props
.getProperty("mail.user");
String password
= props
.getProperty("mail.password");
return new PasswordAuthentication(userName
, password
);
}
};
Session mailSession
= Session
.getInstance(props
, authenticator
);
MimeMessage message
= new MimeMessage(mailSession
);
String username
= props
.getProperty("mail.user");
InternetAddress form
= new InternetAddress(username
);
message
.setFrom(form
);
InternetAddress toAddress
= new InternetAddress(to
);
message
.setRecipient(Message
.RecipientType
.TO
, toAddress
);
message
.setSubject(title
);
message
.setContent(text
, "text/html;charset=UTF-8");
Transport
.send(message
);
return true;
}catch (Exception e
){
e
.printStackTrace();
}
return false;
}
public static void main(String
[] args
) throws Exception
{
MailUtils
.sendMail("itcast_xian@163.com","你好,这是一封测试邮件,无需回复。","测试邮件");
System
.out
.println("发送成功");
}
}
6. Md5Utils
public final class Md5Util {
private Md5Util(){}
public static String
encodeByMd5(String password
) throws Exception
{
MessageDigest md5
= MessageDigest
.getInstance("MD5");
byte[] byteArray
= md5
.digest(password
.getBytes());
return byteArrayToHexString(byteArray
);
}
private static String
byteArrayToHexString(byte[] byteArray
) {
StringBuffer sb
= new StringBuffer();
for(byte b
: byteArray
){
String hex
= byteToHexString(b
);
sb
.append(hex
);
}
return sb
.toString();
}
private static String
byteToHexString(byte b
) {
int n
= b
;
if(n
< 0){
n
= 256 + n
;
}
int d1
= n
/ 16;
int d2
= n
% 16;
return hex
[d1
] + hex
[d2
];
}
private static String
[] hex
= {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
public static void main(String
[] args
) throws Exception
{
String password
= "123456";
String passwordMD5
= Md5Util
.encodeByMd5(password
);
System
.out
.println(password
);
System
.out
.println(passwordMD5
);
}
}