Java tutorial
/* * Copyright 2016 Erwin Mller <erwin.mueller@deventm.org> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.zeroone.guestebook; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import org.springframework.context.MessageSource; import org.springframework.format.Formatter; /** * Formats date field for guesbook entries. Uses the * {@code "guestbook.date.format"} entry from the {@code messages.properties}. * * @author Erwin Mller, erwin.mueller@deventm.de * @since 1.0 */ public class DateFormatter implements Formatter<Date> { private final MessageSource messageSource; public DateFormatter(MessageSource messageSource) { super(); this.messageSource = messageSource; } @Override public Date parse(String text, Locale locale) throws ParseException { SimpleDateFormat dateFormat = createDateFormat(locale); return dateFormat.parse(text); } @Override public String print(Date object, Locale locale) { SimpleDateFormat dateFormat = createDateFormat(locale); return dateFormat.format(object); } private SimpleDateFormat createDateFormat(Locale locale) { String format; format = messageSource.getMessage("guestbook.date.format", null, locale); SimpleDateFormat dateFormat = new SimpleDateFormat(format); dateFormat.setLenient(false); return dateFormat; } }