The Year changer - YYYY

                                              The year changer YYYY


Did you know that the year format yyyy and YYYY are not same? Although round the year you would find both format working for you in the same way but, when the year is to end and there is a transition of the year to happen, at that time you may get the unexpected result if you do not choose the right format.


So, what's the difference ? Let's discuss this.


yyyy - This format represents the calender year. Now what does it mean? It means this format evaluates the year part as the year in which day and month of the date lies. You would say it's a waste of time, I already know this, what's new. I agree, till this point nothing new but, let's see the other format


YYYY -  This format represents the 'year of the week'  or to simplify, year of the last day of the week. Wait, Does it mean any thing?

Yes, it does. To understand the meaning of it, let's take an example : 

var Date = new GlideDate();

Date.setValue("2023-12-31");

gs.print("Date in yyyy format: " +

Date.getByFormat("yyyy-MM-dd") + " - Date in YYYY format: " + Date.getByFormat("YYYY-MM-dd"));





Let's see the output:





This is the unexpected result I was referring to. Now since we have seen the example, let's try to understand what's the difference.

When we say yyyy - It simply means the year in which your date falls, this never goes wrong. This is what you need mostly.

When you say YYYY - It means 'year of the week' i.e. year of the last day of the week in which your given date falls.

To understand it better, you must know that in Java, day of the week starts with 'Sunday', which is considered as the start of the week and 'Saturday' is the last day of the week.

So when you say YYYY in your date format, it evaluates not to the date's year rather the year of the last day of the week in which your date falls.

Based on the above explanation, let's look into the date example we took:




2023-12-31 - Here the day of this date is 'Sunday', but this is the start day of the week, YYYY will look for the end day of this week. Now end day of the week is 'Saturday' which is 6th of January which falls in year 2024, hence YYYY results in 2024 and not 2023.

What about 2022-12-31, fortunately day of this date is 'Saturday' so, yyyy and YYYY both will result into same year.




So, Next time when you are formatting your date, make sure you choose the format wisely.

Comments

  1. Wow! Didn't know this. Took me a while to get it, but definitely something very new.

    ReplyDelete
    Replies
    1. Yes, it takes couple of times of reading to fully consume the information but it's worth.

      Delete
  2. Woww! Really Interesting stuff

    ReplyDelete
  3. Ravi Kumar Singh5 January 2023 at 02:24

    Interesting !!. Thank you for sharing.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to create custom probe and sensor.

Discovery Probes and Sensors