Java DateUtil时间大全
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
* author zys
*/
public class DateUtil{
/**定义常量**/
public static final String DATE_JFP_STR="yyyyMM";
public static final String DATE_FULL_STR = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_SMALL_STR = "yyyy-MM-dd";
public static final String DATE_KEY_STR = "yyyyMMddHHmmss";
public static SimpleDateFormat sdf = new SimpleDateFormat(DATE_FULL_STR);
//获取年月日时
public static String getyyyyMMddHHmmss(){
SimpleDateFormat df = new SimpleDateFormat(DATE_KEY_STR);
return df.format(new Date());
}
/**
* 获取当天的开始时间
* @return
*/
private static Date getTodayStartTime() {
Calendar todayStart = Calendar.getInstance();
todayStart.set(Calendar.HOUR, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTime();
}
/**
* 获取当天的结束时间
* @return
*/
private static Date getTodayEndTime() {
Calendar todayEnd = Calendar.getInstance();
todayEnd.set(Calendar.HOUR, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTime();
}
/**
* yyyy-MM-dd HH:mm:ss
* 使用预设格式提取字符串日期
* @param strDate 日期字符串
* @return
*/
public static Date parse(String strDate) {
return parse(strDate,DATE_FULL_STR);
}
/**
* 使用用户格式提取字符串日期
* @param strDate 日期字符串
* @param pattern 日期格式
* @return
*/
public static Date parse(String strDate, String pattern) {
if(pattern==null||pattern.equals("")){
pattern =DATE_FULL_STR;
}
SimpleDateFormat df = new SimpleDateFormat(pattern);
try {
return df.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 获得几个月后的日期
*
* @param date 日期
* @param afterMonth 月数
* @return 日期Date
*/
public static Date getAfterMonth(Date date, int afterMonth) {
if (date == null)
date = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(java.util.Calendar.MONTH, afterMonth);
return cal.getTime();
}
/**
* 取得指定日期几天前的日期
*
* @param date 日期
* @param beforeDays 天数(大于0)
* @return 日期
*/
public static Date getBeforeDay(Date date, int beforeDays) {
if (date == null)
date = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(java.util.Calendar.DATE, 0 - Math.abs(beforeDays));
return cal.getTime();
}
/**
* 取得指定日期几天后的日期
*
* @param date 日期
* @param afterDays 天数
* @return 日期
*/
public static Date getAfterDay(Date date, int afterDays) {
if (date == null)
date = new Date();
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(java.util.Calendar.DATE, afterDays);
return cal.getTime();
}
/**
* 取得n分钟后的时间
*
* @param date 日期
* @param afterMins
* @return 时间Timestamp
*/
public static Date getAfterMinute(Date date, long afterMins) {
if (date == null)
date = new Date();
long longTime = date.getTime() + afterMins * 60 * 1000;
return new Date(longTime);
}
/**
* 取得n分钟之前的时间
*
* @param date 日期
* @param afterMins
* @return 时间Timestamp
*/
public static Date getBeforeMinute(Date date, long afterMins) {
if (date == null)
date = new Date();
long longTime = date.getTime() - afterMins * 60 * 1000;
return new Date(longTime);
}
/**
* 两个时间比较
* @param date
* @return
*/
public static int compareDateWithNow(Date date1){
Date date2 = new Date();
int rnum =date1.compareTo(date2);
return rnum;
}
/**
* 两个时间比较(时间戳比较)
* @param date
* @return
*/
public static int compareDateWithNow(long date1){
long date2 = dateToUnixTimestamp();
if(date1>date2){
return 1;
}else if(date1<date2){
return -1;
}else{
return 0;
}
}
/**
* 获取系统当前时间+生成5位随机数
* @return
*/
public static String getCurrentTime() {
SimpleDateFormat df = new SimpleDateFormat(DATE_KEY_STR);
// return df.format(new Date())+(int)((Math.random()*9+1)*10000);
return df.format(new Date());
}
/**
* 获取系统当前时间
* @return
*/
public static String getNowTime() {
SimpleDateFormat df = new SimpleDateFormat(DATE_FULL_STR);
return df.format(new Date());
}
/**
* 获取系统当前时间date类型
* @return
*/
public static Date getNowTimeDate() {
SimpleDateFormat df = new SimpleDateFormat(DATE_FULL_STR);
try {
return df.parse(df.format(new Date()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 根据自己格式获取系统当前时间
* @return
*/
public static String getNowTime(String type) {
SimpleDateFormat df = new SimpleDateFormat(type);
return df.format(new Date());
}
/**
* 将Date类型转换为字符串 yyyy-MM-dd HH:mm:ss
*
* @param Date
* @return String
*/
public static String formatDate(Date date) {
if (date == null) {
return "";
}
return sdf.format(date);
}
/**
* 获取系统当前计费期
* @return
*/
public static String getJFPTime() {
SimpleDateFormat df = new SimpleDateFormat(DATE_JFP_STR);
return df.format(new Date());
}
/**
* 将指定的日期转换成Unix时间戳
* @param String date 需要转换的日期 yyyy-MM-dd HH:mm:ss
* @return long 时间戳
*/
public static long dateToUnixTimestamp(String date) {
long timestamp = 0;
try {
timestamp = new SimpleDateFormat(DATE_FULL_STR).parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
/**
* 将指定的日期转换成Unix时间戳
* @param String date 需要转换的日期 yyyy-MM-dd
* @return long 时间戳
*/
public static long dateToUnixTimestamp(String date, String dateFormat) {
long timestamp = 0;
try {
timestamp = new SimpleDateFormat(dateFormat).parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
/**
* 将当前日期转换成Unix时间戳
* @return long 时间戳
*/
public static long dateToUnixTimestamp() {
long timestamp = new Date().getTime();
return timestamp;
}
/**
* 将Unix时间戳转换成日期
* @param long timestamp 时间戳
* @return String 日期字符串
*/
public static String unixTimestampToDate(long timestamp) {
SimpleDateFormat sd = new SimpleDateFormat(DATE_FULL_STR);
sd.setTimeZone(TimeZone.getTimeZone("GMT+8"));
return sd.format(new Date(timestamp));
}
/**
* 给时间加上几个小时
* @param day 当前时间 格式:yyyy-MM-dd HH:mm:ss
* @param hour 需要加的时间
* @return
*/
public static String addDateMinut(String day, int hour){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = format.parse(day);
} catch (Exception ex) {
ex.printStackTrace();
}
if (date == null)
return "";
System.out.println("front:" + format.format(date)); //显示输入的日期
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, hour);// 24小时制
date = cal.getTime();
System.out.println("after:" + format.format(date)); //显示更新后的日期
cal = null;
return format.format(date);
}
}
猜你喜欢
转载自zysnba.iteye.com/blog/2419235
今日推荐
周排行