Rev 32603 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.common.util;import java.io.IOException;import java.math.BigDecimal;import java.math.RoundingMode;import java.text.NumberFormat;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.util.Locale;import static com.google.gson.internal.$Gson$Preconditions.checkArgument;public class FormattingUtils {private static final NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));private static final NumberFormat numberFormatter = NumberFormat.getNumberInstance(new Locale("en", "IN"));private static final NumberFormat digitFormatter = NumberFormat.getNumberInstance(new Locale("en", "IN"));static {numberFormatter.setMaximumFractionDigits(2);numberFormatter.setMinimumFractionDigits(2);numberFormatter.setRoundingMode(RoundingMode.HALF_UP);digitFormatter.setMinimumFractionDigits(0);digitFormatter.setMinimumFractionDigits(0);digitFormatter.setRoundingMode(RoundingMode.HALF_UP);}private static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");private static final DateTimeFormatter ddMMMyyyyHypenatedFormatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");private static final DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("MMM-dd");private static final DateTimeFormatter dateMonthFormatterNonHyphenated = DateTimeFormatter.ofPattern("MMM dd");private static final DateTimeFormatter reporticoFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");//rkbpublic static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");private static final DateTimeFormatter ymFormatter = DateTimeFormatter.ofPattern("yyyyMM");private static final DateTimeFormatter ymdFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");private static final DateTimeFormatter monYYYYFormatter = DateTimeFormatter.ofPattern("MMM, yyyy");public static DateTimeFormatter yyMMDDTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");public static String getYearMonth(LocalDateTime dateTime) {return dateTime.format(ymFormatter);}public static String formatYearMonth(LocalDateTime dateTime) {return dateTime.format(monYYYYFormatter);}public static String formatToYMD(LocalDateTime dateTime) {return dateTime.format(ymdFormatter);}public static String formatDecimal(double number) {return numberFormatter.format(number).replace(".00", "");}public static String formatDecimal(float number) {return numberFormatter.format(number).replace(".00", "");}public static String formatDigit(float number) {return digitFormatter.format(number);}public static String formatDigit(long number) {return digitFormatter.format(number);}public static String formatDigit(int number) {return digitFormatter.format(number);}public static String formatDigit(double number) {return digitFormatter.format(number);}public static String format(LocalDateTime localDateTime) {return (localDateTime == null) ? "" : localDateTime.format(dateTimeFormatter);}public static String formatPrice(double amount) {return currencyFormatter.format(amount).replace(".00", "");}public static String formatPrice(float amount) {return currencyFormatter.format(amount).replace(".00", "");}public static String formatPrice(int amount) {return currencyFormatter.format(amount);}public static String formatPrice(long amount) {return currencyFormatter.format(amount);}public static String formatDate(LocalDateTime dateTime) {return dateTime.format(dateFormatter);}public static String formatDate(LocalDate date) {return date.format(dateFormatter);}public static String formatDateMonth(LocalDateTime dateTime) {return dateTime.format(dateMonthFormatter);}public static String formatDateMonthNonHyphenated(LocalDateTime dateTime) {return dateTime.format(dateMonthFormatterNonHyphenated);}public static String formatDDMMMyyyyFormatter(LocalDate localDate) {return localDate.format(ddMMMyyyyHypenatedFormatter);}public static String formatReporitcoDate(LocalDateTime dateTime) {if (dateTime == null)return "-";return dateTime.format(reporticoFormatter);}public static String formatDateTime(LocalDateTime dateTime) {if (dateTime == null)return "-";return dateTime.format(yyMMDDTimeFormatter);}public static double serialize(double value) throws IOException {BigDecimal bd = new BigDecimal(value);bd = bd.setScale(2, RoundingMode.HALF_UP);return bd.doubleValue();}public static double serialize(BigDecimal value) {value = value.setScale(2, RoundingMode.HALF_UP);return value.doubleValue();}// https://github.com/google/guavaString getDayOfMonthSuffix(final int n) {checkArgument(n >= 1 && n <= 31);if (n >= 11 && n <= 13) {return "th";}switch (n % 10) {case 1:return "st";case 2:return "nd";case 3:return "rd";default:return "th";}}}