From 8ed3ee021d31965efa942619cb37856dd6370c32 Mon Sep 17 00:00:00 2001 From: mguessan Date: Tue, 20 Jul 2010 16:02:59 +0000 Subject: [PATCH] Fix bday generation git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1234 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 24 +++++++++++++++---- .../exchange/TestExchangeSessionContact.java | 4 ++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 20142772..138622e1 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -119,6 +119,7 @@ public abstract class ExchangeSession { private static final String YYYY_MM_DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss"; private static final String YYYYMMDD_T_HHMMSS_Z = "yyyyMMdd'T'HHmmss'Z'"; private static final String YYYY_MM_DD_T_HHMMSS_Z = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + private static final String YYYY_MM_DD = "yyyy-MM-dd"; private static final String YYYY_MM_DD_T_HHMMSS_SSS_Z = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; /** @@ -203,6 +204,12 @@ public abstract class ExchangeSession { return dateFormat; } + protected static SimpleDateFormat getVcardBdayFormat() { + SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD, Locale.ENGLISH); + dateFormat.setTimeZone(GMT_TIMEZONE); + return dateFormat; + } + protected static SimpleDateFormat getExchangeZuluDateFormat() { SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD_T_HHMMSS_Z, Locale.ENGLISH); dateFormat.setTimeZone(GMT_TIMEZONE); @@ -1724,8 +1731,16 @@ public abstract class ExchangeSession { writer.appendProperty("ROLE", get("profession")); writer.appendProperty("NICKNAME", get("nickname")); writer.appendProperty("X-AIM", get("im")); - - writer.appendProperty("BDAY", get("bday")); + // convert bday value + String bday = get("bday"); + if (bday != null && bday.length() > 0) { + try { + SimpleDateFormat parser = ExchangeSession.getZuluDateFormat(); + writer.appendProperty("BDAY", ExchangeSession.getVcardBdayFormat().format(parser.parse(bday))); + } catch (ParseException e) { + LOGGER.warn("Invalid date: " + bday); + } + } writer.appendProperty("CATEGORIES", get("keywords")); @@ -2761,10 +2776,9 @@ public abstract class ExchangeSession { try { SimpleDateFormat parser; if (value.length() == 10) { - parser = new SimpleDateFormat("yyyy-MM-dd"); - parser.setTimeZone(GMT_TIMEZONE); + parser = ExchangeSession.getVcardBdayFormat(); } else { - parser = ExchangeSession.getZuluDateFormat(); + parser = ExchangeSession.getExchangeZuluDateFormat(); } properties.put("bday", ExchangeSession.getExchangeZuluDateFormatMillisecond().format(parser.parse(value))); } catch (ParseException e) { diff --git a/src/test/davmail/exchange/TestExchangeSessionContact.java b/src/test/davmail/exchange/TestExchangeSessionContact.java index 7979f1a7..48496641 100644 --- a/src/test/davmail/exchange/TestExchangeSessionContact.java +++ b/src/test/davmail/exchange/TestExchangeSessionContact.java @@ -86,7 +86,7 @@ public class TestExchangeSessionContact extends AbstractExchangeSessionTestCase vCardWriter.appendProperty("ROLE", "profession"); vCardWriter.appendProperty("X-AIM", "im"); - vCardWriter.appendProperty("BDAY", "20000102T000000Z"); + vCardWriter.appendProperty("BDAY", "2000-01-02T00:00:00Z"); vCardWriter.appendProperty("CATEGORIES", "keyword1,keyword2"); vCardWriter.appendProperty("X-ASSISTANT", "secretarycn"); @@ -421,6 +421,6 @@ public class TestExchangeSessionContact extends AbstractExchangeSessionTestCase contact = getCurrentContact(); assertEquals("20000102T000000Z", contact.get("bday")); - + System.out.println(contact.getBody()); } }