0

I am executing the below Java code:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
System.out.println(cal.get(Calendar.WEEK_OF_YEAR));
System.out.println(cal.getTime());

output:

28

Fri Jul 08 08:56:04 BST 2016

Below is the command I executed in MYSQL:

select week(CURDATE()), CURDATE();

Output:

27 2016-07-08

How to sync the both the week of the year value? I tried week(CURDATE(),0) still same result, without TimeZone also tried but getting same result.

Community
  • 1
  • 1
Rajesh Narravula
  • 1,433
  • 3
  • 26
  • 54
  • FYI: The definition of `Calendar.WEEK_OF_YEAR` varies by locale. Avoid that class entirely; now supplanted by *java.time* classes. – Basil Bourque Feb 19 '18 at 02:07

1 Answers1

2

The definition of the "first week in a year" is an abitrary one. You need to find out what the MySQL definition being used is and make Java match it, or find out what the default Java definition is and make MySQL match it.

On the Java side, it will be influenced by Calendar#setFirstDayOfWeek and Calendar#setMinimalDaysInFirstWeek (possibly others, check the Calendar docs).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I need generic way of solution – Rajesh Narravula Jul 08 '16 at 09:35
  • 3
    @Rajesh: That **is** a generic solution: Ensure that the rules match between the two systems. (And "I need" is a very poor way to ask for help.) – T.J. Crowder Jul 08 '16 at 09:36
  • Thank you. @T.J.Crowder . I am using YEARWEEK() in MySQL with default mode. So what would be the Java equivalent ? I am not able to figure it out. Kindly help. – Vinay Jun 09 '20 at 04:43