/** * 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; }
/** * Clear unused calendar fields by regular expression * * @param calendar The calendar to clear * @param regEx The reg ex */ private static void clearCalendarByRegEx(XingCalendar calendar, String regEx) { switch (regEx) { case YEAR_DATE_FORMAT: { calendar.clear(Calendar.MONTH); } case YEAR_MONTH_DATE_FORMAT: { calendar.clear(Calendar.DAY_OF_MONTH); } case YEAR_MONTH_DAY_DATE_FORMAT: { calendar.clear(Calendar.HOUR); calendar.clear(Calendar.MINUTE); calendar.clear(Calendar.SECOND); calendar.clear(Calendar.MILLISECOND); } } }
/** * Parse the string to a calendar instance * * @param dateStr The string to parse * @return A calendar instance with the encoded date, or null if nothing can be extracted */ @Nullable public static XingCalendar parseCalendarFromString(@Nullable String dateStr) { // Return null if the date string is empty if (TextUtils.isEmpty(dateStr)) { return null; } // Read the format entry DateFormat format = null; for (Map.Entry<String, DateFormat> entry : DATE_FORMAT_MAP.entrySet()) { if (dateStr.matches(entry.getKey())) { format = entry.getValue(); break; } } // No supported format if (format == null) { return null; } try { // Try to parse the date Date date = format.parse(dateStr); // Create a calendar instance, clear it and set the received time XingCalendar calendar = new XingCalendar(); calendar.clear(); calendar.setTime(date); // Clear unnecessary fields form calendar clearCalendarByRegEx(calendar, ((SimpleDateFormat) format).toPattern()); return calendar; } catch (ParseException ex) { return null; } }
/** * 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); }