首页 \ 问答 \ 如何计算下周的数量?(How Do I Calculate the Number of the Following Week?)

如何计算下周的数量?(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!


原文:
更新时间:2022-02-27 13:02

最满意答案

下一个计划:

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 []的equalshashCode方法没有实现,以尊重对int数组的引用的内容或指针值。

这意味着equalshashCode确实尊重指针值,但不尊重值。

我看到Java吞下你的一个int[]两个潜在原因。

  1. 如果你从几个线程填充HashSetHashSet未同步。 您需要将其创建为下一个: Collections.synchronizedSet(new HashSet<int[]>()) 。 当你拥有数百万个物体时,发生碰撞的可能性非常高。
  2. 数组是一个可修改的对象。 如果程序在读取时使用旧的数组实例,则程序中可能存在错误。

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 and hashCode 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 and hashCode do respect the pointer value, but not the value.

I see two potential reasons why Java has swallowed one of your int[].

  1. 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.
  2. Array is a modifiable object. You might have a bug in your program if it is using old array instances on reading.

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。