java實(shí)現(xiàn)簡單日期計(jì)算功能

本文講的java日期計(jì)算比較偏,用到的地方很少(比如獲取今天所在周的周一或者周日,獲取今天是本月的第幾周...),這些方法是以前做項(xiàng)目遺留下來的,現(xiàn)在整理一下,跟大家分享。

創(chuàng)新互聯(lián)公司是一家專業(yè)提供惠水企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、外貿(mào)營銷網(wǎng)站建設(shè)、H5建站、小程序制作等業(yè)務(wù)。10年已為惠水眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jì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è)星期一(以中國為例)
 * @method name: getFirstMonday
 * @return type: void
 */
 public static Date getFirstMondayOfMonth(String month) throws Exception{
 return getFirstMondayOfMonth(month, "yyyy-MM");
 }
 
 /**
 * 獲取當(dāng)月的第一個(gè)星期一(以中國為例)
 * @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è)試截圖:

java實(shí)現(xiàn)簡單日期計(jì)算功能

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文名稱:java實(shí)現(xiàn)簡單日期計(jì)算功能
標(biāo)題來源:http://muchs.cn/article22/ihsgcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站營銷、全網(wǎng)營銷推廣、網(wǎng)站維護(hù)、品牌網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司