Primefaces calendar timezone handling to fixed backend timezone - jsf-2

In a web application the user can choose the timezone which is passed to the timezone attribute of the calendar widget:
<p:calendar value="#{curValue}" timeZone="#{settingsBL.getTimeZoneIdSet()}" />
The delivered date in the backing bean is converted to the timezone of the server (JBoss in my case with CEST). The backend want the date and time as always as UTC (and delivers it in UTC).
So when I store a date I have to convert the CEST date to UTC and save it. If a date is delivered from backend it is UTC. I have to convert it to the system default (JBoss with CEST) and the calendar will take care that it is displayed correctly on the client.
Is this correct? I am a little bit confused about that. The server timezone is variable and cannot be set hard to UTC or something.
The date from the client is always converted to CEST for my example. Regardless what I set to javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE in web.xml.
I am using primefaces 5.2.13 and Mojarra 2.2.12 with JBoss 6.4

The timeZone attribute of any java.util.Date based JSF component which allows manipulating the time part must be set to the one as expected by the view (frontend). It will be used when converting the java.util.Date instance from the model (backend) to the String representation which will be embedded in generated HTML output. It will also be used when converting the incoming String request parameter value to a concrete java.util.Date instance which will be used in the model. If you don't allow manipulating the time part, then just stick to the default of timeZone="GMT".
And now comes the key: the java.util.Date does not hold any timezone information. It's internally always GMT. JSF knows that. JDBC knows that. JPA knows that. As long as you tell JSF what timezone the view uses, and you tell JDBC/JPA what timezone the DB uses, then all should be well.
Perhaps your confusion is caused because you did something like System.out.println(date) to verify one and other. Its toString() result will internally use TimeZone#getDefault() and thus not explicitly use GMT/UTC during the String generation. You'll then confusingly see the date in the system default timezone being printed. To print the date with GMT timezone (in order to debug/log/verify it and such), do so:
System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(date.toInstant().atZone(ZoneId.of("GMT"))));

Related

asp.net core html helper tag - set value (datetime) client side - format

in my json i've this date generated by JsonConvert.SerializeObject()
2020-05-11T15:27:50.3666678+02:00
I get this json by jquery ajax call and i need to set this value (client-side) to an html helper tag
to set this field client-side i do:
$("#scandate").val(details.ScanDate);
where scandate contains the date in the format above 2020-05-11T15:27:50.3666678+02:00
but i got this error:
The specified value "2020-05-11T14:41:59.5753508+02:00" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS".
trying to pass a (fixed) date like this 2020-05-11T15:27:50 the component shows the date!
how can i adjust it avoiding to do a brutal subtring in order remove the last part of the date?

Handling date text input in italian format on Rails 3.0.x

I've a Rails 3.0.7 application that handles many date "text field inputs" in italian format (%d/%m/%Y).
On my Linux Box (configured with italian locale) I run the application and insert into the text box the date 12 november 2014 with an italian format:
12/11/2014 (%d/%m/%Y)
here Rails correctly saves the date without any problem, same thing if I try to insert a date like 27 november 2014:
27/11/2014 (%d/%m/%Y)
On the remote production server the date is wrongly saved swapping day and month parts, so my input:
12/11/2014
is wrongly saved into date:
11/12/2014
and the date input:
27/11/2014
raise an exception because 27 is not a valid month...
I suppose that into production server the problem would be that the date format system setting is wrong (mm/dd/yyyy perhaps...) but the server is administered by a plesk panel and I don't see anything related to date format system setting...
So the question is: what can I configure in Rails (v. 3.0.x) so that I can save the text field date input in italian format ? (possibly avoiding to write a gazillion of code...)
Many thanks in advance...
Your first step should be setting up the locale:
# config/initializers/locale.rb
I18n.default_locale = :it
A few options for dealing with input:
1. Replace the text inputs with HTML5 date inputs or Javascript "date pickers"
I really recommend this since you can get validations on the cheap and its a good feature.
2. Use Time/Date/DateTime .strptime
params_with_dates = params.merge({
"date" => Date.strptime( params["date"], '%d/%m/%Y' )
})
Foo.create(params_with_dates);
Additional reading:
Date.strptime
Rails Internationalization (I18n) API
Ruby 1.8 uses date format conversion %m/%d/%Y, the 1.9 version instead uses date format %d/%m/%Y, so the solution seems to be switching to Ruby 1.9.x...
Example, the date 28th december 2015 is parsed:
In Ruby 1.8: Date.parse("28/01/2015")
In Ruby 1.9: Date.parse("01/28/2015")

How save dd-mm-yyyy date format to mysql using grails

In my grails application i use a javascript calendar plugin which accepts date in dd-mm-yyyy format. I converted the date into yyyy-MM-dd HH:mm:ss z before saving to database which is the default date format in grails. But still it gives me an error of Invalid date format. I tried changing the
default.date.format in messages.properties file. But still it not works. How can i save the dd-mm-yyy format to Mysql using grails?
try using the binding annotation on your domain class
import org.grails.databinding.BindingFormat
...
class MyClass{
#BindingFormat('dd-mm-yyyy hh:mm a')
Date dummydate
}
in the #BindingFormat annotation you can put the format you use client side.
You can also do
Date.parse("dd-MM-yyyy HH:mm:ss", params.date)
In case you want to save it to database without time we can do
Date.parse("dd-MM-yyyy", params.date).clearTime()

How to format a date to JSON in Rails?

I need to produce a date in Rails which looks like this:
/Date(1294268400000)/
I have tried various combinations of DateTime, to_i, to_json but never managed to get the /Date()/ thing.
Do I have to simply get my date in ms and then wrap the /Date(and )/ manually, or is there a built in method?
What about (ruby 1.9.x)?:
Time.now.strftime("/Date(%s%L)/")
=> "/Date(1335280866211)/"
You should try
new Date(posixMillisecondsHere)
first. MDN says that calling the Date function outside of the constructor context (i.e., without the new) will always return a string containing a formatted date rather than a Date object.
Strictly speaking, when you do that, you are writing JavaScript and not JSON. JSON cannot contain Date objects.
RFC 4627 says
2.1. Values
A JSON value MUST be an object, array, number, or string, or one of
the following three literal names:
false null true
If you want to put a Date into what is strictly considered JSON and then get it back out, you must choose some way of using the JSON primitives (to wit, objects, arrays, numbers, strings, etc.) to encode a Date.
If you want to get a Date back out of JSON, whatever parses your JSON must understand the convention that you used to encode the Date.
Hope these are credible and/or official enough to help.
What about something like this:
in your config/en.yml file:
en:
time:
formats:
json: "/Date(%s%L)/"
and than in the view:
<%= l(Time.now, :format => :json) %>
Please note that you would need access to the helpers in the method that renders json. So it won't work if you are using ActiveRecord#to_json method for generating jsons.
Check out this question:
c# serialized JSON date to ruby
... simple answer seems to be to create a parse_date method.
It's the UNIX Epoch (seconds since 1970-01-01) right? What about using DateTime#strftime method?
# Taken from the Ruby documentation
seconds_since_1970 = your_date.strftime("%s")
UPDATE: OK, it's milliseconds, according to the documentation you can use your_date.strftime("%Q") to get the ms (but I've not tried yet).

How does MVC pass datevalues to jQuery?

I am trying to implement the DateRange extension into a MVC site.
The site is trying to use this date format: yyyy/mm/dd (ie 2011/02/14).
When I try a 'Create New' on a page the jquery-ui.js throws an unhandled error on the parseDate function. The parseDate functions shows 'm/d/yy' as the format and '2011/02/09' as the date value.
What is confusing me is how after changing all the m/d/yy formats to yy/mm/dd in the code that there is still the other format showing.
After constant tweaking the sample the only thing that appears to influence the date format is the 'Date.cshtml' file. Changing this files format string will influence how the initial(default) date-time values show. Changing the other C#/JS files seems to have no influence on how the datepicker displays the dateformat.
As a result of this I will be dropping this extension from my project...

Resources