java中经常会用到的时间转换方法,在此记录,拿来即用
以下代码如无特殊说明format格式为一下几种中的一种,转换格式具体自己定义
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DEFAULT_DATETIME_FORMAT2 = "yyyy年MM月dd日 HH:mm:ss";
/**
* 获取时间戳
*
* @return
*/
public static long getTimeStamp() {
long tiemstamp = System.currentTimeMillis();
return tiemstamp;
}
/**
* 获取当前日期 字符串
* @param DATEFORMAT
* 日期格式字符串 如: yyyy-MM-dd
* @return
*/
public static String getNowDateStr(String DATEFORMAT) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(DATEFORMAT);
String nowdate = formatter.format(date);
return nowdate;
}
/**
* HH:mm 格式时间 转换为 HHmm格式
*
* @param timeStr
* @return
*/
public static String switchTime(String timeStr) {
String time = "";
if (timeStr != null && timeStr.length() > 4) {
String hour = timeStr.substring(0, 2);
String minutes = timeStr.substring(3, 5);
time = hour + minutes;
}
return time;
}
/**
* 字符串转时间Str to Date
*
* @param dstr
* @return
*/
public static Date StringToDate(String dstr, String DATEFORMAT) {
Date mdate = new Date();
SimpleDateFormat sd = new SimpleDateFormat(DATEFORMAT);// MM-dd
try {
mdate = sd.parse(dstr);
} catch (ParseException e) {
System.out.println("输入的日期格式有误!");
}
return mdate;
}
/**
*
* @param datestr
* 时间字符串
* @param mm
* 分钟数
* @param DATEFORMAT
* 格式
* @return mm分钟前的时间 Str
*/
public static String getDate_Desmm(String datestr, int mm, String DATEFORMAT) {
SimpleDateFormat formatter = new SimpleDateFormat(DATEFORMAT);
Date curDate = StringToDate(datestr, DATEFORMAT);
Date date = new Date(curDate.getTime() - mm * 60 * 1000);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String mdate = formatter.format(date);
return mdate;
}
/**
*
* @param datestr
* 时间字符串
* @param mm
* 分钟数
* @param DATEFORMAT
* 格式
* @return mm分钟后的时间 Str
*/
public static String getDate_Addmm(String datestr, int mm, String DATEFORMAT) {
SimpleDateFormat formatter = new SimpleDateFormat(DATEFORMAT);
Date curDate = StringToDate(datestr, DATEFORMAT);
Date date = new Date(curDate.getTime() + mm * 60 * 1000);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
String mdate = formatter.format(date);
return mdate;
}
/**
* 时间戳转时间字符串
*
* @param timestampString
* @param DATEFORMAT
* @return
*/
public static String TimeStamp2DateStr(String timestampString,
String DATEFORMAT) {
Long timestamp = Long.parseLong(timestampString);
String date = new java.text.SimpleDateFormat(DATEFORMAT)
.format(new java.util.Date(timestamp));
return date;
}
/**
* 时间戳转日期
*
* @param timestampString
* @param DATEFORMAT
* @return
*/
public static Date TimeStamp2Date(String timestampString, String DATEFORMAT) {
Long timestamp = Long.parseLong(timestampString);
Date date = new Date(timestamp);// 获取当前时间
return date;
}
/**
* 判断date1是否小于date2
*
* @param date1
* @param date2
*/
public static boolean DateCompareSmall(Date date1, Date date2) {
boolean result = date1.before(date2);
return result;
}
/**
* 判断date1是否大于date2
*
* @param date1
* @param date2
*/
public static boolean DateCompareBig(Date date1, Date date2) {
boolean result = date1.after(date2);
return result;
}