1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792 |
- package com.kcim.common.util;
- import com.kcim.common.enums.DateStyleEnum;
- import com.kcim.common.enums.WeekEnum;
- import lombok.extern.slf4j.Slf4j;
- import java.sql.Timestamp;
- import java.text.DateFormat;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.*;
- @Slf4j
- public class DateUtils {
- public static final SimpleDateFormat DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- public static final SimpleDateFormat monthSdf = new SimpleDateFormat("yyyy-MM");
- public static final SimpleDateFormat yearSdf = new SimpleDateFormat("yyyy");
- public static final SimpleDateFormat DATE_FORMAT_YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
- /**
- * 通用的一个DateFormat
- */
- public final static SimpleDateFormat commonDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- /**
- * 处理时分秒的DateFormat
- */
- public final static SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- /**
- * 不显示秒的DateFormat
- */
- public final static SimpleDateFormat noSecondFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- /**
- * 获取SimpleDateFormat
- *
- * @param parttern 日期格式
- * @return SimpleDateFormat对象
- * @throws RuntimeException 异常:非法日期格式
- */
- private static SimpleDateFormat getDateFormat(String parttern) throws RuntimeException {
- return new SimpleDateFormat(parttern);
- }
- /**
- * 获取日期中的某数值。如获取月份
- *
- * @param date 日期
- * @param dateType 日期格式
- * @return 数值
- */
- public static int getInteger(Date date, int dateType) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(dateType);
- }
- /**
- * 当前时间转换.
- *
- * @return 时间字符串
- */
- public static String getDateString() {
- Date date = new Date();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- String strDate = sdf.format(date);
- return strDate;
- }
- /**
- * 获取时间的字符串
- *
- * @param date
- * @param simpleDateFormat
- * @return 时间字符串
- */
- public static String getDateString(Date date, SimpleDateFormat simpleDateFormat) {
- String strDate = simpleDateFormat.format(date);
- return strDate;
- }
- /**
- * 增加日期中某类型的某数值。如增加日期
- *
- * @param date 日期字符串
- * @param dateType 类型
- * @param amount 数值
- * @return 计算后日期字符串
- */
- private static String addInteger(String date, int dateType, int amount) {
- String dateString = null;
- DateStyleEnum dateStyle = getDateStyle(date);
- if (dateStyle != null) {
- Date myDate = StringToDate(date, dateStyle);
- myDate = addInteger(myDate, dateType, amount);
- dateString = DateToString(myDate, dateStyle);
- }
- return dateString;
- }
- /**
- * 增加日期中某类型的某数值。如增加日期
- *
- * @param date 日期字符串
- * @param dateType 类型
- * @param amount 数值
- * @return 计算后日期字符串
- */
- private static String addInteger(String date, DateStyleEnum dateStyle, int dateType, int amount) {
- String dateString = null;
- if (dateStyle != null) {
- Date myDate = StringToDate(date, dateStyle);
- myDate = addInteger(myDate, dateType, amount);
- dateString = DateToString(myDate, dateStyle);
- }
- return dateString;
- }
- /**
- * 增加日期中某类型的某数值。如增加日期
- *
- * @param date 日期
- * @param dateType 类型
- * @param amount 数值
- * @return 计算后日期
- */
- private static Date addInteger(Date date, int dateType, int amount) {
- Date myDate = null;
- if (date != null) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(dateType, amount);
- myDate = calendar.getTime();
- }
- return myDate;
- }
- /**
- * 获取本周开始时间
- *
- * @param date
- *
- * @return
- */
- public static Date getBeginDayOfWeek(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
- if (dayofweek == 1) {
- dayofweek += 7;
- }
- cal.add(Calendar.DATE, 2 - dayofweek);
- return getDayStartTime(cal.getTime());
- }
- /**
- * 获取某个日期的开始时间
- *
- * @param date
- *
- * @return
- */
- public static Timestamp getDayStartTime(Date date) {
- Calendar calendar = Calendar.getInstance();
- if (null != date) {
- calendar.setTime(date);
- }
- calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- calendar.set(Calendar.MILLISECOND, 0);
- return new Timestamp(calendar.getTimeInMillis());
- }
- /**
- * 获取本周结束时间
- *
- * @param date
- *
- * @return
- */
- public static Date getEndDayOfWeek(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(getBeginDayOfWeek(date));
- cal.add(Calendar.DAY_OF_WEEK, 6);
- Date weekEndSta = cal.getTime();
- return getDayEndTime(weekEndSta);
- }
- /**
- * 获取某个日期的结束时间
- *
- * @param date
- *
- * @return
- */
- private static Date getDayEndTime(Date date) {
- Calendar calendar = Calendar.getInstance();
- if(null != date){
- calendar.setTime(date);
- }
- calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
- calendar.set(Calendar.MILLISECOND, 999);
- return new Timestamp(calendar.getTimeInMillis());
- }
- /**
- * 获取精确的日期
- *
- * @param timestamps 时间long集合
- * @return 日期
- */
- private static Date getAccurateDate(List<Long> timestamps) {
- Date date = null;
- long timestamp = 0;
- Map<Long, long[]> map = new HashMap<Long, long[]>();
- List<Long> absoluteValues = new ArrayList<Long>();
- if (timestamps != null && timestamps.size() > 0) {
- if (timestamps.size() > 1) {
- for (int i = 0; i < timestamps.size(); i++) {
- for (int j = i + 1; j < timestamps.size(); j++) {
- long absoluteValue = Math.abs(timestamps.get(i) - timestamps.get(j));
- absoluteValues.add(absoluteValue);
- long[] timestampTmp = {timestamps.get(i), timestamps.get(j)};
- map.put(absoluteValue, timestampTmp);
- }
- }
- // 有可能有相等的情况。如2012-11和2012-11-01。时间戳是相等的
- long minAbsoluteValue = -1;
- if (!absoluteValues.isEmpty()) {
- // 如果timestamps的size为2,这是差值只有一个,因此要给默认值
- minAbsoluteValue = absoluteValues.get(0);
- }
- for (int i = 0; i < absoluteValues.size(); i++) {
- for (int j = i + 1; j < absoluteValues.size(); j++) {
- if (minAbsoluteValue > absoluteValues.get(j)) {
- minAbsoluteValue = absoluteValues.get(j);
- }
- }
- }
- if (minAbsoluteValue != -1) {
- long[] timestampsLastTmp = map.get(minAbsoluteValue);
- if (absoluteValues.size() > 1) {
- timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
- } else if (absoluteValues.size() == 1) {
- // 当timestamps的size为2,需要与当前时间作为参照
- long dateOne = timestampsLastTmp[0];
- long dateTwo = timestampsLastTmp[1];
- if ((Math.abs(dateOne - dateTwo)) < 100000000000L) {
- timestamp = Math.max(timestampsLastTmp[0], timestampsLastTmp[1]);
- } else {
- long now = new Date().getTime();
- if (Math.abs(dateOne - now) <= Math.abs(dateTwo - now)) {
- timestamp = dateOne;
- } else {
- timestamp = dateTwo;
- }
- }
- }
- }
- } else {
- timestamp = timestamps.get(0);
- }
- }
- if (timestamp != 0) {
- date = new Date(timestamp);
- }
- return date;
- }
- /**
- * 判断字符串是否为日期字符串
- *
- * @param date 日期字符串
- * @return true or false
- */
- public static boolean isDate(String date) {
- boolean isDate = false;
- if (date != null) {
- if (StringToDate(date) != null) {
- isDate = true;
- }
- }
- return isDate;
- }
- /**
- * 获取日期字符串的日期风格。失敗返回null。
- *
- * @param date 日期字符串
- * @return 日期风格
- */
- public static DateStyleEnum getDateStyle(String date) {
- DateStyleEnum dateStyle = null;
- Map<Long, DateStyleEnum> map = new HashMap<Long, DateStyleEnum>();
- List<Long> timestamps = new ArrayList<Long>();
- for (DateStyleEnum style : DateStyleEnum.values()) {
- Date dateTmp = StringToDate(date, style.getValue());
- if (dateTmp != null) {
- timestamps.add(dateTmp.getTime());
- map.put(dateTmp.getTime(), style);
- }
- }
- dateStyle = map.get(getAccurateDate(timestamps).getTime());
- return dateStyle;
- }
- /**
- * 将日期字符串转化为日期。失败返回null。
- *
- * @param date 日期字符串
- * @return 日期
- */
- public static Date StringToDate(String date) {
- DateStyleEnum dateStyle = null;
- return StringToDate(date, dateStyle);
- }
- /**
- * 将日期字符串转化为日期。失败返回null。
- *
- * @param date 日期字符串
- * @param parttern 日期格式
- * @return 日期
- */
- public static Date StringToDate(String date, String parttern) {
- Date myDate = null;
- if (date != null) {
- try {
- myDate = getDateFormat(parttern).parse(date);
- } catch (Exception e) {
- }
- }
- return myDate;
- }
- /**
- * 将日期字符串转化为日期。失败返回null。
- *
- * @param date 日期字符串
- * @param dateStyle 日期风格
- * @return 日期
- */
- public static Date StringToDate(String date, DateStyleEnum dateStyle) {
- Date myDate = null;
- if (dateStyle == null) {
- List<Long> timestamps = new ArrayList<Long>();
- for (DateStyleEnum style : DateStyleEnum.values()) {
- Date dateTmp = StringToDate(date, style.getValue());
- if (dateTmp != null) {
- timestamps.add(dateTmp.getTime());
- }
- }
- myDate = getAccurateDate(timestamps);
- } else {
- myDate = StringToDate(date, dateStyle.getValue());
- }
- return myDate;
- }
- /**
- * 将日期转化为日期字符串。失败返回null。
- *
- * @param date 日期
- * @param parttern 日期格式
- * @return 日期字符串
- */
- public static String DateToString(Date date, String parttern) {
- String dateString = null;
- if (date != null) {
- try {
- dateString = getDateFormat(parttern).format(date);
- } catch (Exception e) {
- }
- }
- return dateString;
- }
- /**
- * 将日期转化为日期字符串。失败返回null。
- *
- * @param date 日期
- * @param dateStyle 日期风格
- * @return 日期字符串
- */
- public static String DateToString(Date date, DateStyleEnum dateStyle) {
- String dateString = null;
- if (dateStyle != null) {
- dateString = DateToString(date, dateStyle.getValue());
- }
- return dateString;
- }
- /**
- * 将日期字符串转化为另一日期字符串。失败返回null。
- *
- * @param date 旧日期字符串
- * @param parttern 新日期格式
- * @return 新日期字符串
- */
- public static String StringToString(String date, String parttern) {
- return StringToString(date, null, parttern);
- }
- /**
- * 将日期字符串转化为另一日期字符串。失败返回null。
- *
- * @param date 旧日期字符串
- * @param dateStyle 新日期风格
- * @return 新日期字符串
- */
- public static String StringToString(String date, DateStyleEnum dateStyle) {
- return StringToString(date, null, dateStyle);
- }
- /**
- * 将日期字符串转化为另一日期字符串。失败返回null。
- *
- * @param date 旧日期字符串
- * @param olddParttern 旧日期格式
- * @param newParttern 新日期格式
- * @return 新日期字符串
- */
- public static String StringToString(String date, String olddParttern, String newParttern) {
- String dateString = null;
- if (olddParttern == null) {
- DateStyleEnum style = getDateStyle(date);
- if (style != null) {
- Date myDate = StringToDate(date, style.getValue());
- dateString = DateToString(myDate, newParttern);
- }
- } else {
- Date myDate = StringToDate(date, olddParttern);
- dateString = DateToString(myDate, newParttern);
- }
- return dateString;
- }
- /**
- * 将日期字符串转化为另一日期字符串。失败返回null。
- *
- * @param date 旧日期字符串
- * @param olddDteStyle 旧日期风格
- * @param newDateStyle 新日期风格
- * @return 新日期字符串
- */
- public static String StringToString(String date, DateStyleEnum olddDteStyle, DateStyleEnum newDateStyle) {
- String dateString = null;
- if (olddDteStyle == null) {
- DateStyleEnum style = getDateStyle(date);
- dateString = StringToString(date, style.getValue(), newDateStyle.getValue());
- } else {
- dateString = StringToString(date, olddDteStyle.getValue(), newDateStyle.getValue());
- }
- return dateString;
- }
- /**
- * 增加日期的年份。失败返回null。
- *
- * @param date 日期
- * @param yearAmount 增加数量。可为负数
- * @return 增加年份后的日期字符串
- */
- public static String addYear(String date, int yearAmount) {
- return addInteger(date, Calendar.YEAR, yearAmount);
- }
- /**
- * 增加日期的年份。失败返回null。
- *
- * @param date 日期
- * @param yearAmount 增加数量。可为负数
- * @return 增加年份后的日期
- */
- public static Date addYear(Date date, int yearAmount) {
- return addInteger(date, Calendar.YEAR, yearAmount);
- }
- /**
- * 增加日期的月份。失败返回null。
- *
- * @param date 日期
- * @param yearAmount 增加数量。可为负数
- * @return 增加月份后的日期字符串
- */
- public static String addMonth(String date, int yearAmount) {
- return addInteger(date, Calendar.MONTH, yearAmount);
- }
- /**
- * 增加日期的月份。失败返回null。
- *
- * @param date 日期
- * @param yearAmount 增加数量。可为负数
- * @return 增加月份后的日期
- */
- public static Date addMonth(Date date, int yearAmount) {
- return addInteger(date, Calendar.MONTH, yearAmount);
- }
- /**
- * 增加日期的天数。失败返回null。
- *
- * @param date 日期字符串
- * @param dayAmount 增加数量。可为负数
- * @return 增加天数后的日期字符串
- * @deprecated use {@link #addDay(String, int, DateStyleEnum)}
- */
- @Deprecated
- public static String addDay(String date, int dayAmount) {
- return addInteger(date, Calendar.DATE, dayAmount);
- }
- /**
- * 增加日期的天数。失败返回null。
- *
- * @param date 日期字符串
- * @param dayAmount 增加数量。可为负数
- * @param style 日期样式
- * @return 增加天数后的日期字符串
- */
- public static String addDay(String date, int dayAmount, DateStyleEnum style) {
- return addInteger(date, style, Calendar.DATE, dayAmount);
- }
- /**
- * 增加日期的天数。失败返回null。
- *
- * @param date 日期
- * @param dayAmount 增加数量。可为负数
- * @return 增加天数后的日期
- */
- public static Date addDay(Date date, int dayAmount) {
- return addInteger(date, Calendar.DATE, dayAmount);
- }
- /**
- * 增加日期的小时。失败返回null。
- *
- * @param date 日期字符串
- * @param hourAmount 增加数量。可为负数
- * @return 增加小时后的日期字符串
- */
- public static String addHour(String date, int hourAmount) {
- return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
- }
- /**
- * 增加日期的小时。失败返回null。
- *
- * @param date 日期
- * @param hourAmount 增加数量。可为负数
- * @return 增加小时后的日期
- */
- public static Date addHour(Date date, int hourAmount) {
- return addInteger(date, Calendar.HOUR_OF_DAY, hourAmount);
- }
- /**
- * 增加日期的分钟。失败返回null。
- *
- * @param date 日期字符串
- * @param hourAmount 增加数量。可为负数
- * @return 增加分钟后的日期字符串
- */
- public static String addMinute(String date, int hourAmount) {
- return addInteger(date, Calendar.MINUTE, hourAmount);
- }
- /**
- * 增加日期的分钟。失败返回null。
- *
- * @param date 日期
- * @param hourAmount 增加数量。可为负数
- * @return 增加分钟后的日期
- */
- public static Date addMinute(Date date, int hourAmount) {
- return addInteger(date, Calendar.MINUTE, hourAmount);
- }
- /**
- * 增加日期的秒钟。失败返回null。
- *
- * @param date 日期字符串
- * @param hourAmount 增加数量。可为负数
- * @return 增加秒钟后的日期字符串
- */
- public static String addSecond(String date, int hourAmount) {
- return addInteger(date, Calendar.SECOND, hourAmount);
- }
- /**
- * 增加日期的秒钟。失败返回null。
- *
- * @param date 日期
- * @param hourAmount 增加数量。可为负数
- * @return 增加秒钟后的日期
- */
- public static Date addSecond(Date date, int hourAmount) {
- return addInteger(date, Calendar.SECOND, hourAmount);
- }
- /**
- * 获取日期的年份。失败返回0。
- *
- * @param date 日期字符串
- * @return 年份
- */
- public static int getYear(String date) {
- return getYear(StringToDate(date));
- }
- /**
- * 获取日期的年份。失败返回0。
- *
- * @param date 日期
- * @return 年份
- */
- public static int getYear(Date date) {
- return getInteger(date, Calendar.YEAR);
- }
- /**
- * 获取日期的月份。失败返回0。
- *
- * @param date 日期字符串
- * @return 月份
- */
- public static int getMonth(String date) {
- return getMonth(StringToDate(date));
- }
- /**
- * 获取日期的月份
- *
- * @param date 日期字符串
- * @return 月份
- */
- public static String getMonthStr(Date date) {
- return monthSdf.format(date);
- }
- /**
- * 获取日期的月份。失败返回0。
- *
- * @param date 日期
- * @return 月份
- */
- public static int getMonth(Date date) {
- return getInteger(date, Calendar.MONTH);
- }
- /**
- * 获取日期的天数。失败返回0。
- *
- * @param date 日期字符串
- * @return 天
- */
- public static int getDay(String date) {
- return getDay(StringToDate(date));
- }
- /**
- * 获取日期的天数。失败返回0。
- *
- * @param date 日期
- * @return 天
- */
- public static int getDay(Date date) {
- return getInteger(date, Calendar.DATE);
- }
- /**
- * 获取日期的小时。失败返回0。
- *
- * @param date 日期字符串
- * @return 小时
- */
- public static int getHour(String date) {
- return getHour(StringToDate(date));
- }
- /**
- * 获取日期的小时。失败返回0。
- *
- * @param date 日期
- * @return 小时
- */
- public static int getHour(Date date) {
- return getInteger(date, Calendar.HOUR_OF_DAY);
- }
- /**
- * 获取日期的分钟。失败返回0。
- *
- * @param date 日期字符串
- * @return 分钟
- */
- public static int getMinute(String date) {
- return getMinute(StringToDate(date));
- }
- /**
- * 获取日期的分钟。失败返回0。
- *
- * @param date 日期
- * @return 分钟
- */
- public static int getMinute(Date date) {
- return getInteger(date, Calendar.MINUTE);
- }
- /**
- * 获取日期的秒钟。失败返回0。
- *
- * @param date 日期字符串
- * @return 秒钟
- */
- public static int getSecond(String date) {
- return getSecond(StringToDate(date));
- }
- /**
- * 获取日期的秒钟。失败返回0。
- *
- * @param date 日期
- * @return 秒钟
- */
- public static int getSecond(Date date) {
- return getInteger(date, Calendar.SECOND);
- }
- /**
- * 获取日期 。默认yyyy-MM-dd格式。失败返回null。
- *
- * @param date 日期字符串
- * @return 日期
- */
- public static String getDate(String date) {
- return StringToString(date, DateStyleEnum.YYYY_MM_DD);
- }
- /**
- * 获取日期。默认yyyy-MM-dd格式。失败返回null。
- *
- * @param date 日期
- * @return 日期
- */
- public static String getDate(Date date) {
- return DateToString(date, DateStyleEnum.YYYY_MM_DD);
- }
- /**
- * 获取日期。默认yyyy-MM-dd HH:mm:ss格式。失败返回null。
- *
- * @param date 日期
- * @return 日期
- */
- public static String getDateTime(Date date) {
- return DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM_SS);
- }
- /**
- * 获取日期。默认yyyy-MM-dd HH:mm格式。失败返回null。
- *
- * @param date 日期
- * @return 日期
- */
- public static String getDateTimeExcludeSec(Date date) {
- return DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM);
- }
- /**
- * 获取日期。默认yyyy-MM-dd HH:mm格式。失败返回null。
- *
- * @param date 日期
- * @return 日期
- */
- public static Date getDateTimeExcludeSec(String date) {
- return StringToDate(date, DateStyleEnum.YYYY_MM_DD_HH_MM);
- }
- /**
- * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
- *
- * @param date 日期字符串
- * @return 时间
- */
- public static String getTime(String date) {
- return StringToString(date, DateStyleEnum.HH_MM_SS);
- }
- /**
- * 获取日期的时间。默认HH:mm:ss格式。失败返回null。
- *
- * @param date 日期
- * @return 时间
- */
- public static String getTime(Date date) {
- return DateToString(date, DateStyleEnum.HH_MM_SS);
- }
- /**
- * 获取日期的星期。失败返回null。
- *
- * @param date 日期字符串
- * @return 星期
- */
- public static WeekEnum getWeek(String date) {
- WeekEnum week = null;
- DateStyleEnum dateStyle = getDateStyle(date);
- if (dateStyle != null) {
- Date myDate = StringToDate(date, dateStyle);
- week = getWeek(myDate);
- }
- return week;
- }
- /**
- * 获取日期的星期。失败返回null。
- *
- * @param date 日期
- * @return 星期
- */
- public static WeekEnum getWeek(Date date) {
- WeekEnum week = null;
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- int weekNumber = calendar.get(Calendar.DAY_OF_WEEK) - 1;
- switch (weekNumber) {
- case 0:
- week = WeekEnum.SUNDAY;
- break;
- case 1:
- week = WeekEnum.MONDAY;
- break;
- case 2:
- week = WeekEnum.TUESDAY;
- break;
- case 3:
- week = WeekEnum.WEDNESDAY;
- break;
- case 4:
- week = WeekEnum.THURSDAY;
- break;
- case 5:
- week = WeekEnum.FRIDAY;
- break;
- case 6:
- week = WeekEnum.SATURDAY;
- break;
- }
- return week;
- }
- /**
- * 获取两个日期相差的天数
- *
- * @param date 日期字符串
- * @param otherDate 另一个日期字符串
- * @return 相差天数
- */
- public static int getIntervalDays(String date, String otherDate) {
- return getIntervalDays(StringToDate(date), StringToDate(otherDate));
- }
- /**
- * @param date 日期
- * @param otherDate 另一个日期mingt
- * @return 相差天数
- */
- public static int getIntervalDays(Date date, Date otherDate) {
- //date = DateUtil.StringToDate(DateUtil.getDate(date));
- long time = Math.abs(date.getTime() - otherDate.getTime());
- long day = time / (24 * 60 * 60 * 1000);
- return (int) day;
- }
- /**
- * @param date
- * @param otherDate
- * @return 相差年数
- */
- public static long getIntervalYear(Date date, Date otherDate) {
- Calendar cal = Calendar.getInstance();
- int yearNow = cal.get(Calendar.YEAR);
- cal.setTime(otherDate);
- int yearOther = cal.get(Calendar.YEAR);
- long year = yearNow - yearOther;
- return year;
- }
- /**
- * 获取月份差
- *
- * @param date
- * @param otherDate
- * @return
- */
- public static long getIntervalMonths(Date date, Date otherDate) {
- long months;
- Calendar cal = Calendar.getInstance();
- int yearNow = cal.get(Calendar.YEAR);
- int monthNow = cal.get(Calendar.MONTH);
- cal.setTime(otherDate);
- int yearOther = cal.get(Calendar.YEAR);
- int monthOther = cal.get(Calendar.MONTH);
- long year = yearNow - yearOther;
- if (year >= 1) {
- months = year * 12 + monthNow - monthOther;
- } else {
- months = monthNow - monthOther;
- }
- return months;
- }
- public static String getWeekFromDate(Date date) {
- Calendar c = Calendar.getInstance();
- c.setTime(date);
- int week = c.get(Calendar.DAY_OF_WEEK);
- switch (week) {
- case 1:
- return "星期日";
- case 2:
- return "星期一";
- case 3:
- return "星期二";
- case 4:
- return "星期三";
- case 5:
- return "星期四";
- case 6:
- return "星期五";
- case 7:
- return "星期六";
- default:
- return "";
- }
- }
- /**
- * 获取指定天数之前的时间(相对于当前天数)
- *
- * @param currentDate 当前时间
- * @param dayNo 当前时间的几天之前
- * @return
- * @Title: getLastDays
- * @date 2014-3-26 下午11:23:57
- * @author Lawrence
- */
- public static Date getLastTimeForDays(Date currentDate, Integer dayNo) {
- if (null == currentDate || null == dayNo) {
- return null;
- }
- Calendar c = Calendar.getInstance();
- c.setTime(currentDate);
- c.add(Calendar.DATE, -dayNo);
- return c.getTime();
- }
- /**
- * 获取指定月数之前的时间(相对于当前天数)
- *
- * @param currentDate 当前时间
- * @param monthNo 当前时间的几月之前
- * @return
- * @Title: getLastDays
- * @date 2014-3-26 下午11:23:57
- * @author Lawrence
- */
- public static Date getLastTimeForMonths(Date currentDate, Integer monthNo) {
- if (null == currentDate || null == monthNo) {
- return null;
- }
- Calendar c = Calendar.getInstance();
- c.setTime(currentDate);
- c.add(Calendar.MONTH, -monthNo);
- return c.getTime();
- }
- /**
- * 获取指定月数之前的时间(相对于当前天数)
- *
- * @param currentDate 当前时间
- * @param yearNo 当前时间的几年之前
- * @return
- * @Title: getLastDays
- * @date 2014-3-26 下午11:23:57
- * @author Lawrence
- */
- public static Date getLastTimeForYears(Date currentDate, Integer yearNo) {
- Calendar c = Calendar.getInstance();
- c.setTime(currentDate);
- c.add(Calendar.YEAR, -yearNo);
- return c.getTime();
- }
- /**
- * 获取当天00:00:00
- *
- * @param currentDate
- * @return
- * @Title: getZeroPoint
- * @Description: (这里用一句话描述这个方法的作用)
- * @date 2014年10月10日 上午11:09:35
- * @author Administrator
- */
- public static Date getZeroPoint(Date currentDate) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(currentDate);
- cal.set(Calendar.HOUR_OF_DAY, 0); // 把当前时间小时变成0
- cal.set(Calendar.MINUTE, 0); // 把当前时间分钟变成0
- cal.set(Calendar.SECOND, 0); // 把当前时间秒数变成0
- cal.set(Calendar.MILLISECOND, 0); // 把当前时间毫秒变成0
- return cal.getTime();
- }
- /**
- * 获取当天00:00:00
- *
- * @param currentDate
- * @return
- * @Title: getZeroPoint
- * @Description: (这里用一句话描述这个方法的作用)
- * @date 2014年10月10日 上午11:09:35
- * @author Administrator
- */
- public static Date getZeroPoint(String currentDate) {
- return getZeroPoint(DateUtils.StringToDate(currentDate, DateStyleEnum.YYYY_MM_DD));
- }
- /**
- * 获取日期的最后一秒时间
- *
- * @param currentDate
- * @return
- */
- public static Date getLastPoint(String currentDate) {
- return getLastPoint(DateUtils.StringToDate(currentDate, DateStyleEnum.YYYY_MM_DD));
- }
- /**
- * 获取日期的最后一秒时间
- *
- * @param currentDate
- * @return
- */
- public static Date getLastPoint(Date currentDate) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(currentDate);
- cal.set(Calendar.HOUR_OF_DAY, 23); // 把当前时间小时变成
- cal.set(Calendar.MINUTE, 59); // 把当前时间分钟变成
- cal.set(Calendar.SECOND, 59); // 把当前时间秒数变成
- return cal.getTime();
- }
- /**
- * 获取指定分钟最后一秒时间
- *
- * @param currentDate
- * @return
- */
- public static Date getDateEndTime(Date currentDate) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(currentDate);
- cal.set(Calendar.SECOND, 59); // 把当前时间秒数变成
- return cal.getTime();
- }
- /**
- * 获取指定分钟最后一秒时间
- *
- * @param currentDate
- * @return
- */
- public static Date getDateBegTime(Date currentDate) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(currentDate);
- cal.set(Calendar.SECOND, 00); // 把当前时间秒数变成
- return cal.getTime();
- }
- /**
- * 时间比较大小工具类
- *
- * @param date1
- * @param date2
- * @return
- */
- public static Boolean compareDate(Date date1, Date date2) {
- try {
- if (date1.getTime() >= date2.getTime()) {
- return true;
- } else if (date1.getTime() < date2.getTime()) {
- return false;
- }
- } catch (Exception e) {
- log.error(e.toString(), e);
- }
- return false;
- }
- /**
- * 把毫秒转化成日期
- *
- * @param millSec(毫秒数)
- * @return
- */
- public static Date LongToDate(Long millSec) {
- return LongToDate(millSec, "yyyy-MM-dd HH:mm:ss");
- }
- /**
- * 把毫秒转化成日期
- *
- * @param parttern(日期格式,例如:MM/ dd/yyyy HH:mm:ss)
- * @param millSec(毫秒数)
- * @return
- */
- public static Date LongToDate(Long millSec, String parttern) {
- return new Date(millSec);
- }
- /* public static void main(String[] args){
- // Long currentTime = System.currentTimeMillis();
- // System.out.println(LongToDate(currentTime));
-
- // Date currentDate = new Date();
- // currentDate = getZeroPoint(currentDate);
- // //得到一天前
- // Date dd = getLastTimeForDays(currentDate, 1);
- // System.out.println(dd.toLocaleString());
- // //得到前一周
- // Date dw = getLastTimeForDays(currentDate, 7);
- // System.out.println(dw.toLocaleString());
- // //得到前一个月
- // Date dm = getLastTimeForMonths(currentDate, 1);
- // System.out.println(dm.toLocaleString());
- // //得到前一年
- // Date dy = getLastTimeForYears(currentDate, 1);
- // System.out.println(dy.toLocaleString());
- String date = DateToString(new Date(), "yyyyMMddHHmmss");
- System.out.println(date);
- System.out.println(getZeroPoint(new Date()));
- }*/
- /**
- * 截取时间getTime的后N位
- *
- * @param time 时间
- * @return
- * @Title: getTimeToSix
- * @date 2016年9月21日 下午5:14:00
- * @author Chris_He
- */
- public static String getTimeToNumber(Date time, Integer n) {
- String s = String.valueOf(time.getTime());
- return s.substring(s.length() - n, s.length());
- }
- /**
- * 获取当前时间上下午
- *
- * @return
- * @Title: getNowDateAmPm
- * @date 2017年8月29日 下午5:08:37
- * @author Chris_He
- */
- public static DateStyleEnum getNowDateAmPm() {
- GregorianCalendar ca = new GregorianCalendar();
- int i = ca.get(GregorianCalendar.AM_PM);
- if (i == 0) {
- return DateStyleEnum.AM;
- } else {
- return DateStyleEnum.PM;
- }
- }
- public static Date getAfterMinuteDate(Date date, int amount) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.MINUTE, amount);
- date = cal.getTime();
- // String theDate = sdf.format(date);
- return date;
- }
- public static Date getAfterSecondDate(Date date, int amount) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.SECOND, amount);
- date = cal.getTime();
- // String theDate = sdf.format(date);
- return date;
- }
- public static String formatFull(Date date) {
- if (date != null) {
- return DATE_FORMAT_YYYY_MM_DD_HH_MM_SS.format(date);
- }
- return null;
- }
- /**
- * 获取下一天00:00:00
- *
- * @return
- * @Title: getNextDay
- * @date 2018年12月22日 下午5:08:37
- * @author Chris_He
- */
- public static Date getNextDay(Date date) {
- return addDay(StringToDate(DateToString(date, DateStyleEnum.YYYY_MM_DD), DateStyleEnum.YYYY_MM_DD), 1);
- }
- public static void main(String[] args) {
- // System.out.println(DateUtil.getYear(new Date()));
- System.out.println(getHour("7:00"));
- }
- /**
- * 获取下一天指定时间
- *
- * @return
- * @Title: getNextDay
- * @date 2018年12月22日 下午5:08:37
- * @author Chris_He
- */
- public static Date getNextDayHour(Date date, int hour, int amount) {
- Date myDate = null;
- if (date != null) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- calendar.add(Calendar.DATE, amount);
- calendar.set(Calendar.HOUR_OF_DAY, hour); // 把当前时间小时变成0
- calendar.set(Calendar.MINUTE, 0); // 把当前时间分钟变成0
- calendar.set(Calendar.SECOND, 0); // 把当前时间秒数变成0
- calendar.set(Calendar.MILLISECOND, 0); // 把当前时间毫秒变成0
- myDate = calendar.getTime();
- }
- return myDate;
- }
- /**
- * 获取yyyy-MM
- *
- * @return
- */
- public static String getMonthStr() {
- return monthSdf.format(new Date());
- }
- /**
- * 获取时间范围内所有天数 yyyy-MM-dd
- *
- * @param startDate
- * @param endDate
- * @return
- */
- public static List<Date> getDayList(String startDate, String endDate) {
- Date dBegin = getZeroPoint(startDate);
- Date dEnd = getZeroPoint(endDate);
- List<Date> dateList = new ArrayList<>();
- Calendar calBegin = Calendar.getInstance();
- // 使用给定的 Date 设置此 Calendar 的时间
- calBegin.setTime(dBegin);
- Calendar calEnd = Calendar.getInstance();
- // 使用给定的 Date 设置此 Calendar 的时间
- calEnd.setTime(dEnd);
- // 此日期是否在指定日期之后
- dateList.add(dBegin);
- while (dEnd.after(calBegin.getTime())) {
- // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
- calBegin.add(Calendar.DAY_OF_MONTH, 1);
- dateList.add(calBegin.getTime());
- }
- return dateList;
- }
- /**
- * 获取时间范围内所有天 yyyy-MM-dd
- *
- * @param startDate
- * @param endDate
- * @return
- */
- public static List<Date> getDayList(Date startDate, Date endDate) {
- Date dBegin = getZeroPoint(startDate);
- Date dEnd = getZeroPoint(endDate);
- List<Date> dateList = new ArrayList<>();
- Calendar calBegin = Calendar.getInstance();
- // 使用给定的 Date 设置此 Calendar 的时间
- calBegin.setTime(dBegin);
- Calendar calEnd = Calendar.getInstance();
- // 使用给定的 Date 设置此 Calendar 的时间
- calEnd.setTime(dEnd);
- // 此日期是否在指定日期之后
- dateList.add(dBegin);
- while (dEnd.after(calBegin.getTime())) {
- // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
- calBegin.add(Calendar.DAY_OF_MONTH, 1);
- dateList.add(calBegin.getTime());
- }
- return dateList;
- }
- /**
- * 获取时间范围内所有天 yyyy-MM-dd
- *
- * @param startDate
- * @param endDate
- * @return
- */
- public static List<String> getDayStrList(Date startDate, Date endDate) {
- Date dBegin = getZeroPoint(startDate);
- Date dEnd = getZeroPoint(endDate);
- List<String> dateList = new ArrayList<>();
- Calendar calBegin = Calendar.getInstance();
- // 使用给定的 Date 设置此 Calendar 的时间
- calBegin.setTime(dBegin);
- Calendar calEnd = Calendar.getInstance();
- // 使用给定的 Date 设置此 Calendar 的时间
- calEnd.setTime(dEnd);
- // 此日期是否在指定日期之后
- dateList.add(getDate(dBegin));
- while (dEnd.after(calBegin.getTime())) {
- // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
- calBegin.add(Calendar.DAY_OF_MONTH, 1);
- dateList.add(getDate(calBegin.getTime()));
- }
- return dateList;
- }
- /**
- * 获取某个时间段内所有月份
- *
- * @param startDate
- * @param endDate
- * @return
- */
- public static List<Date> getMonthList(String startDate, String endDate) {
- List<Date> result = new ArrayList<>();
- Calendar min = Calendar.getInstance();
- Calendar max = Calendar.getInstance();
- min.setTime(StringToDate(startDate, DateStyleEnum.YYYY_MM));
- min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
- max.setTime(StringToDate(endDate, DateStyleEnum.YYYY_MM));
- max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
- Calendar curr = min;
- while (curr.before(max)) {
- result.add(curr.getTime());
- curr.add(Calendar.MONTH, 1);
- }
- return result;
- }
- /**
- * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
- *
- * @param date
- * @return
- */
- public static int getSeason(Date date) {
- int season = 0;
- Calendar c = Calendar.getInstance();
- c.setTime(date);
- int month = c.get(Calendar.MONTH);
- switch (month) {
- case Calendar.JANUARY:
- case Calendar.FEBRUARY:
- case Calendar.MARCH:
- season = 1;
- break;
- case Calendar.APRIL:
- case Calendar.MAY:
- case Calendar.JUNE:
- season = 2;
- break;
- case Calendar.JULY:
- case Calendar.AUGUST:
- case Calendar.SEPTEMBER:
- season = 3;
- break;
- case Calendar.OCTOBER:
- case Calendar.NOVEMBER:
- case Calendar.DECEMBER:
- season = 4;
- break;
- default:
- break;
- }
- return season;
- }
- /**
- * 取得季度月
- *
- * @param date
- * @return
- */
- public static Date[] getSeasonDate(Date date) {
- Date[] season = new Date[3];
- Calendar c = Calendar.getInstance();
- c.setTime(date);
- int nSeason = getSeason(date);
- if (nSeason == 1) {// 第一季度
- c.set(Calendar.MONTH, Calendar.JANUARY);
- season[0] = c.getTime();
- c.set(Calendar.MONTH, Calendar.FEBRUARY);
- season[1] = c.getTime();
- c.set(Calendar.MONTH, Calendar.MARCH);
- season[2] = c.getTime();
- } else if (nSeason == 2) {// 第二季度
- c.set(Calendar.MONTH, Calendar.APRIL);
- season[0] = c.getTime();
- c.set(Calendar.MONTH, Calendar.MAY);
- season[1] = c.getTime();
- c.set(Calendar.MONTH, Calendar.JUNE);
- season[2] = c.getTime();
- } else if (nSeason == 3) {// 第三季度
- c.set(Calendar.MONTH, Calendar.JULY);
- season[0] = c.getTime();
- c.set(Calendar.MONTH, Calendar.AUGUST);
- season[1] = c.getTime();
- c.set(Calendar.MONTH, Calendar.SEPTEMBER);
- season[2] = c.getTime();
- } else if (nSeason == 4) {// 第四季度
- c.set(Calendar.MONTH, Calendar.OCTOBER);
- season[0] = c.getTime();
- c.set(Calendar.MONTH, Calendar.NOVEMBER);
- season[1] = c.getTime();
- c.set(Calendar.MONTH, Calendar.DECEMBER);
- season[2] = c.getTime();
- }
- return season;
- }
- /**
- * 取得季度第一天
- *
- * @param date
- * @return
- */
- public static Date getFirstDateOfSeason(Date date) {
- return getFirstDateOfMonth(getSeasonDate(date)[0]);
- }
- /**
- * 取得季度最后一天
- *
- * @param date
- * @return
- */
- public static Date getLastDateOfSeason(Date date) {
- return getLastDateOfMonth(getSeasonDate(date)[2]);
- }
- /**
- * 取得月第一天
- *
- * @param date
- * @return
- */
- public static Date getFirstDateOfMonth(Date date) {
- Calendar c = Calendar.getInstance();
- c.setTime(date);
- c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
- return c.getTime();
- }
- /**
- * 取得月最后一天
- *
- * @param date
- * @return
- */
- public static Date getLastDateOfMonth(Date date) {
- Calendar c = Calendar.getInstance();
- c.setTime(date);
- c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
- return c.getTime();
- }
- /**
- * 时间范围内的每个季度开始时间
- *
- * @param startDate
- * @param endDate
- * @return
- */
- public static List<Date> getSeasonDateList(Date startDate, Date endDate) {
- List<Date> list = new ArrayList<>();
- Date firstDateOfSeason = getFirstDateOfSeason(startDate);
- list.add(firstDateOfSeason);
- Date lastDateOfSeason = getFirstDateOfSeason(endDate);
- Calendar c = Calendar.getInstance();
- c.setTime(firstDateOfSeason);
- while (c.getTime().before(lastDateOfSeason)) {
- c.add(Calendar.MONTH, 3);
- list.add(c.getTime());
- }
- return list;
- }
- /**
- * 获取所在年第一天
- *
- * @param date
- * @return
- */
- public static Date getFirstDayOfYear(Date date) {
- final Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.DAY_OF_YEAR, cal.getActualMinimum(Calendar.DAY_OF_YEAR));
- return cal.getTime();
- }
- /**
- * 获取所在年第一天
- *
- * @param
- * @return
- */
- public static Date getFirstDayOfYear(String year) {
- final Calendar cal = Calendar.getInstance();
- cal.setTime(StringToDate(year, DateStyleEnum.YYYY));
- cal.set(Calendar.DAY_OF_YEAR, cal.getActualMinimum(Calendar.DAY_OF_YEAR));
- return cal.getTime();
- }
- /**
- * 获取所在年最后一天
- *
- * @param date
- * @return
- */
- public static Date getLastDayOfYear(Date date) {
- final Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
- return cal.getTime();
- }
- /**
- * 获取所在年最后一天
- *
- * @param year
- * @return
- */
- public static Date getLastDayOfYear(String year) {
- final Calendar cal = Calendar.getInstance();
- cal.setTime(StringToDate(year, DateStyleEnum.YYYY));
- cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
- return cal.getTime();
- }
- /**
- * 时间范围内的每个年开始时间列表
- *
- * @param startDate
- * @param endDate
- * @return
- */
- public static List<Date> getYearDateList(Date startDate, Date endDate) {
- List<Date> list = new ArrayList<>();
- Date firstDate = getFirstDayOfYear(startDate);
- list.add(firstDate);
- Date lastDate = getFirstDayOfYear(endDate);
- Calendar c = Calendar.getInstance();
- c.setTime(firstDate);
- while (c.getTime().before(lastDate)) {
- c.add(Calendar.YEAR, 1);
- list.add(c.getTime());
- }
- return list;
- }
- public static String getChineseDate(Date date) {
- String chineseDate = null;
- SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
- return chineseDate;
- }
- public static StringBuilder getChineseTime(Date date) {
- String sign;
- StringBuilder chineseTime = new StringBuilder();
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
- String time = sdf.format(date);
- String[] split = time.split(":");
- if(split[0].equals("00")){
- chineseTime.append("零时");
- }else{
- String s = formatDigit(split[0]);
- String substring1 = s.substring(0, 1);
- String substring2 = s.substring(1, 2);
- if(substring1.equals("零")){
- }else if(substring1.equals("一")){
- chineseTime.append("十");
- }else{
- chineseTime.append(substring1).append("十");
- }
- if(!substring2.equals("零")){
- chineseTime.append(substring2);
- }
- chineseTime.append("时");
- }
- if(split[1].equals("00")){
- chineseTime.append("整");
- }else {
- String s = formatDigit(split[1]);
- String substring1 = s.substring(0, 1);
- String substring2 = s.substring(1, 2);
- if(substring1.equals("零")){
- }else if(substring1.equals("一")){
- chineseTime.append("十");
- }else{
- chineseTime.append(substring1).append("十");
- }
- if(!substring2.equals("零")){
- chineseTime.append(substring2);
- }
- chineseTime.append("分");
- }
- //
- return chineseTime;
- }
- private static String formatDigit(String sign) {
- StringBuilder sb = new StringBuilder();
- for(int i=0;i<sign.length();i++){
- String substring = sign.substring(i, i + 1);
- switch (substring){
- case "0":
- sb.append("零");
- break;
- case "1":
- sb.append("一");
- break;
- case "2":
- sb.append("二");
- break;
- case "3":
- sb.append("三");
- break;
- case "4":
- sb.append("四");
- break;
- case "5":
- sb.append("五");
- break;
- case "6":
- sb.append("六");
- break;
- case "7":
- sb.append("七");
- break;
- case "8":
- sb.append("八");
- break;
- case "9":
- sb.append("九");
- break;
- }
- }
- sign = sb.toString();
- return sign;
- }
- /**
- * 取出当前日期到指定日期的周数据差值
- *
- * @param date 指日 定日期
- * @param otherDate 当前日期
- * @return 周数差
- */
- public static long intervalWeeks(Date date, Date otherDate) {
- int intervalDays = getIntervalDays(date, otherDate) + 1;
- int i = intervalDays % 7;
- long weekNum = Math.abs((date.getTime() - otherDate.getTime()) / (7 * 60 * 60 * 24 * 1000));
- weekNum += 1;
- return weekNum;
- }
- public static long intervalWeeks(String date, String otherDate) {
- Date date1 = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
- Date date2 = DateUtils.StringToDate(otherDate, DateStyleEnum.YYYY_MM_DD);
- return intervalWeeks(date1, date2);
- }
- /**
- * 获取两个时间相差多少小时多少分钟
- *
- * @param startTime
- * @param endTime
- * @return
- */
- public static String getDistanceHourMin(Date startTime, Date endTime) {
- long hour = 0;
- long min = 0;
- try {
- long time1 = startTime.getTime();
- long time2 = endTime.getTime();
- long diff;
- if (time1 < time2) {
- diff = time2 - time1;
- } else {
- diff = time1 - time2;
- }
- hour = (diff / (60 * 60 * 1000));
- min = ((diff / (60 * 1000)) - hour * 60);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return hour + "小时" + min + "分";
- }
- public static Date getDateHourMin(Date date) {
- if (date == null) {
- return null;
- }
- return StringToDate(DateToString(date, DateStyleEnum.YYYY_MM_DD_HH_MM), DateStyleEnum.YYYY_MM_DD_HH_MM);
- }
- /**
- * 判断一个日期是否是周六、周日
- * @param date
- * @return
- * @throws ParseException
- */
- public static int isWeekend(String date) throws ParseException {
- DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
- Date bdate = format1.parse(date);
- Calendar cal = Calendar.getInstance();
- cal.setTime(bdate);
- if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
- return 1;
- } else {
- return 0;
- }
- }
- }
|