001/** 002 * Copyright 2005-2018 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krad.web.bind; 017 018import org.kuali.rice.core.api.util.RiceKeyConstants; 019import org.kuali.rice.core.web.format.FormatException; 020 021import java.io.Serializable; 022import java.sql.Date; 023import java.text.ParseException; 024import java.util.Calendar; 025 026/** 027 * PropertyEditor converts between date display strings and <code>java.util.Calendar</code> objects using the 028 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>. 029 * 030 * @author Kuali Rice Team (rice.collab@kuali.org) 031 */ 032public class UifCalendarEditor extends UifDateEditor implements Serializable { 033 private static final long serialVersionUID = 8123569337264797008L; 034 035 /** 036 * This overridden method uses the 037 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code> to convert 038 * the calendar object to the display string. 039 */ 040 @Override 041 public String getAsText() { 042 if (this.getValue() == null) { 043 return null; 044 } 045 046 if ("".equals(this.getValue())) { 047 return null; 048 } 049 050 return getDateTimeService().toDateString(new Date(((java.util.Calendar) this.getValue()).getTimeInMillis())); 051 } 052 053 /** 054 * Convert display text to <code>java.util.Calendar</code> object using the 055 * <code>org.kuali.rice.core.api.datetime.DateTimeService</code>. 056 * 057 * @param text the display text 058 * @return the <code>java.util.Calendar</code> object 059 * @throws IllegalArgumentException the illegal argument exception 060 */ 061 protected Object convertToObject(String text) throws IllegalArgumentException { 062 try { 063 // Allow user to clear dates 064 if (text == null || text.equals("")) { 065 return null; 066 } 067 068 Date result = getDateTimeService().convertToSqlDate(text); 069 Calendar calendar = getDateTimeService().getCalendar(result); 070 calendar.setTime(result); 071 072 if (calendar.get(Calendar.YEAR) < 1000 && verbatimYear(text).length() < 4) { 073 throw new FormatException("illegal year format", RiceKeyConstants.ERROR_DATE, text); 074 } 075 076 return calendar; 077 } catch (ParseException e) { 078 throw new FormatException("parsing", RiceKeyConstants.ERROR_DATE, text, e); 079 } 080 } 081 082}