Advertisement
IronJoo

Untitled

Nov 16th, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1.     /**
  2.      * Get a String with a detailed formatted date and time
  3.      *
  4.      * @param date     The instance of the class Date (with time)
  5.      * @param language The l10n language in the two-char ISO format
  6.      * @param shortDate A flag indicating whether to return short date details.
  7.      *                  If set to true, the formatted string includes a shorter version of the date,
  8.      *                  with abbreviated day of the week and month.
  9.      *                  If set to false, the complete date information with full day of the week and month is included.
  10.      * @return The string with the formatted date and time
  11.      */
  12.     public String getLocalizedDetailedDateTimeString(Date date, String language, Boolean shortDate){
  13.         return getLocalizedDetailedDateTimeString(date, language, shortDate, true);
  14.     }
  15.  
  16.     /**
  17.      * Get a String with a detailed formatted date
  18.      *
  19.      * @param date     The instance of the class Date (with time)
  20.      * @param language The l10n language in the two-char ISO format
  21.      * @param shortDate A flag indicating whether to return short date details.
  22.      *                  If set to true, the formatted string includes a shorter version of the date,
  23.      *                  with abbreviated day of the week and month.
  24.      *                  If set to false, the complete date information with full day of the week and month is included.
  25.      * @return The string with the formatted date
  26.      */
  27.     public String getLocalizedDetailedDateString(Date date, String language, Boolean shortDate){
  28.         return getLocalizedDetailedDateTimeString(date, language, shortDate, false);
  29.     }
  30.  
  31.     private String getLocalizedDetailedDateTimeString(Date date, String language, Boolean hasShortDate, Boolean hasTime){
  32.         if (date == null) {
  33.             return "";
  34.         }
  35.  
  36.         if (language == null || !language.equals(LANG_PT)) {
  37.             language = LANG_EN;
  38.         }
  39.  
  40.         SimpleDateFormat dayFormat = new SimpleDateFormat(DATE_FORMAT_DAY);
  41.         SimpleDateFormat yearAndTimeFormat = new SimpleDateFormat(DATE_TIME_FORMAT_YEAR_AND_TIME);
  42.         SimpleDateFormat yearFormat = new SimpleDateFormat(DATE_FORMAT_YEAR);
  43.  
  44.         String dayOfWeek;
  45.         String month;
  46.  
  47.         if (hasShortDate != null && hasShortDate){
  48.             dayOfWeek = getLocalizedDayOfWeekFromDateShort(date, language);
  49.             month = getLocalizedMonthFromDateShort(date, language);
  50.         } else {
  51.             dayOfWeek = getLocalizedDayOfWeekFromDate(date, language);
  52.             month = getLocalizedMonthFromDate(date, language);
  53.         }
  54.         return dayOfWeek + ", " + (language.equals(LANG_PT) ?
  55.                 (dayFormat.format(date) + " " + Character.toUpperCase(month.charAt(0)) + month.substring(1)) :
  56.                 (Character.toUpperCase(month.charAt(0)) + month.substring(1) + " " + dayFormat.format(date)))
  57.                 + " " + ((hasTime) ? yearAndTimeFormat.format(date) : yearFormat.format(date));
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement