01: /*
02: * @(#) DateConverter.java
03: *
04: * Copyright 2002 - 2003 JIDE Software. All rights reserved.
05: */
06: package com.jidesoft.converter;
07:
08: import java.util.Calendar;
09: import java.util.Date;
10:
11: /**
12: * Converter which converts Calendar to String and converts it back.
13: */
14: public class CalendarConverter extends DateConverter {
15:
16: /**
17: * Creates a new CalendarConverter.
18: */
19: public CalendarConverter() {
20: }
21:
22: @Override
23: public String toString(Object object, ConverterContext context) {
24: if (object == null || !(object instanceof Calendar)) {
25: return "";
26: } else {
27: return super .toString(((Calendar) object).getTime(),
28: context);
29: }
30: }
31:
32: /**
33: * Converts from a String to a Calendar.
34: *
35: * @param string the string to be converted.
36: * @param context the context. It could be DATETIME_CONTEXT, DATE_CONTEXT or TIME_CONTEXT.
37: * @return the Calendar object. If the string is null or empty, null will be returned. If the string cannot be parsed as a date, the string itself will be returned.
38: */
39: @Override
40: public Object fromString(String string, ConverterContext context) {
41: if (string == null || string.trim().length() == 0) {
42: return null;
43: }
44:
45: Object date = super .fromString(string, context);
46: Calendar calendar = Calendar.getInstance();
47: if (date instanceof Date) {
48: calendar.setTime((Date) date);
49: return calendar;
50: } else {
51: return string;
52: }
53: }
54: }
|