Over a million developers have joined DZone.

Working with JSF's f:convertDateTime and java.util.Date

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

During a recent class I taught on ICEfaces, one of my students asked me why the calendar was often one day off from what got posted back to the model managed-bean setter.

For example:
// Facelets XHTML Markup:
<ice:selectInputDate value="#{modelManagedBean.dateOfBirth}">
<f:convertDateTime pattern="MM/dd/yyyy" />
</ice:selectInputDate>

// Java Code
import java.util.Date;
public class ModelManagedBean {

private Date dateOfBirth;

public Date getDateOfBirth() {
return dateOfBirth;
}

public Date setDateOfBirth( Date dateOfBirth) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm z" );
// The value printed here during postback was often wrong by 1 day
System.out.println("dateOfBirth=" + dateFormat.format(dateOfBirth));
this.dateOfBirth = dateOfBirth;
}
}
Basically, the JSF DateTimeConverter Javadoc states that if the timeZone attribute is not specified, then the default is GMT. But when you create an instance of java.text.SimpleDateFormat, the default TimeZone is equal to TimeZone.getDefault() which (for me) was EST. So the solution I explained to my students was to make sure we were comparing apples-to-apples the whole way through, by using GMT for the SimpleDateFormat printing, like this:
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT" )); 
And of course, I think it's the recommended practice to run your application server JVM in GMT. That would eliminate the problem entirely. But when you're using Eclipse and Tomcat for development, that's typically not the case.

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Ted Goddard, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}