Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.json; |
| 2 | |
| 3 | import java.io.*; |
| 4 | import java.lang.reflect.*; |
| 5 | import java.text.*; |
| 6 | import java.util.*; |
| 7 | |
| 8 | public class DateHandler extends Handler { |
| 9 | final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); |
| 10 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 11 | @Override |
| 12 | void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | String s; |
| 14 | synchronized (sdf) { |
| 15 | s = sdf.format((Date) object); |
| 16 | } |
| 17 | StringHandler.string(app, s); |
| 18 | } |
| 19 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 20 | @Override |
| 21 | Object decode(String s) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 22 | synchronized (sdf) { |
| 23 | return sdf.parse(s); |
| 24 | } |
| 25 | } |
| 26 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 27 | @Override |
| 28 | Object decode(Number s) throws Exception { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 29 | return new Date(s.longValue()); |
| 30 | } |
| 31 | |
| 32 | } |