I want to transform a Instant time to Date but I'm getting this error:
freemarker.template.TemplateException: Expected hash. newDate evaluated instead to freemarker.template.SimpleDate
I'm doing this on Java:
Date newDate = new Date();
Instant instant = Instant.now();
webContext.put("newDate",new Date());
webContext.put("instant",instant);
And I'm doing this on Freemarker:
[#assign dateFormated = newDate.getAsDate().from(instant.ofEpochSecond(data.time.seconds))/]
Thank you
FreeMarker templates don't in general expose Java API-s, or allow you to access Java classes by name. I mean, in some cases it does, but not in general like newDate has no subvariables (like getAsDate) in FreeMarker. There are utilities with which you can expose the static methods of classes, like:
TemplateHashModel staticModels
= ((BeansWrapper) configuration.getObjectWrapper())
.getStaticModels();
webContext.put("Date", staticModels.get("java.util.Date"));
webContext.put("Instant", staticModels.get("java.time.Instant"));
where configuration is your freemarker.template.Configuration singleton. Actually, you can add Date and Instant to that singleton with Configuration.setSharedVariable, once where you configure FreeMarker.
And then, you can write Date.from(Instant.now()) into a template, because now there's a Date and and Instant variable, and you have specifically told FreeMarker to expose thier static methods.
How can I print date and time is specified timezone with Thymeleaf? Something like:
<span th:text="${#dates.format(myDate, 'yyyy-MM-dd HH:mm', 'PST')}">2010-01-01 16:30</span>
Another approach to the same problem may be to use your own static methods:
${T(xx.xxx.utils.DateUtils).format(myDate, 'yyyy-MM-dd HH:mm', 'CET')}
public static String format(Date date, String pattern, String timeZone) {
TimeZone tz = TimeZone.getTimeZone(timeZone);
return DateFormatUtils.format(date, pattern, tz);
}
or even directly from lang3 (does not work on GAE because of some class access restrictions in sun.util.calendar package):
<div th:with="timeZone=${T(java.util.TimeZone).getTimeZone('CET')}">
<span th:text="${T(org.apache.commons.lang3.time.DateFormatUtils).format(myDate, 'yyyy-MM-dd HH:mm', timeZone)}"></span>
</div>
As I was puzzled by this question, I searched extensively for possible solutions.
These are my findings:
I did not found any clean function for changing timezone and displaying it like it is in jsp:
<fmt:timeZone value="US">
<fmt:formatDate value="${today}" type="both" />
</fmt:timeZone>
Possible solution, that works would be to create calendar instance using createForTimeZone and format it, since it returns a raw calendar value, so from this:
#calendars.createForTimeZone(year, month, day, hour, minute, second, milisecond, Object timezone)
you would get something like this:
java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="PST",offset=-28800000,dstSavings=3600000,useDaylight=true,transitions=185,lastRule=java.util.SimpleTimeZone[id=PST,offset=-28800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=1,WEEK_OF_YEAR=14,WEEK_OF_MONTH=1,DAY_OF_MONTH=24,DAY_OF_YEAR=91,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=6,HOUR_OF_DAY=7,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-28800000,DST_OFFSET=3600000]
As you can see (you have to look carefully) it did converted time to the timezone provided.
Now, I still haven't gotten to the point where I can get it all to work fine, but if you add calendars.format in front of this, you would get it to properly show time in the given timezone. ${#calendars.format(#calendars.createForTimeZone(year, month, day, hour, minute, second, milisecond, Object timezone), 'dd-MMM-yyyy HH:mm')}
Adding "zzz" to the end of the string, always return my locale timezone. I guess there are way to work this out so it looks better, but main point for me was to find out if it was possible at all.
Examples that work:
${#dates.format(#calendars.createForTimeZone(#calendars.year(ticket.ticketDate), #calendars.month(ticket.ticketDate), #calendars.day(ticket.ticketDate), #calendars.hour(ticket.ticketDate), #calendars.minute(ticket.ticketDate),'PST'), 'yyyy-MMM-dd HH:mm')}
${#calendars.format(#calendars.createForTimeZone(#calendars.year(ticket.ticketDate), #calendars.month(ticket.ticketDate), #calendars.day(ticket.ticketDate), #calendars.hour(ticket.ticketDate), #calendars.minute(ticket.ticketDate),'CET'), 'yyyy-MMM-dd HH:mm')}
and either one would return identical results.
Here are the results when comparing same format, using PST and CET:
2014-Feb-24 16:00
2014-Feb-24 07:00
or:
2014-Mar-01 03:00
2014-Feb-28 18:00
Regards,
I found this answer when I wanted to format LocalDateTime to some time zone in the templates. It turned out that the purpose of LocalDateTime is to do not work with time zones at all.
However, there is also a class called ZonedDateTime which purpose is obvious. You can also use LocalDateTime#atZone which creates a new instance of local converted to the new zone.
Note that usual DateTimeFormatter ignores any time zone settings in the case of local date time but not in the case of zoned date time. So you can use usual formatters as well in the templates.
The server renders the page based on the server time, in order to get the timezone of the user from the request, the user when submitting the request should attach the timezone information in the request headers or parameters, so that the server knows the appropriate time zone to render. To do that, use javascript to get the browser's time zone.
Using Thymeleaf's #temporals doesn't provide the ability to use the constructor new Temporals(locale, zoneId).
Create your own Temporals temporals = new Temporals(LocaleContextHolder.getLocale(), zone) (somewhere, session bean, or so - zone should probably come from your user's preferences), and put it available in the controller (eg. as "temporalsZone").
Then use it in the UI: th:text="${temporalsZone.format(value, pattern, #locale)}"> and enjoy the full support of #temporals.
If you add org.thymeleaf.extras:thymeleaf-extras-java8time as a dependency, you get the #temporals object to help format types like:
Instant
LocalDateTime
ZonedDateTime
etc
If you want to format java.util.Date you can use #dates instead.
The methods you're looking for are #dates.format or #temporals.format. You can specify a Locale as the third argument. The general syntax is #temporals.format(<temporal object>, <pattern>, <optional locale>)
Examples:
th:text="${#temporals.format(myDate, 'dd-MM-yyyy', new java.util.Locale('en'))}"
th:text="${#temporals.format(myDate, 'dd-MM-yyyy', #java.util.Locale#ENGLISH)}"
Note that this is true even if you're working with Kotlin Spring Boot. The syntax in the Thymeleaf template isn't Java, it's an OGNL Expression.
https://commons.apache.org/proper/commons-ognl/language-guide.html
I'll quote the useful syntax used here:
#variable
Context variable reference
#class#method(args)
Static method reference
#class#field
Static field reference
new class(args)
Constructor call
One other option is to specify the Locale in the Thymeleaf context, if you just want to override the default system Locale. I've included a Kotlin snippet of how that might work:
val context = Context() // org.thymeleaf.Context
context.locale = Locale.ENGLISH
context.setVariable("x", 0)
templateEngine.process("classpath:template.html", context)
Then you can simply use th:text="${#temporals.format(datum.timestamp, 'E MMM dd yyyy')} without the explicit Locale argument.
how to convert date in string variable to date variable..??
Date in database is in yyyy-MM-dd format..
im entering in dd-MM-yyyy format..and trying to convert it in db format so that i can use 'between' query.. code is below
<%String date1=(String)request.getAttribute("from");%>
<%String date2=(String)request.getAttribute("to");%>
<%String empcode=(String)request.getAttribute("occ");%>
<%SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");%>
<%Date date = new Date(); %>
<%Date fromdate=formatter.parse(date1);%>
<%Date todate=formatter.parse(date2);%>
You're using getAttribute, but I suspect you wanted getParameter (if you're trying to get information submitted as part of a GET or POST request).
Here's what getAttribute works with:
Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. For example, for requests made using HTTPS, the attribute javax.servlet.request.X509Certificate can be used to retrieve information on the certificate of the client. Attributes can also be set programatically using setAttribute(java.lang.String, java.lang.Object). This allows information to be embedded into a request before a RequestDispatcher call.
Here's what getParameter works with:
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
Looking at the date formatting, you've mentioned two different formats in your question. If the date1 string looks like 2013-12-19, then that code will work. If it's 19-12-2013 then that code won't work because you've told SimpleDateFormat to use "yyyy-MM-dd" but you want "dd-MM-yyyy".
In both cases, the way to debug this is to look at what date1 and date2 contain. That would point the way toward how to fix it.
SimpleDateFormat formatter=new SimpleDateFormat("dd-MM-yyyy");
Date fromdate=formatter.parse(date1);
SimpleDateFormat formatter2=new SimpleDateFormat("yyyy-MM-dd");
String newDate1 = formatter2.format(fromdate);
newDate1 is now in format yyyy-MM-dd.
You can again create the date object with the new string
formatter2.parse(newDate1);
but not necessary I suppose
I am having a hard time trying to make a web service client work. It is a XML RPC specification. I am using Apache WS XML-RPC library, which I find full of holes that causes problem due to Serialization. I have to send a Date parameter for the library to add the tags , however the web service expects it with the TZ, that means adding -0500 at the end of the Date object. If I dont send it as Date Object, it wont add the tags and it will fail. And when trying to do this:
DateFormat df = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ");
String fecha = df.format(new Date());
Date date = new SimpleDateFormat("yyyyMMdd'T'HH:mm:ssZ").parse(fecha);
And using parameter date, and it always sends it as
<dateTime.iso8601>20130517T20:30:33</dateTime.iso8601>
Can't find a way for it to send it as Date object in the format above but with the -0500 at the end. Any help would be appreciated.
I instantiated a java.util.Date object called myDate in my controller and passed it to my JSP where I have a Joda Time JSP tag configured with this at the top of the page:
<%#taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>
and of course the necessary Maven dependencies added to the project via the POM file.
However, when I try to access myDate from the JSP like this:
<joda:format value="${myDate}" style="SM" />
I get this error:
javax.servlet.jsp.JspException:
value attribute of format tag must be a
ReadableInstant or ReadablePartial, was: java.util.Date
Referring to the documentation for the Joda Time JSP tags, I can't tell how I should 'convert' my myDate to a ReadableInstant or ReadablePartial in the context of this JSP?
The error message is self-explaining. The JodaTime tags doesn't accept a Java SE standard Date instance, but a JodaTime DateTime instance or whatever implements JodaTime's ReadableInstant or ReadablePartial.
You need to convert it before providing it to the view.
DateTime dateTime = new DateTime(date.getTime());
request.setAttribute("myDate", dateTime);