System.date.parse not working properly

I was testing this Scripting Function in the Script Console and I am getting some bad results.
Does anyone have any idea about what I'm doing wrong here:

d1 = system.date.format(system.date.now(), "YYYY-MM-dd HH:mm:ss.SSS")
d1

d2 = system.date.parse(d1, "YYYY-MM-dd HH:mm:ss.SSS")
d2

Thank you!

What are you actually trying to accomplish?

You're just getting confused over the string representation.

d1 is a String you've formatted a Date object into. You chose the format of that string.

d2 is Date object you've parsed from d1, being displayed using the built-in toString() representation. The Date object displays itself differently than the format you parsed the original date into.

The problem that I noticed is that the date value is different, the day and month, specifically.

Oh I see, hang on...

Use lower-case 'y':

d1 = system.date.format(system.date.now(), "yyyy-MM-dd HH:mm:ss.SSS")
d1

d2 = system.date.parse(d1, "yyyy-MM-dd HH:mm:ss.SSS")
d2

upper-case 'Y' is for "week years", which is still not a thing I actually understand.

https://docs.inductiveautomation.com/display/DOC81/Data+Type+Formatting+Reference
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html

3 Likes

Awesome, thank you!