/** * Converts a XingCalendar object into a String with the format "yyyy-MM-ddThh:mm:ssZ". Useful to * save dates into the database, easily convertible to XingCalendar again with * com.xing.android.sdk.model.DateUtils.parseCalendarFromString(String ); It is possible to do the * same with a DateFormat, but due to the issues on DateFormat on API 15 and the simplicity of the * implementation, it's better to use our own one. * * @param calendar The XingCalendar to convert. * @return The String with the timestamp. */ public static String calendarToTimestamp(@NonNull XingCalendar calendar) { String output = null; if (calendar.isSet(Calendar.YEAR)) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(calendar.get(Calendar.YEAR)); if (calendar.isSet(Calendar.MONTH)) { stringBuilder.append('-'); stringBuilder.append(TWO_DIGITS_FORMATTER.format(calendar.get(Calendar.MONTH) + 1)); if (calendar.isSet(Calendar.DAY_OF_MONTH)) { stringBuilder.append('-'); stringBuilder.append(TWO_DIGITS_FORMATTER.format(calendar.get(Calendar.DAY_OF_MONTH))); if (isFilledToTime(calendar)) { stringBuilder.append('T'); stringBuilder.append(TWO_DIGITS_FORMATTER.format(calendar.get(Calendar.HOUR_OF_DAY))); stringBuilder.append(':'); stringBuilder.append(TWO_DIGITS_FORMATTER.format(calendar.get(Calendar.MINUTE))); stringBuilder.append(':'); stringBuilder.append(TWO_DIGITS_FORMATTER.format(calendar.get(Calendar.SECOND))); stringBuilder.append('Z'); } } } output = stringBuilder.toString(); } return output; }
/** * Checks if a XingCalendar object has all the necessary fields set to generate a timeStamp. These * fields are: year, month, day of month, hour of day, minute and second. * * @param calendar The XingCalendar to check. * @return True if has all the fields set, false otherwise. */ private static boolean isFilledToTime(@NonNull XingCalendar calendar) { return calendar.isSet(Calendar.HOUR_OF_DAY) && calendar.isSet(Calendar.MINUTE) && calendar.isSet(Calendar.SECOND); }