- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我可能在这里遗漏了一些东西,但我似乎无法在 Joda Time 的文档中或任何地方找到解释。从一年到下一年,在计算周时加减周时,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 是正确的,但不是您的逻辑。您必须仔细区分“日历年”(从一月一日开始)和星期日期的年份(如 ISO-8601 中所定义,也称为“基于周的年份”或简称为“周-年”)。
例如,您在类中使用两个彼此不严格相关的成员来存储中间结果:
week = nextWeek.getWeekOfWeekyear();
year = nextWeek.getYear();
这些行的问题在于周与基于周的年相关,而不是第二行所示的日历年。请记住,基于周的年份可能比一月一日的日历年少一年。例如 [2014-12-31] 与 [2015-W01-3] 的日期相同。另请记住,Joda-Time 提供了另一种称为 getWeekyear()
的方法。
然后您将使用这两个值以这种方式操作日期:
LocalDate today = new LocalDate()
.withDayOfWeek(4)
.withWeekOfWeekyear(week)
.withYear(year);
同样的术语问题。方法 withWeekOfWeekyear(week)
已经可以更改日历年并将月中的某天转移到另一天,同时尝试在当前 week-year 中保留星期几 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
关于java - Joda Time minusweeks() 和 plusweeks() 超过一年打破 2014/2015 分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31014905/
实际上我只需要用JAVA编写一个简单的程序来将MySQL INSERTS行转换为CSV文件(每个mysql表等于一个CSV文件) 在JAVA中使用正则表达式是最好的解决方案吗? 我的主要问题是如何正确
我有一个 txt 文件,其格式为: Key:value Key:value Key:value ... 我想将所有键及其值放入我创建的 hashMap 中。如何让 FileReader(file) 或
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度的了解。包括尝试的解决方案、为什么它们不起作用以及预期结果
我每周都会从我的主机下载数据库的备份。它生成一个 .sql 文件,当前大小约为 800mb。此 .sql 文件包含 44 个表。 有什么方法可以通过某些软件将 .sql 文件与所有表分开,以便单独导出
在 iOS 4.0 及更高版本中,有没有一种方法可以在不将整个图像加载到内存的情况下对 CGImage 进行分割?我试图做的是*以编程方式*分割图像,以便在使用大图像的 CATiledLayer 应用
我的 .split() 函数有问题,我有以下字符串: var imageUrl = "Images\Products\randomImage.jpg"; 我想用字符“\”分割,但是,这种情况发生了:
是否可以使用正则表达式将字符串拆分两次?例如,假设我有字符串: example=email@address.com|fname|lname 如何拆分结果为: email@address.com,fna
我正在寻找一种在线程系统(主从)中使用数组的解决方案,它允许我通过用户输入在多个线程上划分矩阵的计算,并将其通过 1 个主线程引导到多个从属线程,这些从属线程计算矩阵的 1 个字段。 我尝试运用我的知
我建立了一个系统来分割包含手写符号的二值图像并对它们进行分类(专门用于音乐)。我知道有商业应用程序可以执行此操作,但这是我尝试将其作为一个项目从头开始。 为了简单起见,假设我的整个图像中有两个元素:
我正在尝试找到一种可接受的复杂性的有效方法 检测图像中的对象,以便将其与周围环境隔离 将该对象分割成它的子部分并标记它们,这样我就可以随意获取它们 我进入图像处理世界已经 3 周了,我已经阅读了很多算
我有一组3D 空间中的点。下图是一个示例: 我想把这些点变成一个面。我只知道点的 X、Y 和 Z 值。例如,查看下图,它显示了从 3D 空间中的点生成的人脸网格。 我在谷歌上搜索了很多,但我找到的是一
我有一个字符串 String placeStr="place1*place2*place3"我想获取包含 place1、place2、place3 的数组,如下所示: String[] places=
我在 Python 中有一个类似于 google.com 的字符串,我想将其分成两部分:google 和 .com。问题是我有一个 URL,例如 subdomain.google.com,我想将其拆分
朋友需要对一个pdf文件进行分割,在网上查了查发现这个pypdf2可以完成这些操作,所以就研究了下这个库,并做一些记录。首先pypdf2是python3版本的,在之前的2版本有一个对应pypdf库。
伙计们,这是一个难以解决的问题,因为它涉及很多硬件细节,所以我想把它放到 EE.SE,但它的主要重点是编程,所以我决定坚持在这里。 我最近怀旧(以及渴望回到 CPU 内在函数),所以我决定自制一个 8
给定 haskell 中的排序列表,我如何获得分段列表,其中连续数字位于同一列表中。例如,如果我有一个排序列表 [1,2,3,4,7,8,10,12,13,15] 结果将是 [[1,2,3 ,4],[
如果我添加三个分割 View ,如下图所示,第三个分割 View (称为 splitView-3)将自动为该分割 View 中的自定义 View 生成约束,例如 customview1 的 Heigh
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 6 年前。 Improve th
如何为馈送给映射器的文件的每一行提供相同文件的拆分? 基本上我想做的是 for each line in file-split { for each line in file{
带有Snappy压缩功能的ORC文件是否可拆分成条形? 据我所知,Snappy Compressed File是不可拆分的。 但我在博客中读到,快速压缩的文件可以在 strip 上拆分。 真的吗? 最
我是一名优秀的程序员,十分优秀!