JAVA Web项目中常用工具类之 DateUtil,面试题经典

DateFormat dateFormat = new SimpleDateFormat(SHORT_FORMAT);

return dateFormat.parse(sDate);

}

public static Date parseDateNoTime(String sDate, String format) throws ParseException {

if (StringUtils.isBlank(format)) {

throw new ParseException("Null format. ", 0);

}

DateFormat dateFormat = new SimpleDateFormat(format);

return dateFormat.parse(sDate);

}

public static Date parseDateNoTimeWithDelimit(String sDate, String delimit) throws ParseException {

sDate = sDate.replaceAll(delimit, “”);

DateFormat dateFormat = new SimpleDateFormat(SHORT_FORMAT);

return dateFormat.parse(sDate);

}

public static Date parseDateLongFormat(String sDate) throws ParseException {

DateFormat dateFormat = new SimpleDateFormat(LONG_FORMAT);

return dateFormat.parse(sDate);

}

public static Date parseDateNewFormat(String sDate) throws ParseException {

DateFormat dateFormat = new SimpleDateFormat(datetimeFormat);

dateFormat.setLenient(false);

return dateFormat.parse(sDate);

}

public static Date parseDateNoSecondFormat(String sDate) throws ParseException {

DateFormat dateFormat = new SimpleDateFormat(noSecondFormat);

dateFormat.setLenient(false);

return dateFormat.parse(sDate);

}

public static Date parseDateWebFormat(String sDate) throws ParseException {

DateFormat dateFormat = new SimpleDateFormat(webFormat);

dateFormat.setLenient(false);

return dateFormat.parse(sDate);

}

public static Date parseDateWebMonthFormat(String sDate) throws ParseException {

DateFormat dateFormat = new SimpleDateFormat(webMonthFormat);

dateFormat.setLenient(false);

return dateFormat.parse(sDate);

}

/**

  • 计算当前时间几小时之后的时间

  • @param date

  • @param hours

  • @return

*/

public static Date addHours(Date date, long hours) {

return addMinutes(date, hours * 60);

}

/**

  • 计算当前时间几分钟之后的时间

  • @param date

  • @param minutes

  • @return

*/

public static Date addMinutes(Date date, long minutes) {

return addSeconds(date, minutes * 60);

}

/**

  • @param date1

  • @param secs

  • @return

*/

public static Date addSeconds(Date date1, long secs) {

return new Date(date1.getTime() + secs * 1000);

}

/**

  • 判断输入的字符串是否为合法的小时

  • @param hourStr

  • @return true/false

*/

public static boolean isValidHour(String hourStr) {

if (!StringUtils.isEmpty(hourStr) && StringUtils.isNumeric(hourStr)) {

int hour = Integer.parseInt(hourStr);

return hour >= 0 && hour <= 23;

}

return false;

}

/**

  • 判断输入的字符串是否为合法的分或秒

  • @param str

  • @return true/false

*/

public static boolean isValidMinuteOrSecond(String str) {

if (!StringUtils.isEmpty(str) && StringUtils.isNumeric(str)) {

int hour = Integer.parseInt(str);

return hour >= 0 && hour <= 59;

}

return false;

}

/**

  • 取得新的日期

  • @param date1 日期

  • @param days 天数

  • @return 新的日期

*/

public static Date addDays(Date date1, long days) {

Calendar cal = Calendar.getInstance();

cal.setTime(date1);

cal.add(Calendar.DATE, (int) days);

return cal.getTime();

}

public static String getTomorrowDateString(String sDate) throws ParseException {

Date aDate = parseDateNoTime(sDate);

aDate = addSeconds(aDate, ONE_DAY_SECONDS);

return getDateString(aDate);

}

public static String getTomorrowDateNewFMTString(String sDate) throws ParseException {

Date aDate = parseDateWebFormat(sDate);

aDate = addDays(aDate, 1);

return getWebDateString(aDate);

}

public static String getTomorrowDateNewFormatString(String sDate) throws ParseException {

Date aDate = parseDateNewFormat(sDate);

aDate = addDays(aDate, 1);

return getWebDateString(aDate);

}

public static String getLongDateString(Date date) {

DateFormat dateFormat = new SimpleDateFormat(LONG_FORMAT);

return getDateString(date, dateFormat);

}

public static String getNewFormatDateString(Date date) {

DateFormat dateFormat = new SimpleDateFormat(datetimeFormat);

return getDateString(date, dateFormat);

}

public static String getWebFormatDateString(Date date) {

DateFormat dateFormat = new SimpleDateFormat(webFormat);

return getDateString(date, dateFormat);

}

public static String getConcurrentFormatDateString(Date date) {

DateFormat dateFormat = new SimpleDateFormat(concurrentFormat);

return getDateString(date, dateFormat);

}

public static String getDateString(Date date, DateFormat dateFormat) {

if (date == null || dateFormat == null) {

return null;

}

return dateFormat.format(date);

}

public static String getYesterDayDateString(String sDate) throws ParseException {

Date aDate = parseDateNoTime(sDate);

aDate = addSeconds(aDate, -ONE_DAY_SECONDS);

return getDateString(aDate);

}

/**

  • @return 当天的时间格式化为"yyyyMMdd"

*/

public static String getDateString(Date date) {

DateFormat df = getNewDateFormat(SHORT_FORMAT);

return df.format(date);

}

public static String getWebDateString(Date date) {

DateFormat dateFormat = getNewDateFormat(webFormat);

return getDateString(date, dateFormat);

}

/**

  • 取得“X年X月X日”的日期格式

  • @param date

  • @return

*/

public static String getChineseDateString(Date date) {

DateFormat dateFormat = getNewDateFormat(chineseDtFormat);

return getDateString(date, dateFormat);

}

public static String getTodayString() {

DateFormat dateFormat = getNewDateFormat(SHORT_FORMAT);

return getDateString(new Date(), dateFormat);

}

public static String getTomorrowString() {

DateFormat dateFormat = getNewDateFormat(SHORT_FORMAT);

return getDateString(DateUtil.addDays(new Date(), 1), dateFormat);

}

public static String getTimeString(Date date) {

DateFormat dateFormat = getNewDateFormat(timeFormat);

return getDateString(date, dateFormat);

}

public static String getBeforeDayString(int days) {

Date date = new Date(System.currentTimeMillis() - ONE_DAY_MILL_SECONDS * days);

DateFormat dateFormat = getNewDateFormat(SHORT_FORMAT);

return getDateString(date, dateFormat);

}

/**

  • 取得两个日期间隔毫秒数(日期1-日期2)

  • @param one 日期1

  • @param two 日期2

  • @return 间隔秒数

*/

public static long getDiffMillis(Date one, Date two) {

Calendar sysDate = new GregorianCalendar();

sysDate.setTime(one);

Calendar failDate = new GregorianCalendar();

failDate.setTime(two);

return sysDate.getTimeInMillis() - failDate.getTimeInMillis();

}

/**

  • 取得两个日期间隔秒数(日期1-日期2)

  • @param one 日期1

  • @param two 日期2

  • @return 间隔秒数

*/

public static long getDiffSeconds(Date one, Date two) {

Calendar sysDate = new GregorianCalendar();

sysDate.setTime(one);

Calendar failDate = new GregorianCalendar();

failDate.setTime(two);

return (sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / 1000;

}

/**

  • 取得两个日期间隔分钟数(日期1-日期2)

  • @param one 日期1

  • @param two 日期2

  • @return 间隔秒数

*/

public static long getDiffMinutes(Date one, Date two) {

Calendar sysDate = new GregorianCalendar();

sysDate.setTime(one);

Calendar failDate = new GregorianCalendar();

failDate.setTime(two);

return (sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / (60 * 1000);

}

/**

  • 取得两个日期的间隔天数

  • @param one

  • @param two

  • @return 间隔天数

*/

public static long getDiffDays(Date one, Date two) {

Calendar sysDate = new GregorianCalendar();

sysDate.setTime(one);

Calendar failDate = new GregorianCalendar();

failDate.setTime(two);

return (sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / (24 * 60 * 60 * 1000);

}

/**

  • 取得两个日期相差的自然日

  • @param date1

  • @param date2

  • @return

*/

public static long getDiffNaturalDays(Date date1, Date date2) throws ParseException {

return Math.abs(getDiffNaturalDayNotAbs(date1, date2));

}

/**

  • 取得两个日期相差的自然日

  • @param date1

  • @param date2

  • @return

*/

public static long getDiffNaturalDayNotAbs(Date date1, Date date2) throws ParseException {

long diffDays;

DateFormat dateFormat = new SimpleDateFormat(webFormat);

//去掉时分秒

String dateStr1 = dateFormat.format(date1);

String dateStr2 = dateFormat.format(date2);

diffDays = (dateFormat.parse(dateStr1).getTime() - dateFormat.parse(dateStr2).getTime()) / (24 * 60 * 60 * 1000);

return diffDays;

}

public static String getBeforeDayString(String dateString, int days) throws ParseException {

DateFormat df = getNewDateFormat(SHORT_FORMAT);

Date date = df.parse(dateString);

date = new Date(date.getTime() - ONE_DAY_MILL_SECONDS * days);

return df.format(date);

}

public static boolean isValidShortDateFormat(String strDate) {

if (strDate == null || strDate.length() != SHORT_FORMAT.length()) {

return false;

}

try {

// ---- 避免日期中输入非数字 ----

Integer.parseInt(strDate);

} catch (NumberFormatException e) {

return false;

}

DateFormat df = getNewDateFormat(SHORT_FORMAT);

try {

df.parse(strDate);

} catch (ParseException e) {

return false;

}

return true;

}

public static boolean isValidShortDateFormat(String strDate, String delimiter) {

String temp = strDate.replaceAll(delimiter, “”);

return isValidShortDateFormat(temp);

}

/**

  • 判断表示时间的字符是否为符合yyyyMMddHHmmss格式

  • @param strDate

  • @return

*/

public static boolean isValidLongDateFormat(String strDate) {

if (strDate.length() != LONG_FORMAT.length()) {

return false;

}

try {

Long.parseLong(strDate); // ---- 避免日期中输入非数字 ----

} catch (Exception NumberFormatException) {

return false;

}

DateFormat df = getNewDateFormat(LONG_FORMAT);

try {

df.parse(strDate);

} catch (ParseException e) {

return false;

}

return true;

}

/**

  • 判断表示时间的字符是否为符合yyyyMMddHHmmss格式

  • @param strDate

  • @param delimiter

  • @return

*/

public static boolean isValidLongDateFormat(String strDate, String delimiter) {

String temp = strDate.replaceAll(delimiter, “”);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
img

最后

编程基础的初级开发者,计算机科学专业的学生,以及平时没怎么利用过数据结构与算法的开发人员希望复习这些概念为下次技术面试做准备。或者想学习一些计算机科学的基本概念,以优化代码,提高编程技能。这份笔记都是可以作为参考的。

名不虚传!字节技术官甩出的"保姆级"数据结构与算法笔记太香了

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

238)]
[外链图片转存中…(img-iWgoa22q-1712726284239)]
[外链图片转存中…(img-W1RjyWKR-1712726284239)]
[外链图片转存中…(img-LXPeul2d-1712726284240)]
[外链图片转存中…(img-xVnhmZzS-1712726284240)]
[外链图片转存中…(img-7KKVLvwe-1712726284240)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
[外链图片转存中…(img-SkJSoCYp-1712726284240)]

最后

编程基础的初级开发者,计算机科学专业的学生,以及平时没怎么利用过数据结构与算法的开发人员希望复习这些概念为下次技术面试做准备。或者想学习一些计算机科学的基本概念,以优化代码,提高编程技能。这份笔记都是可以作为参考的。

名不虚传!字节技术官甩出的"保姆级"数据结构与算法笔记太香了

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-k2U3zCWF-1712726284241)]

  • 24
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值