如何计算下周的数量?(How Do I Calculate the Number of the Following Week?)
我需要计算一周后的一周数。 这对于一年中的大多数星期来说是微不足道的,但不是在年末/年初。 例如,如果我需要知道2013年第52周之后的一周。
我想使用defaul_week_format 3,即a)从星期一(而不是星期日)开始的星期b)一年内的编号从1开始(而不是0)和c)第1周是第一周以上今年3天。
我计算2014年第36周后一周的方法如下:
SELECT WEEK(DATE_ADD(STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W'), INTERVAL 1 WEEK))
对于此示例,我计算第36周的星期二日期,添加一周,并计算当天的星期。 这可行,但即使没有增加一周,我也有这种奇怪的行为:
它适用于default_week_format 0:
SET @@local.default_week_format=0; SELECT STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W'); -> Tuesday of week 36 is 2014-09-09 SELECT week('2014-09-09'); -> 2014-09-09 is in week 36
到目前为止,非常好:36周的第二周是第36周。
但是这个简单的实验不适用于default_week_format 3:
SET @@local.default_week_format=3; SELECT STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W'); -> Tuesday of week 36 is 2014-09-09 -> This is wrong, because numbering should start with 1 (instead of 0 now)! -> Tuesday of week 36 should be 2014-09-02 SELECT week('2014-09-09'); -> 2014-09-09 is in week 37
所以MySQL告诉我36周的星期二是在第37周!
我唯一的解释是:
- week()将default_week_format考虑在内
- str_to_date()忽略default_week_format
如何强制MySQL函数str_to_date使用default_week_format?
或者:我怎么能计算下一周的数量? 我也可以在PHP中完成它,但我从MySQL数据库中获取周数。
谢谢你的帮助!
I need to calculate the number of the week following a given week. This is trivial for most weeks within the year, but not at the end / beginning of the year. For example if I need to know the week following week 52 of 2013.
I would like to use the defaul_week_format 3, i.e. a) the weeks start with Monday (instead of Sunday) b) the numbering within a year starts with 1 (instead of 0) and c) week number 1 is the first week with more than 3 days this year.
My approach to e.g. calculate the week after week 36 2014 is the following:
SELECT WEEK(DATE_ADD(STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W'), INTERVAL 1 WEEK))
For this example I calculate the date of Tuesday in week 36, add one week, and calculate the week of this day. This would work, but even without adding one week, I have this strange behaviour:
It works for the default_week_format 0:
SET @@local.default_week_format=0; SELECT STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W'); -> Tuesday of week 36 is 2014-09-09 SELECT week('2014-09-09'); -> 2014-09-09 is in week 36
So far, so good: Tuesday of week 36 is in week 36.
But this simple experiment does NOT work for default_week_format 3:
SET @@local.default_week_format=3; SELECT STR_TO_DATE('W36 2014 Tuesday', 'W%V %X %W'); -> Tuesday of week 36 is 2014-09-09 -> This is wrong, because numbering should start with 1 (instead of 0 now)! -> Tuesday of week 36 should be 2014-09-02 SELECT week('2014-09-09'); -> 2014-09-09 is in week 37
So MySQL tells me that Tuesday of week 36 is in week 37!
My only explanation for this:
- week() takes default_week_format into account
- str_to_date() ignores the default_week_format
How can I force MySQL function str_to_date to use the default_week_format?
Or: How else can I calculate the number of the following week? I could also do it in PHP, but I get the weeks' numbers from a MySQL database.
Thanks for any help!
原文:
最满意答案
下一个计划:
final int[] a1 = { 1 }; final int[] a2 = { 1 }; final int[] a3 = a1; System.out.println("a1.equals(a2): " + a1.equals(a2)); System.out.println("a1.equals(a3): " + a1.equals(a3));
输出:
a1.equals(a2): false a1.equals(a3): true
关于你的消息:
我的理解是int []的
equals
和hashCode
方法没有实现,以尊重对int数组的引用的内容或指针值。这意味着
equals
和hashCode
确实尊重指针值,但不尊重值。我看到Java吞下你的一个
int[]
两个潜在原因。
- 如果你从几个线程填充
HashSet
。HashSet
未同步。 您需要将其创建为下一个:Collections.synchronizedSet(new HashSet<int[]>())
。 当你拥有数百万个物体时,发生碰撞的可能性非常高。- 数组是一个可修改的对象。 如果程序在读取时使用旧的数组实例,则程序中可能存在错误。
The next program:
final int[] a1 = { 1 }; final int[] a2 = { 1 }; final int[] a3 = a1; System.out.println("a1.equals(a2): " + a1.equals(a2)); System.out.println("a1.equals(a3): " + a1.equals(a3));
outputs:
a1.equals(a2): false a1.equals(a3): true
In regards to your message:
My understanding is that the
equals
andhashCode
methods of the int[] are not implemented to respect either the content or the pointer value of the reference to an int array.It means that the
equals
andhashCode
do respect the pointer value, but not the value.I see two potential reasons why Java has swallowed one of your
int[]
.
- If your are filling the
HashSet
from several threads.HashSet
is not synchronized. You need to create it as next:Collections.synchronizedSet(new HashSet<int[]>())
. As you having so millions objects, the chance that collision on add happens is very high.- Array is a modifiable object. You might have a bug in your program if it is using old array instances on reading.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
Java将int []的列表转换为int []的数组 - 并发安全(Java convert list of int[] to array of int[] - concurrent safe)[2022-04-28]
更好的解决方案是使用ConcurrentQueue。 这是您可以在消费时添加值。 注意:完成复制后,您将无法复制任何生成的int[] 。 A better solution would be to use a ConcurrentQueue. This was you can be adding values while you are consuming them. Note: you won't be able to copy any int[] produced after you have done ... -
对你的误解: Object int_wrapped_in_object = 1; int_wrapped_in_object的类型是Integer ,而不是Object! 编译器已经为你准备好了! 换句话说:java编译器无法将int“封装”为“仅” 对象的东西。 因此,如果该Object实际上是一个Integer,那么您需要确定一个简单的int_wrapped_in_object instanceof Integer Integer。 然后你可以简单地投 ! Misconception on your ...
-
GetHashCode确实返回一个int,但如果哈希代码的比较确定它们是相同的,它通过调用Equals方法(您应该重写)进行简单说明。 所以,不,你不必切换。 您可以继续使用旧的可爱HashSet (只要您没有耗尽内存)。 GetHashCode does return an int, but if the comparison for the hash codes determines they are the same, it folllows by calling the Equals method ...
-
HashSet使用int数组(HashSet usage with int arrays)[2021-07-08]
数组不覆盖hashCode和equals在Object类中实现,因此,只有在a1 == a2的情况下,两个数组a1和a2才会被认为是相同的。 如果你使用ArrayList而不是数组,你的问题将被解决,因为对于ArrayList的平等是由列表成员的平等(以及它们出现的顺序)决定的。 Arrays don't override hashCode and equals implemented in Object class, and therefore, two arrays a1 and a2 will be ... -
SomeObjects的HashSet和包含int参数的函数(HashSet of SomeObjects and Contains function with int argument)[2023-04-05]
不幸的是, HashSet没有能力使用不同类型的对象进行搜索并获取存储的内容。 这种能力理论上可以存在,也许你可以在网上找到一个收集库来做到这一点。 最佳解决方法: 创建一个单独的键结构(如MyKey )并使用Dictionary。 这样你就可以在不创建对象的情况下创建一个键。 或者,您可以创建封装结构来封装关键字或整个对象。 你可以比较便宜地实例化该结构并将其传递给HashSet 。 我发现这比第一个想法更复杂。 HashSet unfortunately does no ... -
C# - 如何按最小到最大的顺序重新排列新的HashSet
(C# - How to rearrange new HashSet [2021-08-29]by order of least to greatest) 您不需要订购商品,您可以使用SetEquals检查这些商品是否包含相同的元素。 private static List> RemoveDuplicates(List > hash) { List > returnSet = new List >(); for (int i = 0; i < hash.Count; i++) { var isDupe = false; ... -
基本上,一旦从set删除值,就需要打破内部循环。 for(int j=set.Count-1; j >= 0; j--) { for (int i = subSet.Count-1; i >= 0; i--) { if (subSet[i].IsProperSubsetOf(set[j])) { subSet.RemoveAt(i); set.RemoveAt(j); break; ...
-
下一个计划: final int[] a1 = { 1 }; final int[] a2 = { 1 }; final int[] a3 = a1; System.out.println("a1.equals(a2): " + a1.equals(a2)); System.out.println("a1.equals(a3): " + a1.equals(a3)); 输出: a1.equals(a2): false a1.equals(a3): true 关于你的消息: 我的理解是int []的eq ...