java – Joda时间减去周()和加上周(2014)2014年崩溃?

前端之家收集整理的这篇文章主要介绍了java – Joda时间减去周()和加上周(2014)2014年崩溃?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可能会在这里遗漏一些东西,但是我似乎无法在Joda Time的文档中找到解释,或者真的在任何地方找不到解释.当你从一年到另一年的时间里加上和减去周数时,犹他时间似乎算下来了几周.

任何人都可以解释为什么会发生这种情况,以及如何正确地做到这一点?

我从下面的代码中得到以下输出

2015-01-08 - This is the current week
2015-01-01 - Removed one week
2014-12-25 - Removed one week
2014-12-17 - Removed one week //for some reason,program backed 8 days here
2014-12-10 - Removed one week
2014-12-17 - Added one week
2014-12-24 - Added one week
2014-12-31 - Added one week
2014-01-08 - Added one week //for some reason,program forwarded 8 days here,but it did not forward to 2015.

原始码

import org.joda.time.*;

public class WonkyWeeks {
    int year;
    int week;

    public void backUpOneWeek() {
        LocalDate today = new LocalDate()
                .withDayOfWeek(4)
                .withWeekOfWeekyear(week)
                .withYear(year);
        LocalDate lastWeek = today.minusWeeks(1);

        week = lastWeek.getWeekOfWeekyear();
        year = lastWeek.getYear();
        System.out.println(lastWeek+" - Removed one week");
    }

    public void forwardOneWeek() {
        LocalDate today = new LocalDate()
                .withDayOfWeek(4)
                .withWeekOfWeekyear(week)
                .withYear(year);
        LocalDate nextWeek = today.plusWeeks(1);

        week = nextWeek.getWeekOfWeekyear();
        year = nextWeek.getYear();
        System.out.println(nextWeek+" - Added one week");
    }

    public void thisWeek() {
        LocalDate thisWeek = new LocalDate()
                .withDayOfWeek(4)
                .withWeekOfWeekyear(week)
                .withYear(year);
        System.out.println(thisWeek+" - This is the current week");
    }

    public static void main(String[] args) {
        WonkyWeeks wonky = new WonkyWeeks();
        wonky.week = 2;
        wonky.year = 2015;
        wonky.thisWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
    }
}

经过进一步的测试,它变得更加混乱.我尝试添加删除日子,而不是几周,由于某种原因似乎跳过日期.

输出

2015-01-08 - This is the current week
2015-01-07 - removed one day
2015-01-06 - removed one day
2015-01-05 - removed one day
2015-01-04 - removed one day
2015-01-03 - removed one day
2015-01-02 - removed one day
2015-01-01 - Removed one full week
2014-12-31 - removed one day
2014-12-30 - removed one day
2014-12-29 - removed one day
2014-12-28 - removed one day
2014-12-27 - removed one day
2014-12-26 - removed one day
2014-12-25 - Removed one full week
2014-12-23 - removed one day // For some reason,it skipped 2014-12-24?
2014-12-22 - removed one day
2014-12-21 - removed one day
2014-12-20 - removed one day
2014-12-19 - removed one day
2014-12-18 - removed one day
2014-12-17 - Removed one full week
2014-12-16 - removed one day
2014-12-15 - removed one day
2014-12-14 - removed one day
2014-12-13 - removed one day
2014-12-12 - removed one day
2014-12-11 - removed one day
2014-12-10 - Removed one full week
2014-12-11 - added one day
2014-12-12 - added one day
2014-12-13 - added one day
2014-12-14 - added one day
2014-12-15 - added one day
2014-12-16 - added one day
2014-12-17 - Added one week
2014-12-18 - added one day
2014-12-19 - added one day
2014-12-20 - added one day
2014-12-21 - added one day
2014-12-22 - added one day
2014-12-23 - added one day
2014-12-24 - Added one week
2014-12-25 - added one day
2014-12-26 - added one day
2014-12-27 - added one day
2014-12-28 - added one day
2014-12-29 - added one day
2014-12-30 - added one day
2014-12-31 - Added one week
2014-01-02 - added one day //Skipped 2014-01-01 and did not forward to 2015
2014-01-03 - added one day
2014-01-04 - added one day
2014-01-05 - added one day
2014-01-06 - added one day
2014-01-07 - added one day
2014-01-08 - Added one week

进一步测试代码

import org.joda.time.*;

public class WonkyWeeks {
    int year;
    int week;

    public void backUpOneWeek() {
        LocalDate today = new LocalDate()
                .withDayOfWeek(4)
                .withWeekOfWeekyear(week)
                .withYear(year);
        LocalDate adayago = today.minusDays(1);
        System.out.println(adayago+" - removed one day");
        LocalDate twodaysago = adayago.minusDays(1);
        System.out.println(twodaysago+" - removed one day");
        LocalDate threedaysago = twodaysago.minusDays(1);
        System.out.println(threedaysago+" - removed one day");
        LocalDate fourdaysago = threedaysago.minusDays(1);
        System.out.println(fourdaysago+" - removed one day");
        LocalDate fivedaysago = fourdaysago.minusDays(1);
        System.out.println(fivedaysago+" - removed one day");
        LocalDate sixdaysago = fivedaysago.minusDays(1);
        System.out.println(sixdaysago+" - removed one day");
        LocalDate lastWeek = sixdaysago.minusDays(1);

        week = lastWeek.getWeekOfWeekyear();
        year = lastWeek.getYear();
        System.out.println(lastWeek+" - Removed one full week");
    }
    public void forwardOneWeek() {
        LocalDate today = new LocalDate()
                .withDayOfWeek(4)
                .withWeekOfWeekyear(week)
                .withYear(year);
        LocalDate tomorrow = today.plusDays(1);
        System.out.println(tomorrow+" - added one day");
        LocalDate dayAfterTomorrow = tomorrow.plusDays(1);
        System.out.println(dayAfterTomorrow+" - added one day");
        LocalDate threeDaysFromNow = dayAfterTomorrow.plusDays(1);
        System.out.println(threeDaysFromNow+" - added one day");
        LocalDate fourDaysFromNow = threeDaysFromNow.plusDays(1);
        System.out.println(fourDaysFromNow+" - added one day");
        LocalDate fiveDaysFromNow = fourDaysFromNow.plusDays(1);
        System.out.println(fiveDaysFromNow+" - added one day");
        LocalDate sixDaysFromNow = fiveDaysFromNow.plusDays(1);
        System.out.println(sixDaysFromNow+" - added one day");
        LocalDate nextWeek = sixDaysFromNow.plusDays(1);

        week = nextWeek.getWeekOfWeekyear();
        year = nextWeek.getYear();
        System.out.println(nextWeek+" - Added one week");
    }
    public void thisWeek() {
        LocalDate thisWeek = new LocalDate()
                .withDayOfWeek(4)
                .withWeekOfWeekyear(week)
                .withYear(year);
        System.out.println(thisWeek+" - This is the current week");
    }
    public static void main(String[] args) {
        WonkyWeeks wonky = new WonkyWeeks();
        wonky.week = 2;
        wonky.year = 2015;
        wonky.thisWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
    }
}

解决方法

Joda-Time是正确的,但不是你的逻辑.你必须仔细地在“日历年”(从1月1日开始)到周末的年份(如ISO-8601中定义的,也称为“周年”或简称“周” -年”).

例如,您在类中使用两个不相互关联的成员来存储中间结果:

week = nextWeek.getWeekOfWeekyear();
year = nextWeek.getYear();

这些方面的问题是,这周与周年相关,而不是第二行表示的日历年.请记住,周一年可能比1月份的历年减少一年.例如[2014-12-31]与[2015-W01-3]相同的日期.还要记住,Joda-Time提供另一种称为getWeekyear()的方法.

那么你将使用这两个值来操作这个日期:

LocalDate today = new LocalDate()
        .withDayOfWeek(4)
        .withWeekOfWeekyear(week)
        .withYear(year);

同样的术语问题.而周末(WweekOfWeekyear(周))的方法已经可以改变日历年,并将月日转移到另一天,当时要在2015年的当前周内保留星期几,而不是2014年的日历日期班!整个代码产生的结果不是真正可预测的,并且会让每个人都惊喜.另一个大问题是方法调用的顺序是重要的,因为周操纵是指当前的周年(哪一个?!).以下代码看起来更健康:

LocalDate today = new LocalDate()
        .withWeekyear(year)
        .withWeekOfWeekyear(week)
        .withDayOfWeek(4);

解决方案:您应该更好地参考您的代码中的周年,而不是日历年.或者甚至更好:如果你只是想添加删除几周,那么我建议你存储日期(作为对象的类型LocalDate)而不是应用date.plusWeeks(1)或类似的.您可以随时查询星期几,星期,日历年等的日期.比保存年度和日历年要好得多.

测试后更新:

我现在已将年份改为周年,并在设置日期时更改方法通话的顺序(首先是周年,然后是周,最后是星期几).这些更改后,您的代码将根据我自己的测试工作正常(尽管我仍然建议您简化您的类状态和逻辑).这里我的全部更改和更正的代码

import org.joda.time.LocalDate;

public class WonkyWeeks {
    int year;
    int week;

    public void backUpOneWeek() {
        LocalDate today =
            new LocalDate().withWeekyear(year).withWeekOfWeekyear(week).withDayOfWeek(4);
        LocalDate adayago = today.minusDays(1);
        System.out.println(adayago + " - removed one day");
        LocalDate twodaysago = adayago.minusDays(1);
        System.out.println(twodaysago + " - removed one day");
        LocalDate threedaysago = twodaysago.minusDays(1);
        System.out.println(threedaysago + " - removed one day");
        LocalDate fourdaysago = threedaysago.minusDays(1);
        System.out.println(fourdaysago + " - removed one day");
        LocalDate fivedaysago = fourdaysago.minusDays(1);
        System.out.println(fivedaysago + " - removed one day");
        LocalDate sixdaysago = fivedaysago.minusDays(1);
        System.out.println(sixdaysago + " - removed one day");
        LocalDate lastWeek = sixdaysago.minusDays(1);

        week = lastWeek.getWeekOfWeekyear();
        year = lastWeek.getWeekyear();
        System.out.println(lastWeek + " - Removed one full week");
    }

    public void forwardOneWeek() {
        LocalDate today =
            new LocalDate().withWeekyear(year).withWeekOfWeekyear(week).withDayOfWeek(4);
        LocalDate tomorrow = today.plusDays(1);
        System.out.println(tomorrow + " - added one day");
        LocalDate dayAfterTomorrow = tomorrow.plusDays(1);
        System.out.println(dayAfterTomorrow + " - added one day");
        LocalDate threeDaysFromNow = dayAfterTomorrow.plusDays(1);
        System.out.println(threeDaysFromNow + " - added one day");
        LocalDate fourDaysFromNow = threeDaysFromNow.plusDays(1);
        System.out.println(fourDaysFromNow + " - added one day");
        LocalDate fiveDaysFromNow = fourDaysFromNow.plusDays(1);
        System.out.println(fiveDaysFromNow + " - added one day");
        LocalDate sixDaysFromNow = fiveDaysFromNow.plusDays(1);
        System.out.println(sixDaysFromNow + " - added one day");
        LocalDate nextWeek = sixDaysFromNow.plusDays(1);

        week = nextWeek.getWeekOfWeekyear();
        year = nextWeek.getWeekyear();
        System.out.println(nextWeek + " - Added one week");
    }

    public void thisWeek() {
        LocalDate thisWeek =
            new LocalDate().withWeekyear(year).withWeekOfWeekyear(week).withDayOfWeek(4);
        System.out.println(thisWeek + " - This is the current week");
    }

    public static void main(String[] args) {
        WonkyWeeks wonky = new WonkyWeeks();
        wonky.week = 2;
        wonky.year = 2015;
        wonky.thisWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.backUpOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
        wonky.forwardOneWeek();
    }
}

输出更改的代码

2015-01-08 - This is the current week
2015-01-07 - removed one day
2015-01-06 - removed one day
2015-01-05 - removed one day
2015-01-04 - removed one day
2015-01-03 - removed one day
2015-01-02 - removed one day
2015-01-01 - Removed one full week
2014-12-31 - removed one day
2014-12-30 - removed one day
2014-12-29 - removed one day
2014-12-28 - removed one day
2014-12-27 - removed one day
2014-12-26 - removed one day
2014-12-25 - Removed one full week
2014-12-24 - removed one day
2014-12-23 - removed one day
2014-12-22 - removed one day
2014-12-21 - removed one day
2014-12-20 - removed one day
2014-12-19 - removed one day
2014-12-18 - Removed one full week
2014-12-17 - removed one day
2014-12-16 - removed one day
2014-12-15 - removed one day
2014-12-14 - removed one day
2014-12-13 - removed one day
2014-12-12 - removed one day
2014-12-11 - Removed one full week
2014-12-12 - added one day
2014-12-13 - added one day
2014-12-14 - added one day
2014-12-15 - added one day
2014-12-16 - added one day
2014-12-17 - added one day
2014-12-18 - Added one week
2014-12-19 - added one day
2014-12-20 - added one day
2014-12-21 - added one day
2014-12-22 - added one day
2014-12-23 - added one day
2014-12-24 - added one day
2014-12-25 - Added one week
2014-12-26 - added one day
2014-12-27 - added one day
2014-12-28 - added one day
2014-12-29 - added one day
2014-12-30 - added one day
2014-12-31 - added one day
2015-01-01 - Added one week
2015-01-02 - added one day
2015-01-03 - added one day
2015-01-04 - added one day
2015-01-05 - added one day
2015-01-06 - added one day
2015-01-07 - added one day
2015-01-08 - Added one week
原文链接:https://www.f2er.com/java/122502.html

猜你在找的Java相关文章