java中惯用日期工具类
/**
* 此文件描述的是:常用工具转换类
*/
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* @author blaiu
*
*/
public class Common {
/**
* 此类描述的是:字符串与时间类型的转换
* @param date
* @return String类型,格式为:yyyy年MM月dd日 星期EEE HH时mm分ss秒
*/
public static String getStr1(Date date) {
DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 星期EEE HH时mm分ss秒");
String timeStr = dateFormat.format(date);
return timeStr;
}
public static String getStrShort(Date date) {
DateFormat dateFormat = new SimpleDateFormat("MM-dd");
String timeStr = dateFormat.format(date);
return timeStr;
}
/**
* 此类描述的是:字符串与时间类型的转换
* @param date
* @return String类型,格式为:yyyyMM
*/
public static String getStr2() {
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyyMM");
String timeStr = dateFormat.format(date);
return timeStr;
}
/**
* 此类描述的是:字符串与时间类型的转换
* @param date
* @return String类型,格式为:yyyy-MM-dd
*/
public static String getStr(Date date) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String timeStr = dateFormat.format(date);
return timeStr;
}
/**
* 此类描述的是:字符串与时间类型的转换
* @param time
* @return String类型,格式为:yyyy-MM-dd HH:mm:ss
*/
public static String getStr2(Date date) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeStr = dateFormat.format(date);
return timeStr;
}
/**
* 此类描述的是:字符串与时间类型的转换
* @param str
* @return Date类型,格式为:yyyy-MM-dd
*/
public static Date getDateShort(String str) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date formatDate = dateFormat.parse(str, pos);
return formatDate;
}
/**
* 此类描述的是:字符串与时间类型的转换
* @param str
* @return Date类型,格式为:yyyy-MM-dd HH:mm:ss
*/
public static Date getDateFull(String str) {
if(null==str || "".equals(str)) {
return null;
} else {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date formatDate = dateFormat.parse(str, pos);
System.out.println(formatDate.toString());
return formatDate;
}
}
/**
*
* @此方法描述的是:获得当前的时间 例如:Mon May 18 10:26:29 CST 2009
* @return
* Date
*/
public static Date getDate() {
Date date = new Date();
return date;
}
/**
*
* @此方法描述的是:获得当前的时间 例如:yyyy-MM-dd HH:mm:ss
* @return
* String
*/
public static String getFullTime() {
Calendar calendar = null;
SimpleDateFormat formatter = null;
calendar = new GregorianCalendar();
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return formatter.format(calendar.getTime());
}
/**
* 对给定字符进行 URL 解码
*
* @param value
* String
* @return String
*/
public static String decode(String value) {
String result = "";
if (!isEmpty(value)) {
try {
result = java.net.URLDecoder.decode(value, "UTF-8");
result = java.net.URLDecoder.decode(result, "UTF-8");
} catch (UnsupportedEncodingException ex) {
}
}
return result;
}
/**
* 对给定字符进行 URL 编码
*
* @param value
* String
* @return String
*/
public static String encode(String value) {
String result = "";
if (!isEmpty(value)) {
try {
result = java.net.URLEncoder.encode(value, "UTF-8");
result = java.net.URLEncoder.encode(result, "UTF-8");
} catch (UnsupportedEncodingException ex) {
}
}
return result;
}
/**
* 判断是否为空,为空返回true
*
* @param value
* String
* @return boolean
*/
public static boolean isEmpty(String value) {
if (value == null || value.trim().equals("") || value.trim().equals("null")) {
return true;
} else {
return false;
}
}
/**
*
* 此方法描述的是:时间转换
* @param num
* @return
* String
*/
public static String getClosedDate(String num){
if(null==num || "".equals(num)) {
return null;
} else {
Calendar calendar = new GregorianCalendar();
SimpleDateFormat formatter = null;
formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(num.equals("1")){
calendar.add(calendar.DAY_OF_YEAR, +1);
return formatter.format(calendar.getTime());
}else if(num.equals("2")){
calendar.add(calendar.DAY_OF_YEAR, +7);
return formatter.format(calendar.getTime());
}else if(num.equals("3")){
calendar.add(calendar.DAY_OF_YEAR, +30);
return formatter.format(calendar.getTime());
}else if(num.equals("4")){
calendar.add(calendar.MONTH, +3);
return formatter.format(calendar.getTime());
}else if(num.equals("5")){
calendar.add(calendar.MONTH, +6);
return formatter.format(calendar.getTime());
}else{
calendar.add(calendar.YEAR, +1);
return formatter.format(calendar.getTime());
}
}
}
public static String getWebInfPath(){
return new File(Common.class.getResource("/").getPath()).getParent();
}
public static String getUploadPath() {
return new File( new File( new File( new File(Common.class.getResource("/").getPath()).getParent()).getParent()).getParent()).getParent();
}
public static void main(String[] args) {
System.out.println(getWebInfPath());
}
}