Java DateUtil Time Daquan
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
* extract string date using preset format
* @param strDate date string
* @ return
*/
public static Date parse(String strDate) {
return parse(strDate,DATE_FULL_STR);
}
/**
* Extract string date using user format
* @param strDate date string
* @param pattern date format
* @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;
}
}
/**
* Get dates in months
*
* @param date date
* @param afterMonth month number
* @return date 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();
}
/**
* Get the date a few days after the specified date
*
* @param date date
* @ param afterDays number of days
* @return date
*/
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();
}
/**
* Get the time after n minutes
*
* @param date Date
* @param afterMins
* @return Time 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;
}
}
/**
* Get the current time of the system + generate a 5-digit random number
* @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());
}
/**
* Get the current system time
* @return
*/
public static String getNowTime() {
SimpleDateFormat df = new SimpleDateFormat(DATE_FULL_STR);
return df.format(new Date());
}
/**
* Get the current date type of the system
* @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;
}
/**
* Get the current system time according to your own format
* @return
*/
public static String getNowTime(String type) {
SimpleDateFormat df = new SimpleDateFormat(type);
return df.format(new Date());
}
/**
* Convert Date to string yyyy-MM-dd HH:mm:ss
*
* @param Date
* @return String
*/
public static String formatDate(Date date) {
if (date == null) {
return "";
}
return sdf.format(date);
}
/**
* Get the current billing period of the system
* @return
*/
public static String getJFPTime() {
SimpleDateFormat df = new SimpleDateFormat(DATE_JFP_STR);
return df.format(new Date());
}
/**
* Convert the specified date to a Unix timestamp
* @param String date needs to be converted date yyyy-MM-dd HH:mm:ss
* @return long timestamp
*/
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;
}
/**
* Convert the specified date to a Unix timestamp
* @param String date The date to be converted yyyy-MM-dd
* @return long timestamp
*/
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;
}
/**
* Convert current date to Unix timestamp
* @return long timestamp
*/
public static long dateToUnixTimestamp() {
long timestamp = new Date().getTime();
return timestamp;
}
/* *
* Convert Unix timestamp to date
* @param long timestamp timestamp
* @return String date 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));
}
/**
* Add a few hours to the time
* @param day Current time format: yyyy-MM-dd HH:mm:ss
* @param hour the time to be added
* @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)); //Display the entered date
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.HOUR, hour);// 24-hour
date = cal.getTime();
System.out.println("after:" + format.format(date)); //display the updated date
cal = null;
return format.format(date);
}
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326035089&siteId=291194637
Recommended
Ranking