Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix date parsing (GH issue #8)
- Loading branch information
Showing
2 changed files
with
90 additions
and
30 deletions.
There are no files selected for viewing
83 changes: 53 additions & 30 deletions
83
spring-social-xing/src/main/java/org/springframework/social/xing/api/XingDate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,64 @@ | ||
package org.springframework.social.xing.api; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import java.io.Serializable; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
public class XingDate implements Serializable { | ||
|
||
private static final Log logger = LogFactory.getLog(XingDate.class); | ||
|
||
private static final long serialVersionUID = 6747607636067970948L; | ||
private Integer year; | ||
private Integer month; | ||
|
||
|
||
public XingDate(String dateString) { | ||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); | ||
try { | ||
Calendar cal = Calendar.getInstance(); | ||
Date date = format.parse(dateString); | ||
cal.setTime(date); | ||
month = cal.get(Calendar.MONTH) + 1; // MONTH is zero based | ||
year = cal.get(Calendar.YEAR); | ||
} catch (ParseException e) { | ||
logger.fatal("failed to parse date string " + dateString, e); | ||
} | ||
} | ||
|
||
public Integer getMonth() { | ||
return month; | ||
} | ||
|
||
public Integer getYear() { | ||
return year; | ||
} | ||
private static final String DEFAULT_DATE_FORMAT = "yyyy-MM"; | ||
private static final String FALLBACK_DATE_FORMAT = "yyyy"; | ||
|
||
private static final Log logger = LogFactory.getLog(XingDate.class); | ||
|
||
private static final long serialVersionUID = 6747607636067970948L; | ||
private Integer year; | ||
private Integer month; | ||
|
||
public XingDate(String dateString) { | ||
|
||
if (dateString != null && !"".equals(dateString.trim())) { | ||
|
||
if (DEFAULT_DATE_FORMAT.length() == dateString.length()) { | ||
try { | ||
SimpleDateFormat format = new SimpleDateFormat( | ||
DEFAULT_DATE_FORMAT); | ||
Calendar cal = Calendar.getInstance(); | ||
Date date = format.parse(dateString); | ||
cal.setTime(date); | ||
month = cal.get(Calendar.MONTH) + 1; // MONTH is zero based | ||
year = cal.get(Calendar.YEAR); | ||
} catch (ParseException e) { | ||
logger.fatal("failed to parse date string " + dateString, e); | ||
} | ||
} else if (FALLBACK_DATE_FORMAT.length() == dateString.length()) { | ||
try { | ||
SimpleDateFormat fallbackFormat = new SimpleDateFormat( | ||
FALLBACK_DATE_FORMAT); | ||
Calendar cal = Calendar.getInstance(); | ||
Date date = fallbackFormat.parse(dateString); | ||
cal.setTime(date); | ||
year = cal.get(Calendar.YEAR); | ||
} catch (ParseException e) { | ||
logger.fatal("failed to parse date string " + dateString, e); | ||
} | ||
} else { | ||
logger.fatal("unknown date format: " + dateString); | ||
} | ||
|
||
} | ||
} | ||
|
||
public Integer getMonth() { | ||
return month; | ||
} | ||
|
||
public Integer getYear() { | ||
return year; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
spring-social-xing/src/test/java/org/springframework/social/xing/api/XingDateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.springframework.social.xing.api; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
public class XingDateTest { | ||
|
||
@Test | ||
public void testDifferentDateFormats() { | ||
|
||
{ | ||
XingDate date = new XingDate("2010-01"); | ||
assertEquals(Integer.valueOf(2010), date.getYear()); | ||
assertEquals(Integer.valueOf(1), date.getMonth()); | ||
} | ||
|
||
{ | ||
XingDate date = new XingDate("2007"); | ||
assertEquals(Integer.valueOf(2007), date.getYear()); | ||
assertNull(date.getMonth()); | ||
} | ||
|
||
{ | ||
XingDate date = new XingDate(""); | ||
assertNull(date.getYear()); | ||
assertNull(date.getMonth()); | ||
} | ||
|
||
{ | ||
XingDate date = new XingDate(null); | ||
assertNull(date.getYear()); | ||
assertNull(date.getMonth()); | ||
} | ||
} | ||
|
||
} |