全部都 是自己写的。转载请注明。
package bbs.util;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 其它工具类
*
* @author mfkwfc
*
*/
public class Other {
/**
* 返回一个32位的UUID
*
* @return
*/
public final static String getUUID() {
return String.valueOf(UUID.randomUUID()).replace("-", "");
}
/**
* 把时间转换成字符串
*
* @param date
* @return
*/
public final static String convertDate(Date date) {
String value = "";
if (date != null) {
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
value = sim.format(date);
}
return value;
}
/**
* 计算时间差。返回分钟数
*
* @param max
* 最大时间
* @param min
* 最小时间
* @return
*/
public final static int dateDifference(Date max, Date min) {
int count = 0;
if (max != null && min != null) {
try {
long diff = max.getTime() - min.getTime();
count = (int) (diff / (1000 * 60));
} catch (Exception e) {
e.printStackTrace();
}
}
return count;
}
/**
* 用来判断是否是数字
*/
public final static boolean judgeFigure(String temp){
return temp.matches("[\\d]*");
}
/**
* 判断时间
*
* @param max
* @param min
* @return
*/
public final static String dateFront(Date max, Date min) {
int count = Other.dateDifference(max, min);
String fd = "";
if (max != null && min != null) {
if (count >= 0 && count <= 2)
fd = "刚刚";
else if (count > 2 && count <= 30) {
fd = count + " 分钟前";
} else if (count > 30 && count <= 60) {
fd = "半个小时前";
} else if (count > 60 && count <= 24 * 60) {
fd = Integer.valueOf(count / 60) + "小时前";
} else if (count > 24 * 60 && count < 10 * 24 * 60) {
fd = Integer.valueOf(count / 60 / 24) + "天前";
} else {
fd = Other.convertDate(min);
}
}
return fd;
}
/**
* 换时间转换为字符串。按传过来的格式转
*
* @param date
* @param cls
* @return
*/
public final static String convertDate(Date date, String cls) {
String value = "";
if (date != null && cls != null && cls.trim().length() != 0) {
SimpleDateFormat sim = new SimpleDateFormat(cls);
value = sim.format(date);
}
return value;
}
/**
* 换字符串强制转换为时间
*
* @param value
* @return
*/
public final static Date convertDate(String value) {
Date date = null;
if (value != null && value.trim().length() != 0) {
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd hh:mm");
try {
date = sim.parse(value);
} catch (ParseException e) {
System.out.println("转换时间出现错误");
e.printStackTrace();
}
}
return date;
}
/**
* 将字符串强制转换为时间类型 并且传过转换的样式 如:yyyy-MM-dd hh:mm
*
* @param value
* @param cls
* @return
*/
public final static Date convertDate(String value, String cls) {
Date date = null;
if (value != null && value.trim().length() != 0 && cls != null
&& cls.trim().length() != 0) {
SimpleDateFormat sim = new SimpleDateFormat(cls);
try {
date = sim.parse(value);
} catch (ParseException e) {
System.out.println("转换时间出现错误");
e.printStackTrace();
}
}
return date;
}
/**
* 时间减法
* @param oldDate
* @param i
* @return
*/
public final static Date DateAdd(Date date, int i) {
i-=i*2;
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, i);// 在天上加i
return calendar.getTime();
}
/**
* 给图片添加文字水印
*
* @param filePath
* 需要添加水印的图片的路径
* @param markContent
* 水印的文字
* @param markContentColor
* 水印文字的颜色
* @param qualNum
* 图片质量
* @return
*/
public final static boolean setCharacters(String filePath,
String markContent, Color markContentColor, float qualNum) {
ImageIcon imgIcon = new ImageIcon(filePath);
Image theImg = imgIcon.getImage();
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);
g.drawImage(theImg, 0, 0, null);
g.drawString(markContent, width / 14, height / 12); // 添加水印的文字和设置水印文字出现的内容
g.dispose();
try {
FileOutputStream out = new FileOutputStream(filePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);
param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
return false;
}
return true;
}
/**
* 给图片添加图片水印
*
* @param pressImg
* -- 水印文件
* @param targetImg
* --
*/
public final static void pressImage(String pressImg, String targetImg) {
try {
// 目标文件
File _file = new File(targetImg);
Image src = ImageIO.read(_file);
int wideth = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src, 0, 0, wideth, height, null);
// 水印文件
File _filebiao = new File(pressImg);
Image src_biao = ImageIO.read(_filebiao);
int wideth_biao = src_biao.getWidth(null);
int height_biao = src_biao.getHeight(null);
g.drawImage(src_biao, (wideth - wideth_biao),
(height - height_biao), wideth_biao, height_biao, null);
// 水印文件结束
g.dispose();
FileOutputStream out = new FileOutputStream(targetImg);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}