欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2018年11月30日 14:36:15   作者:集成顯卡  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡單日期計(jì)算功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文講的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è)星期一(以中國為例)
 * @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è)試截圖:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?boot數(shù)據(jù)庫依賴詳解

    Spring?boot數(shù)據(jù)庫依賴詳解

    這篇文章主要介紹了Spring?boot數(shù)據(jù)庫依賴,需要的朋友可以參考下
    2023-09-09
  • SpringBoot數(shù)據(jù)庫恢復(fù)的兩種方法mysqldump和mysqlbinlog

    SpringBoot數(shù)據(jù)庫恢復(fù)的兩種方法mysqldump和mysqlbinlog

    binlog用來實(shí)現(xiàn)主從復(fù)制,也常用來誤刪數(shù)據(jù)庫找回丟失的記錄,本文主要介紹了SpringBoot數(shù)據(jù)庫恢復(fù)的兩種方法mysqldump和mysqlbinlog,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • 一文搞懂Java中對(duì)象池的實(shí)現(xiàn)

    一文搞懂Java中對(duì)象池的實(shí)現(xiàn)

    池化并不是什么新鮮的技術(shù),它更像一種軟件設(shè)計(jì)模式,主要功能是緩存一組已經(jīng)初始化的對(duì)象,以供隨時(shí)可以使用。本文將為大家詳細(xì)講講Java中對(duì)象池的實(shí)現(xiàn),需要的可以參考一下
    2022-07-07
  • springboot配置https訪問的方法

    springboot配置https訪問的方法

    這篇文章主要介紹了springboot配置https訪問的方法,需要的朋友可以參考下
    2018-11-11
  • 解決DataOutputStream亂碼的問題

    解決DataOutputStream亂碼的問題

    這篇文章主要介紹了DataOutputStream亂碼問題的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • JavaWeb簡單用戶登錄注冊(cè)實(shí)例代碼(有驗(yàn)證碼)

    JavaWeb簡單用戶登錄注冊(cè)實(shí)例代碼(有驗(yàn)證碼)

    這篇文章主要介紹了JavaWeb簡單用戶登錄注冊(cè)實(shí)例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn)

    5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn)

    這篇文章主要介紹了5分鐘搭建SpringCloud Eureka服務(wù)注冊(cè)中心的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java設(shè)計(jì)模式之裝飾模式詳解

    Java設(shè)計(jì)模式之裝飾模式詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式中的裝飾者模式,裝飾者模式即Decorator?Pattern,裝飾模式是在不必改變?cè)愇募褪褂美^承的情況下,動(dòng)態(tài)地?cái)U(kuò)展一個(gè)對(duì)象的功能,裝飾模式又名包裝模式。裝飾器模式以對(duì)客戶端透明的方式拓展對(duì)象的功能,是繼承關(guān)系的一種替代方案
    2022-07-07
  • 詳解Java?Map中三個(gè)冷門容器的使用

    詳解Java?Map中三個(gè)冷門容器的使用

    本篇文章主要講解下Map家族中3個(gè)相對(duì)冷門的容器,分別是WeakHashMap、EnumMap、IdentityHashMap,?想必大家在平時(shí)的工作中也很少用到,或者壓根不知道他們的特性以及適用場景,本篇文章就帶你一探究竟
    2022-12-12
  • 詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法

    詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法

    這篇文章主要介紹了詳解Java的內(nèi)置異常以及創(chuàng)建自定義異常子類的方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-09-09

最新評(píng)論