| 1 |
/* |
| 2 |
* @(#) DateConverter.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import java.text.DateFormat; |
| 9 |
import java.text.ParseException; |
| 10 |
import java.text.SimpleDateFormat; |
| 11 |
import java.util.Calendar; |
| 12 |
import java.util.Date; |
| 13 |
|
| 14 |
/** |
| 15 |
* Converter which converts Date to String and converts it back. |
| 16 |
*/ |
| 17 |
public class MonthConverter implements ObjectConverter { |
| 18 |
|
| 19 |
/** |
| 20 |
* Default ConverterContext for MonthConverter. |
| 21 |
*/ |
| 22 |
public static ConverterContext CONTEXT_MONTH = new ConverterContext("Calendar.Month"); |
| 23 |
|
| 24 |
private DateFormat _conciseFormat = new SimpleDateFormat("MMyy"); |
| 25 |
private DateFormat _shortFormat = new SimpleDateFormat("MM/yy"); |
| 26 |
private DateFormat _mediumFormat = new SimpleDateFormat("MM, yyyy"); |
| 27 |
private DateFormat _longFormat = new SimpleDateFormat("MMMMM, yyyy"); |
| 28 |
|
| 29 |
private DateFormat _defaultFormat = _shortFormat; |
| 30 |
|
| 31 |
/** |
| 32 |
* Creates a new CalendarConverter. |
| 33 |
*/ |
| 34 |
public MonthConverter() { |
| 35 |
} |
| 36 |
|
| 37 |
public String toString(Object object, ConverterContext context) { |
| 38 |
if (object == null || !(object instanceof Calendar)) { |
| 39 |
return ""; |
| 40 |
} |
| 41 |
else { |
| 42 |
return _defaultFormat.format(((Calendar) object).getTime()); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public boolean supportToString(Object object, ConverterContext context) { |
| 47 |
return true; |
| 48 |
} |
| 49 |
|
| 50 |
public Object fromString(String string, ConverterContext context) { |
| 51 |
Calendar calendar = Calendar.getInstance(); |
| 52 |
try { |
| 53 |
Date time = _defaultFormat.parse(string); |
| 54 |
calendar.setTime(time); |
| 55 |
} |
| 56 |
catch (ParseException e1) { // if current formatter doesn't work try those default ones. |
| 57 |
try { |
| 58 |
Date time = _shortFormat.parse(string); |
| 59 |
calendar.setTime(time); |
| 60 |
} |
| 61 |
catch (ParseException e2) { |
| 62 |
try { |
| 63 |
Date time = _mediumFormat.parse(string); |
| 64 |
calendar.setTime(time); |
| 65 |
} |
| 66 |
catch (ParseException e3) { |
| 67 |
try { |
| 68 |
Date time = _longFormat.parse(string); |
| 69 |
calendar.setTime(time); |
| 70 |
} |
| 71 |
catch (ParseException e4) { |
| 72 |
try { |
| 73 |
Date time = _conciseFormat.parse(string); |
| 74 |
calendar.setTime(time); |
| 75 |
} |
| 76 |
catch (ParseException e5) { |
| 77 |
return null; // nothing works just return null so that old value will be kept. |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
return calendar; |
| 84 |
} |
| 85 |
|
| 86 |
public boolean supportFromString(String string, ConverterContext context) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Gets DefaultFormat to format an calendar. |
| 92 |
* |
| 93 |
* @return DefaultFormat |
| 94 |
*/ |
| 95 |
public DateFormat getDefaultFormat() { |
| 96 |
return _defaultFormat; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Sets DefaultFormat to format an calendar. |
| 101 |
* |
| 102 |
* @param defaultFormat the default format to format a calendar. |
| 103 |
*/ |
| 104 |
public void setDefaultFormat(DateFormat defaultFormat) { |
| 105 |
_defaultFormat = defaultFormat; |
| 106 |
} |
| 107 |
} |