/**
* 時(shí)間、日期工具類
* Created by fanji on 2017/7/19.
*/
public class DateTimeUtil {
static SimpleDateFormat format;
/** 日期格式:yyyy-MM-dd HH:mm:ss **/
public static final String DF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/** 日期格式:yyyy-MM-dd HH:mm **/
public static final String DF_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
/** 日期格式:yyyy-MM-dd **/
public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
/** 日期格式:HH:mm:ss **/
public static final String DF_HH_MM_SS = "HH:mm:ss";
/** 日期格式:HH:mm **/
public static final String DF_HH_MM = "HH:mm";
private final static long minute = 60 * 1000;// 1分鐘
private final static long hour = 60 * minute;// 1小時(shí)
private final static long day = 24 * hour;// 1天
private final static long month = 31 * day;// 月
private final static long year = 12 * month;// 年
public DateTimeUtil() {
}
/**
* 將日期格式化成友好的字符串:幾分鐘前童社、幾小時(shí)前戳表、幾天前鄙煤、幾月前耻煤、幾年前具壮、剛剛
*
* @param date
* @return
*/
public static String formatFriendly(Date date) {
if (date == null) {
return null;
}
long diff = new Date().getTime() - date.getTime();
long r = 0;
if (diff > year) {
r = (diff / year);
return r + "年前";
}
if (diff > month) {
r = (diff / month);
return r + "個(gè)月前";
}
if (diff > day) {
r = (diff / day);
return r + "天前";
}
if (diff > hour) {
r = (diff / hour);
return r + "個(gè)小時(shí)前";
}
if (diff > minute) {
r = (diff / minute);
return r + "分鐘前";
}
return "剛剛";
}
/**
* 將日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL
* 日期
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(long dateL) {
SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date date = new Date(dateL);
return sdf.format(date);
}
/**
* 將日期以yyyy-MM-dd HH:mm:ss格式化
*
* @param dateL
* 日期
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(long dateL, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(new Date(dateL));
}
/**
* 將日期以yyyy-MM-dd HH:mm:ss格式化
* @param date 日期
* @param formater
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDateTime(Date date, String formater) {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(date);
}
/**
* 將日期字符串轉(zhuǎn)成日期
*
* @param strDate
* 字符串日期
* @return java.util.date日期類型
*/
@SuppressLint("SimpleDateFormat")
public static Date parseDate(String strDate) {
DateFormat dateFormat = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date returnDate = null;
try {
returnDate = dateFormat.parse(strDate);
} catch (ParseException e) {
}
return returnDate;
}
/**
* 獲取系統(tǒng)當(dāng)前日期
*
* @return
*/
public static Date gainCurrentDate() {
return new Date();
}
/**
* 驗(yàn)證日期是否比當(dāng)前日期早
*
* @param target1
* 比較時(shí)間1
* @param target2
* 比較時(shí)間2
* @return true 則代表target1比target2晚或等于target2,否則比target2早
*/
public static boolean compareDate(Date target1, Date target2) {
boolean flag = false;
try {
String target1DateTime = formatDateTime(target1, DF_YYYY_MM_DD_HH_MM_SS);
String target2DateTime = formatDateTime(target2, DF_YYYY_MM_DD_HH_MM_SS);
if (target1DateTime.compareTo(target2DateTime) <= 0) {
flag = true;
}
} catch (Exception e) {
System.out.println("比較失敗哈蝇,原因:" + e.getMessage());
}
return flag;
}
/**
* 對(duì)日期進(jìn)行增加操作
*
* @param target
* 需要進(jìn)行運(yùn)算的日期
* @param hour
* 小時(shí)
* @return
*/
public static Date addDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() + (long) (hour * 60 * 60 * 1000));
}
/**
* 對(duì)日期進(jìn)行相減操作
*
* @param target
* 需要進(jìn)行運(yùn)算的日期
* @param hour
* 小時(shí)
* @return
*/
public static Date subDateTime(Date target, double hour) {
if (null == target || hour < 0) {
return target;
}
return new Date(target.getTime() - (long) (hour * 60 * 60 * 1000));
}
/** 獲取系統(tǒng)時(shí)間的方法:月/日 時(shí):分:秒 */
public static String getFormateDate() {
Calendar calendar = Calendar.getInstance();
int month = (calendar.get(Calendar.MONTH) + 1);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
String systemTime = (month < 10 ? "0" + month : month) + "/" + (day < 10 ? "0" + day : day) + " " + (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute) + ":" + (second < 10 ? "0" + second : second);
return systemTime;
}
/** 獲取系統(tǒng)時(shí)間的方法:時(shí):分:秒 */
public static String getHourAndMinute() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
return (hour < 10 ? "0" + hour : hour) + ":" + (minute < 10 ? "0" + minute : minute);
}
/** 獲取系統(tǒng)時(shí)間的方法:時(shí) */
public static String getHour() {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
return ((hour < 10 ? "0" + hour : hour) + "");
}
/**
* 將2017-07-10 00:00:00 換2017-07-10
*
* @param strDate
* @return
*/
public static String strFormatStr(String strDate) {
if (strDate.equals("")) {
return "";
}
return dateToStr(strToDate(strDate));
}
/**
* 2015-01-07 15:05:34
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDateHHMMSS(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 2015-01-07
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 2015.01.07
*
* @param strDate
* @return
*/
@SuppressLint("SimpleDateFormat")
public static Date strToDateDorp(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
@SuppressLint("SimpleDateFormat")
public static String dateToStr(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
/** 傳入一個(gè)String轉(zhuǎn)化為long */
@SuppressLint("SimpleDateFormat")
public static Long stringParserLong(String param) throws ParseException {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(param).getTime();
}
/** 當(dāng)前時(shí)間轉(zhuǎn)換為long */
@SuppressLint("SimpleDateFormat")
public static Long currentDateParserLong() throws ParseException {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.parse(format.format(Calendar.getInstance().getTime())).getTime();
}
/** 當(dāng)前時(shí)間 如: 2013-04-22 10:37:00 */
@SuppressLint("SimpleDateFormat")
public static String getCurrentDate() {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return format.format(Calendar.getInstance().getTime());
}
/** 當(dāng)前時(shí)間 如: 10:37 */
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateHHMM() {
format = new SimpleDateFormat("HH:mm");
return format.format(Calendar.getInstance().getTime());
}
/**
* 當(dāng)前時(shí)間 如: 10:37
*
* @throws ParseException
*/
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateHHMMSS() {
format = new SimpleDateFormat("HH:mm:ss");
return format.format(Calendar.getInstance().getTime());
}
/** 當(dāng)前時(shí)間 如: 20130422 */
@SuppressLint("SimpleDateFormat")
public static String getCurrentDateString() {
format = new SimpleDateFormat("yyyyMMddHHmm");
return format.format(Calendar.getInstance().getTime());
}
/** 當(dāng)前時(shí)間 如: 2013-04-22 */
@SuppressLint("SimpleDateFormat")
public static String getCurrentTime() {
format = new SimpleDateFormat("yyyy-MM-dd");
return format.format(Calendar.getInstance().getTime());
}
@SuppressLint("SimpleDateFormat")
public static String getSWAHDate() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(Calendar.getInstance().getTime());
}
@SuppressLint("SimpleDateFormat")
public static Long stringToLongD(String param) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
return format.parse(param.substring(0, param.length() - 4)).getTime();
}
@SuppressLint("SimpleDateFormat")
public static Long stringToLong(String param) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
return format.parse(param).getTime();
}
/**
* 獲取兩個(gè)日期之間的間隔天數(shù)
*
* @return
*/
@SuppressLint("SimpleDateFormat")
public static int getGapCount(Date startDate, Date endDate) {
Calendar fromCalendar = Calendar.getInstance();
fromCalendar.setTime(startDate);
fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
fromCalendar.set(Calendar.MINUTE, 0);
fromCalendar.set(Calendar.SECOND, 0);
fromCalendar.set(Calendar.MILLISECOND, 0);
Calendar toCalendar = Calendar.getInstance();
toCalendar.setTime(endDate);
toCalendar.set(Calendar.HOUR_OF_DAY, 0);
toCalendar.set(Calendar.MINUTE, 0);
toCalendar.set(Calendar.SECOND, 0);
toCalendar.set(Calendar.MILLISECOND, 0);
return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24));
}
/**
* 日期轉(zhuǎn)換成Java字符串
*
* @param date
* @return str
*/
@SuppressLint("SimpleDateFormat")
public static String DateToStr(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);
return str;
}
/**
* 字符串轉(zhuǎn)換成日期
*
* @param str
* @return date
*/
@SuppressLint("SimpleDateFormat")
public static Date StrToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 字符串轉(zhuǎn)換成日期
*
* @param str
* @return date
*/
@SuppressLint("SimpleDateFormat")
public static Date StrToDateDrop(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
Date date = null;
try {
date = format.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
*
* @param time
* @return
*/
@SuppressLint("SimpleDateFormat")
public static long getLongTime(String time) {
long ct = 0;
try {
format = new SimpleDateFormat("HH:mm:ss");
ct = format.parse(time).getTime();
} catch (Exception e) {
e.printStackTrace();
}
return ct;
}
/**
* 判斷兩日期是否同一天
*
* @param str1
* @param str2
* @return
*/
@SuppressLint("SimpleDateFormat")
public static boolean isSameDay(String str1, String str2) {
Date day1 = null, day2 = null;
day1 = DateTimeUtil.strToDate(str1);
day2 = DateTimeUtil.strToDate(str2);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String ds1 = sdf.format(day1);
String ds2 = sdf.format(day2);
if (ds1.equals(ds2)) {
return true;
} else {
return false;
}
}
/**
* 獲取兩個(gè)日期的時(shí)間差
*/
@SuppressLint("SimpleDateFormat")
public static int getTimeInterval(String date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int interval = 0;
try {
Date currentTime = new Date();// 獲取現(xiàn)在的時(shí)間
Date beginTime = dateFormat.parse(date);
interval = (int) ((beginTime.getTime() - currentTime.getTime()) / (1000));// 時(shí)間差
// 單位秒
} catch (ParseException e) {
e.printStackTrace();
}
return interval;
}
/**
* 獲取兩個(gè)日期的時(shí)間差 yyyy.MM.dd HH.mm.ss
*/
@SuppressLint("SimpleDateFormat")
public static int getInterval(String bDate, String eDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
int interval = 0;
try {
Date currentTime = dateFormat.parse(eDate);// 獲取現(xiàn)在的時(shí)間
Date beginTime = dateFormat.parse(bDate);
interval = (int) ((beginTime.getTime() - currentTime.getTime()));// 時(shí)間差
// 單位秒
} catch (ParseException e) {
e.printStackTrace();
}
return interval;
}
/**
* 兩個(gè)時(shí)間之差 求出一個(gè)long Time
* @param date
* @return
*/
@SuppressLint("SimpleDateFormat")
public static long getTime(String date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long diff = 0;
try {
Date currentTime = new Date();// 獲取現(xiàn)在的時(shí)間
Date getdate = df.parse(date);
diff = getdate.getTime() - currentTime.getTime();
} catch (Exception e) {
}
return diff;
}
/**
* 日期轉(zhuǎn)換成Java字符串
* @param DATE1
* @param DATE2
* @return
*/
@SuppressLint("SimpleDateFormat")
public static int compare_date(String DATE1, String DATE2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(DATE1);
Date dt2 = df.parse(DATE2);
if (dt1.getTime() >= dt2.getTime()) {
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
return -1;
} else {
return 0;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return 0;
}
/**
* 傳入時(shí)間 算出星期幾
*
* @param str
* 2014年1月3日
* @param days
* 1:2014年1月4日 類推
* @return
*/
@SuppressLint("SimpleDateFormat")
public static String formatDate(String str, int days) {
String dateStr = "";
try {
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
Date date = df.parse(str);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date d = dateFormat.parse(dateFormat.format(date));
c.setTime(d);
c.add(Calendar.DAY_OF_MONTH, days);
switch (c.get(Calendar.DAY_OF_WEEK) - 1) {
case 0:
dateStr = "周日";
break;
case 1:
dateStr = "周一";
break;
case 2:
dateStr = "周二";
break;
case 3:
dateStr = "周三";
break;
case 4:
dateStr = "周四";
break;
case 5:
dateStr = "周五";
break;
case 6:
dateStr = "周六";
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
}
return dateStr;
}
}
時(shí)間豁辉、日期工具類
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)幌陕,“玉大人诵姜,你說(shuō)我怎么就攤上這事〔ǎ” “怎么了棚唆?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)心例。 經(jīng)常有香客問(wèn)我宵凌,道長(zhǎng),這世上最難降的妖魔是什么止后? 我笑而不...
- 正文 為了忘掉前任瞎惫,我火速辦了婚禮,結(jié)果婚禮上译株,老公的妹妹穿的比我還像新娘瓜喇。我一直安慰自己,他們只是感情好歉糜,可當(dāng)我...
- 文/花漫 我一把揭開白布乘寒。 她就那樣靜靜地躺著,像睡著了一般匪补。 火紅的嫁衣襯著肌膚如雪肃续。 梳的紋絲不亂的頭發(fā)上黍檩,一...
- 那天,我揣著相機(jī)與錄音始锚,去河邊找鬼刽酱。 笑死,一個(gè)胖子當(dāng)著我的面吹牛瞧捌,可吹牛的內(nèi)容都是我干的棵里。 我是一名探鬼主播,決...
- 文/蒼蘭香墨 我猛地睜開眼姐呐,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼殿怜!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起曙砂,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤头谜,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后鸠澈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體柱告,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年笑陈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了际度。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布,位于F島的核電站帆锋,受9級(jí)特大地震影響墩新,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜窟坐,卻給世界環(huán)境...
- 文/蒙蒙 一海渊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧哲鸳,春花似錦臣疑、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至婿奔,卻和暖如春缺狠,著一層夾襖步出監(jiān)牢的瞬間问慎,已是汗流浹背。 一陣腳步聲響...
- 正文 我出身青樓穷劈,卻偏偏與公主長(zhǎng)得像笼恰,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子歇终,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 前言 在任何APP開發(fā)中社证,日期和時(shí)間是無(wú)處不在的,例如QQ评凝、微信追葡,每條信息都會(huì)顯示發(fā)送時(shí)間,還有空間奕短、朋友圈每一條...
- 9月7日 錯(cuò)亂的時(shí)間 現(xiàn)在是晚上23:16宜肉,我在這里寫日志。 雖然對(duì)有些人來(lái)說(shuō)是很晚了篡诽,但是我表示我還在這里寫日志...
- 憶長(zhǎng)安崖飘,九月時(shí)榴捡,登高望見昆池杈女。上苑初開露菊,芳林正獻(xiàn)霜梨吊圾。更想千門萬(wàn)戶达椰,月明砧杵參差。 ...