時(shí)間工具類(lèi)

package com.zymotor.eutech.web.util;

import org.springframework.stereotype.Component;

import org.springframework.util.StringUtils;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.*;

/**

* 日期工具類(lèi)

*

* @author crazyang

*/

@Component

public class DateUtils {

/**

* 獲得當(dāng)前日期 yyyy-MM-dd HH:mm:ss

* 小寫(xiě)的hh取得12小時(shí)划滋,大寫(xiě)的HH取的是24小時(shí)

? ? * @return 2019-08-27 14:12:40

*/

? ? public static StringgetCurrentTime() {

SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

? ? ? ? Date date =new Date();

? ? ? ? return df.format(date);

? ? }

/**

* 獲取系統(tǒng)當(dāng)前時(shí)間戳

*

? ? * @return 1566889186583

*/

? ? public static StringgetSystemTime() {

String current = String.valueOf(System.currentTimeMillis());

? ? ? ? return current;

? ? }

/**

* 獲取當(dāng)前日期 yy-MM-dd

*

? ? * @return 2019-08-27

*/

? ? public static StringgetDateByString() {

Date date =new Date();

? ? ? ? SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

? ? ? ? return sdf.format(date);

? ? }

/**

* 得到兩個(gè)時(shí)間差? 格式y(tǒng)yyy-MM-dd HH:mm:ss

*

? ? * @param start 2019-06-27 14:12:40

? ? * @param end? 2019-08-27 14:12:40

? ? * @return 5270400000

*/

? ? public static long dateSubtraction(String start, String end) {

SimpleDateFormat df =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

? ? ? ? try {

Date date1 = df.parse(start);

? ? ? ? ? ? Date date2 = df.parse(end);

? ? ? ? ? ? return date2.getTime() - date1.getTime();

? ? ? ? }catch (ParseException e) {

e.printStackTrace();

? ? ? ? ? ? return 0;

? ? ? ? }

}

/**

* 得到兩個(gè)時(shí)間差

*

? ? * @param start 開(kāi)始時(shí)間

? ? * @param end 結(jié)束時(shí)間

? ? * @return

? ? */

? ? public static long dateTogether(Date start, Date end) {

return end.getTime() - start.getTime();

? ? }

/**

* 轉(zhuǎn)化long值的日期為yyyy-MM-dd? HH:mm:ss.SSS格式的日期

*

? ? * @param millSec 日期long值? 5270400000

? ? * @return 日期毒返,以yyyy-MM-dd? HH:mm:ss.SSS格式輸出 1970-03-03? 08:00:00.000

*/

? ? public static StringtransferLongToDate(String millSec) {

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd? HH:mm:ss.SSS");

? ? ? ? Date date =new Date(Long.parseLong(millSec));

? ? ? ? return sdf.format(date);

? ? }

/**

* 獲得當(dāng)前日期 yyyy-MM-dd HH:mm:ss

*

? ? * @return

? ? */

? ? public static StringgetOkDate(String date) {

try {

if (StringUtils.isEmpty(date)) {

return null;

? ? ? ? ? ? }

Date date1 =new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH).parse(date);

? ? ? ? ? ? //格式化

? ? ? ? ? ? SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

? ? ? ? ? ? return sdf.format(date1);

? ? ? ? }catch (Exception e) {

e.printStackTrace();

? ? ? ? }

return null;

? ? }

/**

* 獲取當(dāng)前日期是一個(gè)星期的第幾天

*

? ? * @return 2

*/

? ? public static int getDayOfWeek() {

Calendar cal = Calendar.getInstance();

? ? ? ? cal.setTime(new Date());

? ? ? ? return cal.get(Calendar.DAY_OF_WEEK) -1;

? ? }

/**

* 判斷當(dāng)前時(shí)間是否在[startTime, endTime]區(qū)間,注意時(shí)間格式要一致

*

? ? * @param nowTime? ? 當(dāng)前時(shí)間

? ? * @param dateSection 時(shí)間區(qū)間? 2018-01-08,2019-09-09

? ? * @return

? ? * @author jqlin

*/

? ? public static boolean isEffectiveDate(Date nowTime, String dateSection) {

try {

String[] times = dateSection.split(",");

? ? ? ? ? ? String format ="yyyy-MM-dd";

? ? ? ? ? ? Date startTime =new SimpleDateFormat(format).parse(times[0]);

? ? ? ? ? ? Date endTime =new SimpleDateFormat(format).parse(times[1]);

? ? ? ? ? ? if (nowTime.getTime() == startTime.getTime()

|| nowTime.getTime() == endTime.getTime()) {

return true;

? ? ? ? ? ? }

Calendar date = Calendar.getInstance();

? ? ? ? ? ? date.setTime(nowTime);

? ? ? ? ? ? Calendar begin = Calendar.getInstance();

? ? ? ? ? ? begin.setTime(startTime);

? ? ? ? ? ? Calendar end = Calendar.getInstance();

? ? ? ? ? ? end.setTime(endTime);

? ? ? ? ? ? if (isSameDay(date, begin) ||isSameDay(date, end)) {

return true;

? ? ? ? ? ? }

if (date.after(begin) && date.before(end)) {

return true;

? ? ? ? ? ? }else {

return false;

? ? ? ? ? ? }

}catch (Exception e) {

e.printStackTrace();

return false;

? ? ? ? }

}

public static boolean isSameDay(Calendar cal1, Calendar cal2) {

if (cal1 !=null && cal2 !=null) {

return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);

? ? ? ? }else {

throw new IllegalArgumentException("The date must not be null");

? ? ? ? }

}

public static long getTimeByDate(String time) {

SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

? ? ? ? try {

Date date = format.parse(time);

? ? ? ? ? ? //日期轉(zhuǎn)時(shí)間戳(毫秒)

? ? ? ? ? ? return date.getTime();

? ? ? ? }catch (Exception e) {

e.printStackTrace();

? ? ? ? ? ? return 0;

? ? ? ? }

}

/**

* 獲取當(dāng)前小時(shí) :2019-08-23 17

*

? ? * @return? 2019-08-27 17

*/

? ? public static StringgetCurrentHour() {

GregorianCalendar calendar =new GregorianCalendar();

? ? ? ? int hour = calendar.get(Calendar.HOUR_OF_DAY);

? ? ? ? if (hour <10) {

return DateUtils.getCurrentTime() +" 0" + hour;

? ? ? ? }

return DateUtils.getDateByString() +" " + hour;

? ? }

/**

* 獲取當(dāng)前時(shí)間一個(gè)小時(shí)前

? ? * @return 2019-08-27 16

*/

? ? public static StringgetCurrentHourBefore() {

GregorianCalendar calendar =new GregorianCalendar();

? ? ? ? int hour = calendar.get(Calendar.HOUR_OF_DAY);

? ? ? ? if (hour >0) {

hour = calendar.get(Calendar.HOUR_OF_DAY) -1;

? ? ? ? ? ? if (hour <10) {

return DateUtils.getDateByString() +" 0" + hour;

? ? ? ? ? ? }

return DateUtils.getDateByString() +" " + hour;

? ? ? ? }

//獲取當(dāng)前日期前一天

? ? ? ? return DateUtils.getBeforeDay() +" " +23;

? ? }

/**

* 獲取當(dāng)前日期前一天

*

? ? * @return 2019-08-26

*/

? ? public static StringgetBeforeDay() {

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

? ? ? ? Date date =new Date();

? ? ? ? Calendar calendar = Calendar.getInstance();

? ? ? ? calendar.setTime(date);

? ? ? ? calendar.add(Calendar.DAY_OF_MONTH, -1);

? ? ? ? date = calendar.getTime();

? ? ? ? return sdf.format(date);

? ? }

/**

* 獲取最近七天

*

? ? * @return 2019-08-20

*/

? ? public static StringgetServen() {

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

? ? ? ? Calendar c = Calendar.getInstance();

? ? ? ? c.add(Calendar.DATE, -7);

? ? ? ? Date monday = c.getTime();

? ? ? ? String preMonday = sdf.format(monday);

? ? ? ? return preMonday;

? ? }

/**

* 獲取最近一個(gè)月

*

? ? * @return 2019-07-27

*/

? ? public static StringgetOneMonth() {

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

? ? ? ? Calendar c = Calendar.getInstance();

? ? ? ? c.add(Calendar.MONTH, -1);

? ? ? ? Date monday = c.getTime();

? ? ? ? String preMonday = sdf.format(monday);

? ? ? ? return preMonday;

? ? }

/**

* 獲取最近三個(gè)月

*

? ? * @return 2019-05-27

*/

? ? public static StringgetThreeMonth() {

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

? ? ? ? Calendar c = Calendar.getInstance();

? ? ? ? c.add(Calendar.MONTH, -3);

? ? ? ? Date monday = c.getTime();

? ? ? ? String preMonday = sdf.format(monday);

? ? ? ? return preMonday;

? ? }

/**

* 獲取最近一年

*

? ? * @return 2018-08-27

*/

? ? public static StringgetOneYear() {

SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");

? ? ? ? Calendar c = Calendar.getInstance();

? ? ? ? c.add(Calendar.YEAR, -1);

? ? ? ? Date start = c.getTime();

? ? ? ? String startDay = sdf.format(start);

? ? ? ? return startDay;

? ? }

private static int month = Calendar.getInstance().get(Calendar.MONTH) +1;

? ? /**

* 獲取今年月份數(shù)據(jù)

* 說(shuō)明 有的需求前端需要根據(jù)月份查詢(xún)每月數(shù)據(jù),此時(shí)后臺(tái)給前端返回今年共有多少月份

*

? ? * @return [1, 2, 3, 4, 5, 6, 7, 8]

*/

? ? public static ListgetMonthList(){

List list =new ArrayList();

? ? ? ? for (int i =1; i <=month; i++) {

list.add(i);

? ? ? ? }

return list;

? ? }

/**

* 返回當(dāng)前年度季度list

* 本年度截止目前共三個(gè)季度裤纹,然后根據(jù)1,2,3分別查詢(xún)相關(guān)起止時(shí)間

? ? * @return [1, 2, 3]

*/

? ? public static ListgetQuartList(){

int quart =month /3 +1;

? ? ? ? List list =new ArrayList();

? ? ? ? for (int i =1; i <= quart; i++) {

list.add(i);

? ? ? ? }

return list;

? ? }

/**

* 字符串轉(zhuǎn)時(shí)間戳

? ? * @param time

? ? * @return

? ? */

? ? public static LonggetTimestamp(String time) {

Long timestamp =null;

? ? ? ? try {

timestamp =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime();

? ? ? ? }catch (ParseException e) {

e.printStackTrace();

? ? ? ? }

return timestamp;

? ? }

public static void main(String[] args) {

System.out.println(DateUtils.getQuartList());

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末委刘,一起剝皮案震驚了整個(gè)濱河市德谅,隨后出現(xiàn)的幾起案子体捏,更是在濱河造成了極大的恐慌呆馁,老刑警劉巖染苛,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鹊漠,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡殖侵,警方通過(guò)查閱死者的電腦和手機(jī)贸呢,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)拢军,“玉大人楞陷,你說(shuō)我怎么就攤上這事≤园Γ” “怎么了固蛾?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)度陆。 經(jīng)常有香客問(wèn)我艾凯,道長(zhǎng),這世上最難降的妖魔是什么懂傀? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任趾诗,我火速辦了婚禮,結(jié)果婚禮上蹬蚁,老公的妹妹穿的比我還像新娘恃泪。我一直安慰自己,他們只是感情好犀斋,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布贝乎。 她就那樣靜靜地躺著,像睡著了一般叽粹。 火紅的嫁衣襯著肌膚如雪览效。 梳的紋絲不亂的頭發(fā)上却舀,一...
    開(kāi)封第一講書(shū)人閱讀 52,246評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音锤灿,去河邊找鬼挽拔。 笑死,一個(gè)胖子當(dāng)著我的面吹牛衡招,可吹牛的內(nèi)容都是我干的篱昔。 我是一名探鬼主播每强,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼始腾,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了空执?” 一聲冷哼從身側(cè)響起浪箭,我...
    開(kāi)封第一講書(shū)人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎辨绊,沒(méi)想到半個(gè)月后奶栖,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡门坷,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年宣鄙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片默蚌。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡冻晤,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绸吸,到底是詐尸還是另有隱情鼻弧,我是刑警寧澤,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布锦茁,位于F島的核電站攘轩,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏码俩。R本人自食惡果不足惜度帮,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望稿存。 院中可真熱鬧笨篷,春花似錦、人聲如沸挠铲。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)拂苹。三九已至安聘,卻和暖如春痰洒,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背浴韭。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工丘喻, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人念颈。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓泉粉,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親榴芳。 傳聞我的和親對(duì)象是個(gè)殘疾皇子嗡靡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359