Friday, January 19, 2018

Usage of YYYY and yyyy when retrieving the year in a last week of a Calendar Year with SYSTEM_DATE property in WSO2 ESB 5.0.0

Usage of YYYY and yyyy when retrieving the year in a last week of a Calendar Year with SYSTEM_DATE property in WSO2 ESB 5.0.0

This article explains the difference between year retrieved using the below two properties in WSO2 ESB.

1) <property name="YEAR" expression="get-property('SYSTEM_DATE', 'YYYY')"/>
   
2) <property name="year" expression="get-property('SYSTEM_DATE', 'yyyy')"/>

The sample proxy service used is as below:
 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
     name="WeekYearTestProxy"  
     transports="https http"  
     startOnLoad="true">  
   <description/>  
   <target>  
    <inSequence>  
      <log level="custom">  
       <property name="YEAR" expression="get-property('SYSTEM_DATE', 'YYYY')"/>  
       <property name="year" expression="get-property('SYSTEM_DATE', 'yyyy')"/>  
      </log>  
    </inSequence>  
   </target>  
 </proxy>  


The output log of the above proxy will be as below on 31/12/2017.
When you use YYYY as the format, then you may get into an issue during a last week of year as for example in my case it was 31/12/2017. When this property retrieve the year, it will print it as 2018. Why this happens is because YYYY will return the Weekly based calendar year and how that is calculated is as below and refer [1] for more information.

[1] https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html#week_year

Consider, I'm running the sample proxy on 31/12/2017 and it is a Sunday. Based on [1], we need to check the what value set to FirstDayOfWeek and the other parameter we need to check is the MinimalDaysInFirstWeek. We can check both of this using the below sample code:
 import java.util.Calendar;  
 import java.util.GregorianCalendar;  
 import java.util.Locale;  
 public class Test{  
      public static void main(String[] args) {  
           Calendar c = new GregorianCalendar();  
           System.out.println(Locale.getDefault() + ": " + c.getFirstDayOfWeek() + " - " + c.getMinimalDaysInFirstWeek());  
      }  
 }  

Output of the above Java code segment will be as below:

Above output tells us that the first day of week is Sunday and Minimal days in first week is 1.

Below are extracted from [2] [3] to get more understanding on the methods used above.
[2] https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getMinimalDaysInFirstWeek--

getMinimalDaysInFirstWeek

Gets what the minimal days required in the first week of the year are; e.g., if the first week is defined as one that contains the first day of the first month of a year, this method returns 1. If the minimal days required must be a full week, this method returns 7.

[3] https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getFirstDayOfWeek--

getFirstDayOfWeek

Gets what the first day of the week is; e.g., SUNDAY in the U.S., MONDAY in France.

Based on the above definitions and our output.
FirstDayofWeek is Sunday and as getMinimalDaysInFirstWeek returns as 1, the first week is the one that contains the first day of the first month of the year. Due to that the first week starts from Sunday 31/12/2017, due to that when we use YYYY, the year that will be returned is 2018.

That's how the YYYY, get's 2018...

For more info refer below section in [1].



No comments:

Post a Comment