If I want to set the month and day in the date|calendar object to 00 when I
only have a year, it seems that DateUtil forces those to be 01 even when
all it gets is a year such as 1999. I can't allow it to default to 19990101
in such cases - there will be legit occurrences of 19990101. I want to
force it to be 19990000. The Calendar object comes close, because it sets
cal.MONTH to 0, but it does that if the month is 01 legitimately, or if it
is missing. That seems unfortunate.

This is my code so far:

  //
https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
final String[] PATTERNS = new String[] {
        "MMMM dd, yyyy",
        "yyyy",
        "// // MMMM dd, yyyy",  // I am not sure this one is a legit
pattern, and may drop this
        "MM/dd/yyyy",
        "yyyy-MM-dd", "yyyy-mm"
}

     // https://stackoverflow.com/a/54952272, reference credit: Bob Brown
    datesToNormalize.split(/(?ms)::-::/, -1).collect { it.trim() }.each {
candidate ->
       try {
           parsed = DateUtils.parseDateStrictly(candidate, PATTERNS)
           def Calendar cal = Calendar.getInstance()
           cal.setTime(parsed)

           log.info("Given: ${candidate}; parsed: ${cal} month:
${cal.get(Calendar.MONTH)}")
       } catch (Exception e) {
           log.error("Could not parse: ${candidate}")
       }
    }

What I find when I look closely at the details of the calendar object for a
year only, such as 1991..

2023-06-19 17:21:26,235 INFO [Timer-Driven Process Thread-8]
o.a.nifi.processors.script.ExecuteScript
ExecuteScript[id=33a5179c-1df4-128b-52be-aaa96b947012] Given: 1991; parsed:
java.util.GregorianCalendar[time=662688000000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="UTC",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,
*YEAR=1991*,*MONTH=0*,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,*DAY_OF_MONTH=1*,DAY_OF_YEAR=1,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=0,DST_OFFSET=0]
month: 0

Why it sets DAY_OF_MONTH to 1 when we only present a YYYY is unfortunate
too.

I wish to get my final normalized output to be 19910000 in such cases.

On Mon, Jun 19, 2023 at 8:03 PM Bob Brown <b...@transentia.com.au> wrote:

> I guess the key thing to bear in mind is:
>
>
> https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/Calendar.html
> """
> The calendar field values can be set by calling the set methods. Any
> field values set in a Calendar will not be interpreted until it needs to
> calculate its time value (milliseconds from the Epoch) or values of the
> calendar fields. Calling the get, getTimeInMillis, getTime, add and roll 
> involves
> such calculation.
> """
>
> Calendar doesn't provide a way to differentiate, in other words.
>
> See default values for fields at
> https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/util/GregorianCalendar.html
>
> Calendar is often used as a ' bucket' for timey-wimey things: you stuff
> things into fields that contextually make sense and ignore those that
> don't. Not very nice OO or helpful as an API.
>
> It MAY be (speaking off the top of my head here) that you don't need/want
> Calendar...the newer java.time package has many "finer-grained" classes for
> things like Instance, Period, Duration, etc. that might be a better fit for
> a specific use-case: https://www.baeldung.com/java-8-date-time-intro
>
> BOB
> ------------------------------
> *From:* James McMahon <jsmcmah...@gmail.com>
> *Sent:* Tuesday, 20 June 2023 8:53 AM
> *To:* users@groovy.apache.org <users@groovy.apache.org>
> *Subject:* Working with Calendar object in Groovy
>
> If I have a Calendar object created for 1999-01-01, a get() of
> calendar.MONTH will return 0. From references I’ve found through Google, we
> have to add 1 to MONTH to get the 1 for January. The calendar object has 0
> for MONTH.
>
> Now let’s take the case where we set our calendar object from “1999”,.
> When we get MONTH it is also 0 - but not because our month was January, but
> because it was not present.
>
> How does the calendar instance differentiate between those two cases? Is
> there another calendar object element that tells me “hey, I could set no
> day or month from a date like 1999”?
>

Reply via email to