g:formatDate grails taglib does not work with java.time.chrono.HijrahDate instance

463 views Asked by At
 HijrahChronology.INSTANCE.date(LocalDate.of(year, month, day));

This code used to convert a date to Hijrah Date using UmalQuraCalendar API of JAVA8.

<g:formatDate date="${hijrahDate}" /> raises an exception :

Class
java.lang.IllegalArgumentException
Message
Unknown class: org.codehaus.groovy.grails.web.util.StreamCharBuffer

How to format java.time.chrono.HijrahDate object in GSP?

1

There are 1 answers

0
Abdennour TOUMI On

RESOLVED!

Use Cases :

  <abdennour:formatDate showTime="true" date="${new Date()}" type="hijri"/>

another

   <abdennour:formatDate showTime="true" date="${new Date()}" format="dd/MMM/YYYY" type="hijri"/>

NOTES:

  • type attribute does not exist , it means that you use g:formatDate implicitly
  • if type="hijri" ,format contains only dd,MMM or/and YYYY , if you want to show time ,add showTime attribute.

API

Service :

 class UtilsService {
    java.time.chrono.HijrahDate  toHijri(Date date){

            //return HijrahChronology.INSTANCE.date(Instant.ofEpochMilli(date.getTime()));
            Calendar cl=Calendar.getInstance()
            cl.setTime(date);
            return   toHijri(cl.get(Calendar.YEAR), cl.get(Calendar.MONTH)+1, cl.get(Calendar.DATE));
        }

        java.time.chrono.HijrahDate toHijri(int year,int month,int day){


            return   HijrahChronology.INSTANCE.date(LocalDate.of(year, month, day));
        }

}

Taglib :

class UtilsTagLib {
    static namespace='abdennour'
    def utilsService
def formatDate={at,b->
        if(at?.type && at?.type?.toLowerCase()?.startsWith("hij")){
            at.dateh=utilsService.toHijri(at.date);
            if(!at?.format){at?.format='dd-MM-YYYY'}
            //at?.fullformat=at?.format
            //at?.format='dd-MMM-YYYY'
            String format=at.dateh?.format(DateTimeFormatter.ofPattern(at?.format))
            if(at?.showTime){
                Calendar cl=Calendar.getInstance();cl.setTime(at?.date);
                format=format+' '+cl.get(Calendar.HOUR)+':'+cl.get(Calendar.MINUTE)
            }
            out << format

        }else{

           out << g.formatDate(at)
        }

    }
}