java實(shí)現(xiàn)簡(jiǎn)單日期計(jì)算功能
本文講的java日期計(jì)算比較偏,用到的地方很少(比如獲取今天所在周的周一或者周日,獲取今天是本月的第幾周...),這些方法是以前做項(xiàng)目遺留下來的,現(xiàn)在整理一下,跟大家分享。
工具類主要有一下方法:
public static Date getFirstMondayOfMonth(String dateString, String dateFormat) throws Exception
獲取指定月份的第一個(gè)星期一,比如2014-12 月的第一個(gè)周一是2014-12-01
public static int figureWeekIndexOfMonth(String dateString, String dateFormat) throws Exception
計(jì)算指定時(shí)間屬于月份中的第幾周,比如2014-12月的第一周是1號(hào)到7號(hào),那么2014-12-05 就是12月的第一周,2014-12-12 就是第二周
public static String getMondyOfToday(String format)
獲取今天所在周的星期一, 返回一個(gè)時(shí)間字符串。 如今天是2014-12-8,那么返回的是: 2014-12-08 (今天剛好是本周周一)
public static Date getSundayOfToday()
獲取今天所在周的星期天, 如今天是2014-12-8,那么返回的是 2014-12-14
下面是工具類的詳細(xì)代碼:
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @文件名稱 :DateUtil.java
* @所在包 :com.nerve.human.common.util
* @功能描述 :
* 時(shí)間格式工具類
* @創(chuàng)建者 :集成顯卡 1053214511@qq.com
* @公司:IBM GDC
* @創(chuàng)建日期 :2013-4-9
* @log :
*/
public class DateUtil {
public static Date toDate(String timeString, String format) throws Exception{
return new SimpleDateFormat(format).parse(timeString);
}
/**
*
* @method name: toString
* @return type: String
* @param date
* @param format
* @return
*/
public static String toString(Date date, String format){
String strTime = null;
try {
SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
strTime = simpledateformat.format(date);
} catch (Exception ex) {
System.err.println("格式化日期錯(cuò)誤 : " + ex.getMessage());
}
return strTime;
}
/**
* 獲取當(dāng)月的第一個(gè)星期一(以中國(guó)為例)
* @method name: getFirstMonday
* @return type: void
*/
public static Date getFirstMondayOfMonth(String month) throws Exception{
return getFirstMondayOfMonth(month, "yyyy-MM");
}
/**
* 獲取當(dāng)月的第一個(gè)星期一(以中國(guó)為例)
* @method name: getFirstMonday
* @return type: void
*/
public static Date getFirstMondayOfMonth(String dateString, String dateFormat) throws Exception{
Date date = toDate(dateString, dateFormat);
Calendar c = Calendar.getInstance();
c.setTime(date);
int step = (9 - c.get(Calendar.DAY_OF_WEEK)) % 7;
c.add(Calendar.DAY_OF_YEAR, step);
return c.getTime();
}
/**
* 計(jì)算指定時(shí)間屬于月份中的第幾周
* 比如2014-12月的第一周是1號(hào)到7號(hào)
* 那么2014-12-05 就是12月的第一周
* 2014-12-12 就是第二周
*
* @method name: figureWeekIndexOfMonth
* @return type: int
*
* @param date
* @return
*/
public static int figureWeekIndexOfMonth(String dateString, String dateFormat) throws Exception{
Calendar c = Calendar.getInstance();
Date curDate = toDate(dateString, dateFormat);
c.setTime(curDate);
int day = c.get(Calendar.DAY_OF_MONTH);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date firstMondy = getFirstMondayOfMonth(sdf.format(c.getTime()));
c.setTime(firstMondy);
int index = 0;
do{
c.add(Calendar.DAY_OF_MONTH, 7);
index ++;
}
while(c.get(Calendar.DAY_OF_MONTH) < day);
return index;
}
/**
* 獲取今天所在周的星期一
* @method name: getMondyOfToday
* @return type: String
*
* @return
*/
public static String getMondyOfToday(String format){
Calendar c = Calendar.getInstance();
int step = c.get(Calendar.DAY_OF_WEEK);
//星期天
if(step == 1)
step = 6;
else
step -= 2;
c.add(Calendar.DAY_OF_YEAR, -step);
return toString(c.getTime(), format);
}
/**
* 獲取今天所在周的星期天
* @method name: getMondyOfToday
* @return type: String
*
* @return
*/
public static Date getSundayOfToday(){
Calendar c = Calendar.getInstance();
int step = c.get(Calendar.DAY_OF_WEEK);
if(step != Calendar.SUNDAY)
c.add(Calendar.DAY_OF_YEAR, 8-step);
return c.getTime();
}
/**
* 獲取指定時(shí)間所在的星期天
* @param date
* @return
*/
public static Date getSundayOfDate(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
int step = c.get(Calendar.DAY_OF_WEEK);
if(step != Calendar.SUNDAY)
c.add(Calendar.DAY_OF_YEAR, 8-step);
return c.getTime();
}
}
來個(gè)測(cè)試截圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 常用的java日期比較和日期計(jì)算方法小結(jié)
- Java計(jì)算兩個(gè)日期時(shí)間之間的天數(shù)最簡(jiǎn)方法
- Java編程實(shí)現(xiàn)計(jì)算兩個(gè)日期的月份差實(shí)例代碼
- Java中使用LocalDate根據(jù)日期來計(jì)算年齡的實(shí)現(xiàn)方法
- java計(jì)算兩個(gè)日期之前的天數(shù)實(shí)例(排除節(jié)假日和周末)
- Java簡(jiǎn)單計(jì)算兩個(gè)日期月數(shù)差的方法
- java計(jì)算兩個(gè)日期中間的時(shí)間
- 利用Java中Calendar計(jì)算兩個(gè)日期之間的天數(shù)和周數(shù)
- 利用Java計(jì)算某個(gè)日期是星期幾
- JAVA計(jì)算兩個(gè)日期相差的實(shí)例
相關(guān)文章
Java批量插入數(shù)據(jù)的代碼實(shí)現(xiàn)
日常工作或者學(xué)習(xí)中,可能會(huì)遇到批量插入數(shù)據(jù)的需求,一般情況下數(shù)據(jù)量少的時(shí)候,我們會(huì)直接調(diào)用批量接口插入數(shù)據(jù)即可,當(dāng)數(shù)據(jù)量特別大時(shí),我們就會(huì)用到分批插入數(shù)據(jù),所以本文給大家介紹了Java批量插入數(shù)據(jù)的代碼實(shí)現(xiàn),需要的朋友可以參考下2024-01-01
SpringBoot實(shí)現(xiàn)前端驗(yàn)證碼圖片生成和校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)前端驗(yàn)證碼圖片生成和校驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Java使用Redisson分布式鎖實(shí)現(xiàn)原理
Redisson分布式鎖 之前的基于注解的鎖有一種鎖是基本redis的分布式鎖,這篇文章主要介紹了Java使用Redisson分布式鎖實(shí)現(xiàn)原理,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
深度解析Spring內(nèi)置作用域及其在實(shí)踐中的應(yīng)用
這篇文章主要詳細(xì)介紹了Spring內(nèi)置的作用域類型及其在實(shí)踐中的應(yīng)用,文中有詳細(xì)的代碼示例,對(duì)我們的餓學(xué)習(xí)或工作有一定的參考價(jià)值,感興趣的同學(xué)可以借鑒閱讀2023-06-06
SpringBoot啟動(dòng)報(bào)錯(cuò)Whitelabel Error Page: This&nbs
當(dāng)我們使用Spring Boot框架開發(fā)Web應(yīng)用時(shí),有時(shí)會(huì)遇到啟動(dòng)報(bào)錯(cuò)信息為"Whitelabel Error Page: This application has no explicit mapping for",種報(bào)錯(cuò)信息意味著我們的應(yīng)用缺少某個(gè)URL映射的配置,導(dǎo)致請(qǐng)求無法處理,在本篇文章中,我們將詳細(xì)討論如何解決這個(gè)問題2024-03-03
Springboot @Transactional使用時(shí)需注意的幾個(gè)問題記錄
本文詳細(xì)介紹了Spring Boot中使用`@Transactional`注解進(jìn)行事務(wù)管理的多個(gè)方面,包括事務(wù)的隔離級(jí)別(如REPEATABLE_READ)和傳播行為(如REQUIRES_NEW),并指出了在同一個(gè)類中調(diào)用事務(wù)方法時(shí)可能遇到的問題以及解決方案,感興趣的朋友跟隨小編一起看看吧2025-01-01

