時間工具類

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.*;

/**

* 日期工具類

*

* @author crazyang

*/

@Component

public class DateUtils {

/**

* 獲得當前日期 yyyy-MM-dd HH:mm:ss

* 小寫的hh取得12小時,大寫的HH取的是24小時

? ? * @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)當前時間戳

*

? ? * @return 1566889186583

*/

? ? public static StringgetSystemTime() {

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

? ? ? ? return current;

? ? }

/**

* 獲取當前日期 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);

? ? }

/**

* 得到兩個時間差? 格式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;

? ? ? ? }

}

/**

* 得到兩個時間差

*

? ? * @param start 開始時間

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

? ? * @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);

? ? }

/**

* 獲得當前日期 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;

? ? }

/**

* 獲取當前日期是一個星期的第幾天

*

? ? * @return 2

*/

? ? public static int getDayOfWeek() {

Calendar cal = Calendar.getInstance();

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

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

? ? }

/**

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

*

? ? * @param nowTime? ? 當前時間

? ? * @param dateSection 時間區(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)時間戳(毫秒)

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

? ? ? ? }catch (Exception e) {

e.printStackTrace();

? ? ? ? ? ? return 0;

? ? ? ? }

}

/**

* 獲取當前小時 :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;

? ? }

/**

* 獲取當前時間一個小時前

? ? * @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;

? ? ? ? }

//獲取當前日期前一天

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

? ? }

/**

* 獲取當前日期前一天

*

? ? * @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;

? ? }

/**

* 獲取最近一個月

*

? ? * @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;

? ? }

/**

* 獲取最近三個月

*

? ? * @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ù)

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

*

? ? * @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;

? ? }

/**

* 返回當前年度季度list

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

? ? * @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)時間戳

? ? * @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)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容