获取本月周次和日期时间段信息

package com.wdcloud.monitoring.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.wdcloud.monitoring.Model.WeekInfoModel;

public class DateUtils {

    //是否打印日志
    private static boolean isLog = false;

    /**
     * 获取本月周次和日期时间段信息
     *
     * @return
     */
    public static List<WeekInfoModel> getThisMonthWeekDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<WeekInfoModel> list = new ArrayList<WeekInfoModel>();

        // 1 获取本月的第一天
        Date firstDayOfMonth = getFirstDayOfMonth();
        Calendar firstDayOfMonthCal = Calendar.getInstance();
        firstDayOfMonthCal.setFirstDayOfWeek(Calendar.MONDAY);
        firstDayOfMonthCal.setTime(firstDayOfMonth);

        // 2 获取本月第一周第一天
        Date firstWeekFirstDay = getFirstDayOfFirstWeekOfMonth();
        Calendar firstWeekFirstDayCal = Calendar.getInstance();
        firstWeekFirstDayCal.setFirstDayOfWeek(Calendar.MONDAY);
        firstWeekFirstDayCal.setTime(firstWeekFirstDay);

        // 3 获取本次周期起止时间
        Date sDate = null;// 本月第一周第一天
        Date eDate = null;// 本月最后一周最后一天
        Calendar sCal = Calendar.getInstance();
        Calendar eCal = Calendar.getInstance();
        sCal.setFirstDayOfWeek(Calendar.MONDAY);
        sCal.setFirstDayOfWeek(Calendar.MONDAY);

        // 获取本月第一天是周几
        int weekDay = firstDayOfMonthCal.get(Calendar.DAY_OF_WEEK) - 1;

        // 如果是周末,起始时间就是本月第一周第一天
        // 否则起始时间就是本月第一周第一天 倒退7天
        if (weekDay == 6 || weekDay == 0 || weekDay == 1) {// 周六或周天
            sDate = firstWeekFirstDay;
        } else {
            firstWeekFirstDayCal.getTime();
            firstWeekFirstDayCal.add(Calendar.DATE, -7);
            sDate = firstWeekFirstDayCal.getTime();
        }
        sCal.setTime(sDate);

        // 结束时间是起始时间+34天(5周)
        eCal.setTime(sDate);
        eDate = eCal.getTime();
        eCal.add(Calendar.DATE, 34);
        eDate = eCal.getTime();

        Date cDate = new Date();
        if (cDate.getTime() < eDate.getTime()) {
            eDate = cDate;
        }

        if (isLog) {
            System.out.println(" 本月统计起止时间是:" + format.format(sDate) + " 到 "
                    + format.format(eDate));
            System.out
                    .println("-------------------------------------------------------------------------------");
        }

        // 4 循环得到周次信息
        for (int i = 0; i < 5; i++) {// 循环5周次
            WeekInfoModel model = new WeekInfoModel();
            Calendar endTime = Calendar.getInstance();
            endTime.setFirstDayOfWeek(Calendar.MONDAY);
            endTime.setTime(sDate);
            endTime.set(Calendar.HOUR_OF_DAY, 23);
            endTime.set(Calendar.MINUTE, 59);
            endTime.set(Calendar.SECOND, 59);
            endTime.add(Calendar.DATE, 6);

            model.setIndexOfWeek(i + 1);
            model.setStartDateOfWeek(sDate);
            model.setEndDateOfWeek(endTime.getTime());

            Date currDate = new Date();
            // 如果当前时间在某个周次内
            if (sDate.getTime() < currDate.getTime()
                    && endTime.getTime().getTime() > currDate.getTime()) {
                model.setEndDateOfWeek(currDate);

                if (isLog) {
                    System.out.println(" 本月第" + (i + 1) + "周起止时间是:"
                            + format.format(sDate) + " 到 "
                            + format.format(currDate));
                }
                list.add(model);
                break;
            }
            if (isLog) {
                System.out.println(" 本月第" + (i + 1) + "周起止时间是:"
                        + format.format(sDate) + " 到 "
                        + format.format(endTime.getTime()));
            }
            list.add(model);

            sCal.add(Calendar.DATE, 7);
            sDate = sCal.getTime();
        }

        return list;
    }

    // 获取本月第一天
    public static Date getFirstDayOfMonth() {
        Calendar now = Calendar.getInstance();
//        Date date = new Date(2015 - 1900, 12 - 1, 1);
//        now.setTime(date);
        now.set(Calendar.DATE, now.getActualMinimum(Calendar.DATE));
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        return now.getTime();
    }

    // 获取本月第一周第一天
    public static Date getFirstDayOfFirstWeekOfMonth() {
        Calendar now = Calendar.getInstance();
        now.setFirstDayOfWeek(Calendar.MONDAY);
        now.setTime(getFirstDayOfMonth());

        int i = 1;
        while (now.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            now.set(Calendar.DAY_OF_MONTH, i++);// 设置这个月的星期1 为几号
        }

        Date firstMonday = now.getTime();// 取得日期和时间
        String dtStr = new SimpleDateFormat("YYYY-MM-dd").format(firstMonday);// 格式化日期
        if (isLog) {
            System.out
                    .println("-------------------------------------------------------------------------------");
            System.out.println(" 本月第一周第一天是:" + dtStr);
            System.out
                    .println("-------------------------------------------------------------------------------");
        }
        return now.getTime();
    }

    /**
     * 获取本周周次和日期时间段信息
     *
     * @return
     */
    public static WeekInfoModel getThisWeekDate() {

        WeekInfoModel model = new WeekInfoModel();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 根据今天的时间获取本周属于本月的第几周
        Calendar now = Calendar.getInstance();
        now.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 获取本周一的日期

        int wom = now.get(Calendar.WEEK_OF_MONTH);
        model.setIndexOfWeek(wom);

        // 根据今天的时间获取本周的开始时间
        now.set(Calendar.HOUR_OF_DAY, 0);
        now.set(Calendar.MINUTE, 0);
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);
        System.out.println("********" + now.get(Calendar.YEAR) + "年"
                + ((now.get(Calendar.MONTH)) + 1) + "月第" + wom + "周星期一的日期是:"
                + df.format(now.getTime()));
        model.setStartDateOfWeek(now.getTime());

        // 根据今天的时间获取本周的结束时间
        now.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        now.add(Calendar.WEEK_OF_YEAR, 1);
        now.set(Calendar.HOUR_OF_DAY, 23);
        now.set(Calendar.MINUTE, 59);
        now.set(Calendar.SECOND, 59);
        now.set(Calendar.MILLISECOND, 999);
        System.out.println("********" + now.get(Calendar.YEAR) + "年"
                + ((now.get(Calendar.MONTH)) + 1) + "月第" + wom + "周星期天的日期是:"
                + df.format(now.getTime()));
        model.setEndDateOfWeek(now.getTime());

        return model;
    }

}
时间: 2024-10-16 14:23:47

获取本月周次和日期时间段信息的相关文章

c# 获取一年中的周/根据一年中的第几周获取该周的开始日期与结束日期

/// <summary> /// 获取一年中的周 /// </summary> /// <param name="dt">日期</param> /// <returns></returns> public static int GetWeekOfYear(DateTime dt) { System.Globalization.GregorianCalendar gc = new System.Globalizat

java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import org.junit.Test; public class TestDateUtil { //1.获取当月第一天 @Test public void testForDate(){ //规定返回日期格式 SimpleDateFormat sf=ne

关于时间的操作(Java版)——获取距离系统时间N天后的日期时间信息

import java.util.Calendar; import java.util.TimeZone; public class Test { /** * 获取距离系统时间N天后的日期时间信息 * * @author GaoHuanjie */ public String getDateAfterNdays(int days){ Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8")); cale

获取日期时间信息

1 function netDate:string; 2 var//网页头获取日期时间信息 3 v:Variant; 4 url,GetText:string; 5 dd,mm,yy:string; 6 ii,iGMT:integer; 7 TT:Tdate; 8 function MonthStr(x:string):integer; 9 begin 10 if x='jan' then 11 result:=1 12 else if x='feb' then 13 result:=2 14

js获取本月、三个月、今年的日期插件dateHelp

最近看了一些关于面向对象的知识,最近工作中在做统计查询的时候需要用到本月.近三个月.今年的日期范围,所以下面用用面向对象的思想写了一个获取日期的插件,大家可以借鉴使用. 直接通过new DateHelp就可以调用了 var myDate = new DateHelp({ date:'2015-02-01',//从此日期开始计算 format:'yyyy/MM/dd'}); myDate.getThisMonth();myDate.getThreeMonth();myDate.getThisYea

根据周次获取周次的日期范围

根据周次获取周次的日期范围 /** * 返回某年某月周次范围 * @param year 年份 * @param weeks 周次 * @return 返回周次的日期范围 */ function getXDate(year,weeks){ var date = new Date(year,"0","1"); var time = date.getTime(); // 获取当前星期几,0:星期一 .... var _week = date.getDay(); //当这

js获取某周、某月、下月、某季度的开始日期、结束日期及判断日期第几周

/** * 获取某周.某月.上月.某季度的开始日期.结束日期及判断日期第几周 */ //格式化日期:yyyy-MM-dd function formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.getMonth()+1; var myweekday = date.getDate(); if(mymonth < 10){ mymonth = "0" + mymonth; } if(myweekd

PHP获取日期时间信息

getdate函数 描述:可以获取日期/时间信息 语法:array getdate( [ int timestamp ] ) 返回一个数组 例: Array ( [seconds] => 30 [minutes] => 59 [hours] => 10 [mday] => 31 [wday] => 2 [mon] => 3 [year] =>2020 [yday] =>90 [weekday] => Tuesday [month] => Marc

获取某一天所在周的开始日期和结束日期

指定一个日期,获取其所属周的开始日期和结束日期.以及前一周和后一周的开始日期和结束日期. Insus.NET把这个功能写成一个自定义函数Table-values Functions. 如何使用上面这个函数,列举例子说明: 原文地址:https://www.cnblogs.com/insus/p/10843667.html