Debian Codegraph @

src/com/jidesoft/converter/CalendarConverter.java

raw
1 /*
2 * @(#) DateConverter.java
3 *
4 * Copyright 2002 - 2003 JIDE Software. All rights reserved.
5 */
6 package com.jidesoft.converter;
7
8 import java.util.Calendar;
9 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) {
25 return "";
26 }
27 else if (!(object instanceof Calendar)) {
28 return object.toString();
29 }
30 else {
31 return super.toString(object, context);
32 }
33 }
34
35 /**
36 * Converts from a String to a Calendar.
37 *
38 * @param string the string to be converted.
39 * @param context the context. It could be DATETIME_CONTEXT, DATE_CONTEXT or TIME_CONTEXT.
40 * @return the Calendar object. If the string is null or empty, null will be returned. If the string cannot be
41 * parsed as a date, the string itself will be returned.
42 */
43 @Override
44 public Object fromString(String string, ConverterContext context) {
45 if (string == null || string.trim().length() == 0) {
46 return null;
47 }
48
49 Object date = super.fromString(string, context);
50 Calendar calendar = Calendar.getInstance();
51 if (date instanceof Date) {
52 calendar.setTime((Date) date);
53 return calendar;
54 }
55 else {
56 return string;
57 }
58 }
59 }