今年从本周开始在给定的日期 [英] week of year starting at given date

查看:130
本文介绍了今年从本周开始在给定的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点难以解释,但我需要启动一个指定日期之后计数周。然后,我必须得到当前的日期,并检查它是什么的周数。

It's kind of hard to explain but I need to start counting weeks after a given date. Then I have to get the current date and check what week number it is.

已经尝试过一些东西,不明白这一点...
谢谢!

Already tried a few things and can't figure this out... Thanks!

P.S。也许得到了7两个日期和鸿沟,在天有什么区别?我会怎么做呢?

p.s. Maybe get the difference in days between two dates and divide by 7? How would I do this?

推荐答案

我想你提到的获得差异的方法将正常工作。你刚才设置的给定日期为一年中的一天,然后拿到一年的​​当日,使用Calendar类。

I think the method you mentioned of getting the difference would work fine. You'd just set the given date as one day of the year, and then get the current day of the year, using the Calendar class.

//Set Given Date to what you want it to be, eg 10th March 2006. (Months are indexed from 0-11)
Calendar calendarGivenDate = Calendar.getInstance();
            calendarGivenDate.set(Calendar.DAY_OF_MONTH, 10);
            calendarGivenDate.set(Calendar.MONTH_OF_YEAR, 2);
            calendarGivenDate.set(Calendar.YEAR, 2006);

//Receive the day of the year for what you previously set
    int givenDateDayOfYear = calendarGivenDate.get(Calendar.DAY_OF_YEAR);

//Receive current day of year
Calendar calendarCurrentDate = Calendar.getInstance();
    int currentDateDayOfYear = calendarCurrentDate.get(Calendar.DAY_OF_YEAR);

//Get difference in number of years
            int currentYear = calendarCurrentDate.get(Calendar.YEAR);
            int givenYear = calendarGivenDate.get(Calendar.YEAR);
            int yearDifference = currentYear - givenYear;


//Find difference, divide by 7 (Round value down to get the difference in whole weeks)
double differenceDays = currentDateDayOfYear - givenDateDayOfYear + (365*yearDifference);
double differenceWeeks = Math.floor(differenceDays / 7);

不知道如果我在最后数学是完全正确的,因为我只是试图想象它在我的头上,但给它一试?希望这有助于。

Not sure if my math at the end is completely correct because I'm just trying to visualise it in my head, but give it a try? Hope this helped.

这篇关于今年从本周开始在给定的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆