Friday, June 6, 2014

Java Formatting Price/Number According To Country/Currency

External Link


CurrencyFormat.java


import java.util.*;
import java.lang.*;
import java.text.*;


class CurrencyFormat {
 public static void main (String[] args) throws java.lang.Exception {
 Map<String, String> languagesMap = new TreeMap<String, String>();
        Locale[] locales = Locale.getAvailableLocales();
        for (Locale obj : locales) {
            if ((obj.getDisplayCountry() != null) && (!"".equals(obj.getDisplayCountry()))) {
                languagesMap.put(obj.getCountry(), obj.getLanguage());
            }
        }

        String[] countries = Locale.getISOCountries();
        int supportedLocale = 0, nonSupportedLocale = 0;
        for (String countryCode : countries) {
            Locale obj = null;
            if (languagesMap.get(countryCode) == null) {
                obj = new Locale("", countryCode);
                nonSupportedLocale++;
            }
            else {
                //create a Locale with own country's languages
                obj = new Locale(languagesMap.get(countryCode), countryCode);
                supportedLocale++;

            }
            NumberFormat currency = NumberFormat.getCurrencyInstance(obj);
            DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) currency).getDecimalFormatSymbols();
            String currencySymbol = decimalFormatSymbols.getCurrencySymbol();
            decimalFormatSymbols.setCurrencySymbol("");
            ((DecimalFormat) currency).setDecimalFormatSymbols(decimalFormatSymbols);

            System.out.println("Country Code = " + obj.getISO3Country()
                    + ", Price = " + currency.format(35147869.3518).trim()
                    + ", Symbol: " + currencySymbol
                    + ", Name: " + decimalFormatSymbols.getCurrency().getDisplayName()
                    + ", Code: " + decimalFormatSymbols.getCurrency().getCurrencyCode()
                    + ", Country Name = " + obj.getDisplayCountry(obj)
                    + ", Country Name = " + obj.getDisplayCountry()
                    + ", Languages = " + obj.getDisplayLanguage());
        }
        System.out.println("nonSupportedLocale : " + nonSupportedLocale);
        System.out.println("supportedLocale : " + supportedLocale);
    }
}

Output


Country Code = AND, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = ARE, Price = 35,147,869.35, Symbol: د.إ.‏, Code: AED
Country Code = AFG, Price = 35,147,869.35, Symbol: AFN, Code: AFN
Country Code = ATG, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = AIA, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = ALB, Price = 35.147.869,35, Symbol: Lek, Code: ALL
Country Code = ARM, Price = 35,147,869.35, Symbol: AMD, Code: AMD
Country Code = ANT, Price = 35,147,869.35, Symbol: ANG, Code: ANG
Country Code = AGO, Price = 35,147,869.35, Symbol: AOA, Code: AOA
Country Code = ATA, Price = 35,147,869.35, Symbol: ¤, Code: XXX
Country Code = ARG, Price = 35.147.869,35, Symbol: $, Code: ARS
Country Code = ASM, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = AUT, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = AUS, Price = 35,147,869.35, Symbol: $, Code: AUD
Country Code = ABW, Price = 35,147,869.35, Symbol: AWG, Code: AWG
Country Code = ALA, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = AZE, Price = 35,147,869.35, Symbol: AZN, Code: AZN
Country Code = BIH, Price = 35.147.869,35, Symbol: КМ., Code: BAM
Country Code = BRB, Price = 35,147,869.35, Symbol: BBD, Code: BBD
Country Code = BGD, Price = 35,147,869.35, Symbol: BDT, Code: BDT
Country Code = BEL, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = BFA, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = BGR, Price = 35 147 869,35, Symbol: лв., Code: BGN
Country Code = BHR, Price = 35,147,869.352, Symbol: د.ب.‏, Code: BHD
Country Code = BDI, Price = 35,147,869, Symbol: BIF, Code: BIF
Country Code = BEN, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = BLM, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = BMU, Price = 35,147,869.35, Symbol: BMD, Code: BMD
Country Code = BRN, Price = 35,147,869.35, Symbol: BND, Code: BND
Country Code = BOL, Price = 35.147.869,35, Symbol: B$, Code: BOB
Country Code = BES, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = BRA, Price = 35.147.869,35, Symbol: R$, Code: BRL
Country Code = BHS, Price = 35,147,869.35, Symbol: BSD, Code: BSD
Country Code = BTN, Price = 35,147,869.35, Symbol: BTN, Code: BTN
Country Code = BVT, Price = 35,147,869.35, Symbol: NOK, Code: NOK
Country Code = BWA, Price = 35,147,869.35, Symbol: BWP, Code: BWP
Country Code = BLR, Price = 35 147 869, Symbol: Руб, Code: BYR
Country Code = BLZ, Price = 35,147,869.35, Symbol: BZD, Code: BZD
Country Code = CAN, Price = 35 147 869,35, Symbol: $, Code: CAD
Country Code = CCK, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = COD, Price = 35,147,869.35, Symbol: CDF, Code: CDF
Country Code = CAF, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = COG, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = CHE, Price = 35'147'869.35, Symbol: SFr., Code: CHF
Country Code = CIV, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = COK, Price = 35,147,869.35, Symbol: NZD, Code: NZD
Country Code = CHL, Price = 35.147.869, Symbol: Ch$, Code: CLP
Country Code = CMR, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = CHN, Price = 35,147,869.35, Symbol: ¥, Code: CNY
Country Code = COL, Price = 35.147.869,35, Symbol: $, Code: COP
Country Code = CRI, Price = 35,147,869.35, Symbol: C, Code: CRC
Country Code = CUB, Price = 35,147,869.35, Symbol: CUP, Code: CUP
Country Code = CPV, Price = 35,147,869.35, Symbol: CVE, Code: CVE
Country Code = CUW, Price = 35,147,869.35, Symbol: ANG, Code: ANG
Country Code = CXR, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = CYP, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = CZE, Price = 35 147 869,35, Symbol: Kč, Code: CZK
Country Code = DEU, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = DJI, Price = 35,147,869, Symbol: DJF, Code: DJF
Country Code = DNK, Price = 35.147.869,35, Symbol: kr, Code: DKK
Country Code = DMA, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = DOM, Price = 35,147,869.35, Symbol: RD$, Code: DOP
Country Code = DZA, Price = 35,147,869.35, Symbol: د.ج.‏, Code: DZD
Country Code = ECU, Price = 35.147.869,35, Symbol: $, Code: USD
Country Code = EST, Price = 35 147 869,35, Symbol: €, Code: EUR
Country Code = EGY, Price = 35,147,869.35, Symbol: ج.م.‏, Code: EGP
Country Code = ESH, Price = 35,147,869.35, Symbol: MAD, Code: MAD
Country Code = ERI, Price = 35,147,869.35, Symbol: ERN, Code: ERN
Country Code = ESP, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = ETH, Price = 35,147,869.35, Symbol: ETB, Code: ETB
Country Code = FIN, Price = 35 147 869,35, Symbol: €, Code: EUR
Country Code = FJI, Price = 35,147,869.35, Symbol: FJD, Code: FJD
Country Code = FLK, Price = 35,147,869.35, Symbol: FKP, Code: FKP
Country Code = FSM, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = FRO, Price = 35,147,869.35, Symbol: DKK, Code: DKK
Country Code = FRA, Price = 35 147 869,35, Symbol: €, Code: EUR
Country Code = GAB, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = GBR, Price = 35,147,869.35, Symbol: £, Code: GBP
Country Code = GRD, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = GEO, Price = 35,147,869.35, Symbol: GEL, Code: GEL
Country Code = GUF, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = GGY, Price = 35,147,869.35, Symbol: GBP, Code: GBP
Country Code = GHA, Price = 35,147,869.35, Symbol: GHS, Code: GHS
Country Code = GIB, Price = 35,147,869.35, Symbol: GIP, Code: GIP
Country Code = GRL, Price = 35,147,869.35, Symbol: DKK, Code: DKK
Country Code = GMB, Price = 35,147,869.35, Symbol: GMD, Code: GMD
Country Code = GIN, Price = 35,147,869, Symbol: GNF, Code: GNF
Country Code = GLP, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = GNQ, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = GRC, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = SGS, Price = 35,147,869.35, Symbol: GBP, Code: GBP
Country Code = GTM, Price = 35,147,869.35, Symbol: Q, Code: GTQ
Country Code = GUM, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = GNB, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = GUY, Price = 35,147,869.35, Symbol: GYD, Code: GYD
Country Code = HKG, Price = 35,147,869.35, Symbol: HK$, Code: HKD
Country Code = HMD, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = HND, Price = 35,147,869.35, Symbol: L, Code: HNL
Country Code = HRV, Price = 35.147.869,35, Symbol: Kn, Code: HRK
Country Code = HTI, Price = 35,147,869.35, Symbol: HTG, Code: HTG
Country Code = HUN, Price = 35 147 869,35, Symbol: Ft, Code: HUF
Country Code = IDN, Price = 35.147.869,35, Symbol: Rp, Code: IDR
Country Code = IRL, Price = 35,147,869.35, Symbol: €, Code: EUR
Country Code = ISR, Price = 35,147,869.35, Symbol: ש"ח, Code: ILS
Country Code = IMN, Price = 35,147,869.35, Symbol: GBP, Code: GBP
Country Code = IND, Price = 35,147,869.35, Symbol: Rs., Code: INR
Country Code = IOT, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = IRQ, Price = 35,147,869.352, Symbol: د.ع.‏, Code: IQD
Country Code = IRN, Price = 35,147,869.35, Symbol: IRR, Code: IRR
Country Code = ISL, Price = 35.147.869,, Symbol: kr., Code: ISK
Country Code = ITA, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = JEY, Price = 35,147,869.35, Symbol: GBP, Code: GBP
Country Code = JAM, Price = 35,147,869.35, Symbol: JMD, Code: JMD
Country Code = JOR, Price = 35,147,869.352, Symbol: د.أ.‏, Code: JOD
Country Code = JPN, Price = 35,147,869, Symbol: ¥, Code: JPY
Country Code = KEN, Price = 35,147,869.35, Symbol: KES, Code: KES
Country Code = KGZ, Price = 35,147,869.35, Symbol: KGS, Code: KGS
Country Code = KHM, Price = 35,147,869.35, Symbol: KHR, Code: KHR
Country Code = KIR, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = COM, Price = 35,147,869, Symbol: KMF, Code: KMF
Country Code = KNA, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = PRK, Price = 35,147,869.35, Symbol: KPW, Code: KPW
Country Code = KOR, Price = 35,147,869, Symbol: ₩, Code: KRW
Country Code = KWT, Price = 35,147,869.352, Symbol: د.ك.‏, Code: KWD
Country Code = CYM, Price = 35,147,869.35, Symbol: KYD, Code: KYD
Country Code = KAZ, Price = 35,147,869.35, Symbol: KZT, Code: KZT
Country Code = LAO, Price = 35,147,869.35, Symbol: LAK, Code: LAK
Country Code = LBN, Price = 35,147,869.35, Symbol: ل.ل.‏, Code: LBP
Country Code = LCA, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = LIE, Price = 35,147,869.35, Symbol: CHF, Code: CHF
Country Code = LKA, Price = 35,147,869.35, Symbol: LKR, Code: LKR
Country Code = LBR, Price = 35,147,869.35, Symbol: LRD, Code: LRD
Country Code = LSO, Price = 35,147,869.35, Symbol: LSL, Code: LSL
Country Code = LTU, Price = 35 147 869,35, Symbol: Lt, Code: LTL
Country Code = LUX, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = LVA, Price = 35 147 869,35, Symbol: Ls, Code: LVL
Country Code = LBY, Price = 35,147,869.352, Symbol: د.ل.‏, Code: LYD
Country Code = MAR, Price = 35,147,869.35, Symbol: د.م.‏, Code: MAD
Country Code = MCO, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = MDA, Price = 35,147,869.35, Symbol: MDL, Code: MDL
Country Code = MNE, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = MAF, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = MDG, Price = 35,147,869.35, Symbol: MGA, Code: MGA
Country Code = MHL, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = MKD, Price = 35.147.869,35, Symbol: Den, Code: MKD
Country Code = MLI, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = MMR, Price = 35,147,869.35, Symbol: MMK, Code: MMK
Country Code = MNG, Price = 35,147,869.35, Symbol: MNT, Code: MNT
Country Code = MAC, Price = 35,147,869.35, Symbol: MOP, Code: MOP
Country Code = MNP, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = MTQ, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = MRT, Price = 35,147,869.35, Symbol: MRO, Code: MRO
Country Code = MSR, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = MLT, Price = 35,147,869.35, Symbol: €, Code: EUR
Country Code = MUS, Price = 35,147,869.35, Symbol: MUR, Code: MUR
Country Code = MDV, Price = 35,147,869.35, Symbol: MVR, Code: MVR
Country Code = MWI, Price = 35,147,869.35, Symbol: MWK, Code: MWK
Country Code = MEX, Price = 35,147,869.35, Symbol: $, Code: MXN
Country Code = MYS, Price = 35,147,869.35, Symbol: RM, Code: MYR
Country Code = MOZ, Price = 35,147,869.35, Symbol: MZN, Code: MZN
Country Code = NAM, Price = 35,147,869.35, Symbol: NAD, Code: NAD
Country Code = NCL, Price = 35,147,869, Symbol: XPF, Code: XPF
Country Code = NER, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = NFK, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = NGA, Price = 35,147,869.35, Symbol: NGN, Code: NGN
Country Code = NIC, Price = 35,147,869.35, Symbol: $C, Code: NIO
Country Code = NLD, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = NOR, Price = 35 147 869,35, Symbol: kr, Code: NOK
Country Code = NPL, Price = 35,147,869.35, Symbol: NPR, Code: NPR
Country Code = NRU, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = NIU, Price = 35,147,869.35, Symbol: NZD, Code: NZD
Country Code = NZL, Price = 35,147,869.35, Symbol: $, Code: NZD
Country Code = OMN, Price = 35,147,869.352, Symbol: ر.ع.‏, Code: OMR
Country Code = PAN, Price = 35,147,869.35, Symbol: B, Code: PAB
Country Code = PER, Price = 35.147.869,35, Symbol: S/, Code: PEN
Country Code = PYF, Price = 35,147,869, Symbol: XPF, Code: XPF
Country Code = PNG, Price = 35,147,869.35, Symbol: PGK, Code: PGK
Country Code = PHL, Price = 35,147,869.35, Symbol: Php, Code: PHP
Country Code = PAK, Price = 35,147,869.35, Symbol: PKR, Code: PKR
Country Code = POL, Price = 35 147 869,35, Symbol: zł, Code: PLN
Country Code = SPM, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = PCN, Price = 35,147,869.35, Symbol: NZD, Code: NZD
Country Code = PRI, Price = 35,147,869.35, Symbol: $, Code: USD
Country Code = PSE, Price = 35,147,869.35, Symbol: ILS, Code: ILS
Country Code = PRT, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = PLW, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = PRY, Price = 35.147.869, Symbol: G, Code: PYG
Country Code = QAT, Price = 35,147,869.35, Symbol: ر.ق.‏, Code: QAR
Country Code = REU, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = ROU, Price = 35.147.869,35, Symbol: LEI, Code: RON
Country Code = SRB, Price = 35.147.869,35, Symbol: дин., Code: RSD
Country Code = RUS, Price = 35 147 869,35, Symbol: руб., Code: RUB
Country Code = RWA, Price = 35,147,869, Symbol: RWF, Code: RWF
Country Code = SAU, Price = 35,147,869.35, Symbol: ر.س.‏, Code: SAR
Country Code = SLB, Price = 35,147,869.35, Symbol: SBD, Code: SBD
Country Code = SYC, Price = 35,147,869.35, Symbol: SCR, Code: SCR
Country Code = SDN, Price = 35,147,869.35, Symbol: ج.س.‏, Code: SDG
Country Code = SWE, Price = 35 147 869,35, Symbol: kr, Code: SEK
Country Code = SGP, Price = 35,147,869.35, Symbol: S$, Code: SGD
Country Code = SHN, Price = 35,147,869.35, Symbol: SHP, Code: SHP
Country Code = SVN, Price = 35.147.869,35, Symbol: €, Code: EUR
Country Code = SJM, Price = 35,147,869.35, Symbol: NOK, Code: NOK
Country Code = SVK, Price = 35 147 869,35, Symbol: €, Code: EUR
Country Code = SLE, Price = 35,147,869.35, Symbol: SLL, Code: SLL
Country Code = SMR, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = SEN, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = SOM, Price = 35,147,869.35, Symbol: SOS, Code: SOS
Country Code = SUR, Price = 35,147,869.35, Symbol: SRD, Code: SRD
Country Code = STP, Price = 35,147,869.35, Symbol: STD, Code: STD
Country Code = SLV, Price = 35,147,869.35, Symbol: C, Code: SVC
Country Code = SXM, Price = 35,147,869.35, Symbol: ANG, Code: ANG
Country Code = SYR, Price = 35,147,869.35, Symbol: ل.س.‏, Code: SYP
Country Code = SWZ, Price = 35,147,869.35, Symbol: SZL, Code: SZL
Country Code = TCA, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = TCD, Price = 35,147,869, Symbol: XAF, Code: XAF
Country Code = ATF, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = TGO, Price = 35,147,869, Symbol: XOF, Code: XOF
Country Code = THA, Price = 35,147,869.35, Symbol: ฿, Code: THB
Country Code = TJK, Price = 35,147,869.35, Symbol: TJS, Code: TJS
Country Code = TKL, Price = 35,147,869.35, Symbol: NZD, Code: NZD
Country Code = TLS, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = TKM, Price = 35,147,869.35, Symbol: TMT, Code: TMT
Country Code = TUN, Price = 35,147,869.352, Symbol: د.ت.‏, Code: TND
Country Code = TON, Price = 35,147,869.35, Symbol: TOP, Code: TOP
Country Code = TUR, Price = 35.147.869,35, Symbol: TL, Code: TRY
Country Code = TTO, Price = 35,147,869.35, Symbol: TTD, Code: TTD
Country Code = TUV, Price = 35,147,869.35, Symbol: AUD, Code: AUD
Country Code = TWN, Price = 35,147,869.35, Symbol: NT$, Code: TWD
Country Code = TZA, Price = 35,147,869.35, Symbol: TZS, Code: TZS
Country Code = UKR, Price = 35 147 869,35, Symbol: грн., Code: UAH
Country Code = UGA, Price = 35,147,869.35, Symbol: UGX, Code: UGX
Country Code = UMI, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = USA, Price = 35,147,869.35, Symbol: $, Code: USD
Country Code = URY, Price = 35.147.869,35, Symbol: NU$, Code: UYU
Country Code = UZB, Price = 35,147,869.35, Symbol: UZS, Code: UZS
Country Code = VAT, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = VCT, Price = 35,147,869.35, Symbol: XCD, Code: XCD
Country Code = VEN, Price = 35.147.869,35, Symbol: Bs.F., Code: VEF
Country Code = VGB, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = VIR, Price = 35,147,869.35, Symbol: USD, Code: USD
Country Code = VNM, Price = 35.147.869,35, Symbol: đ, Code: VND
Country Code = VUT, Price = 35,147,869, Symbol: VUV, Code: VUV
Country Code = WLF, Price = 35,147,869, Symbol: XPF, Code: XPF
Country Code = WSM, Price = 35,147,869.35, Symbol: WST, Code: WST
Country Code = YEM, Price = 35,147,869.35, Symbol: ر.ي.‏, Code: YER
Country Code = MYT, Price = 35,147,869.35, Symbol: EUR, Code: EUR
Country Code = ZAF, Price = 35,147,869.35, Symbol: R, Code: ZAR
Country Code = ZMB, Price = 35,147,869.35, Symbol: ZMK, Code: ZMK
Country Code = ZWE, Price = 35,147,869.35, Symbol: ZWL, Code: ZWL
nonSupportedLocale : 155
supportedLocale : 94

Thursday, June 5, 2014

Java: Rounding Number To Nearest Interval Such: 0.05, 0.5, 0.1, 5, 10, 100

RoundNumber.java


import java.math.BigDecimal;
import java.math.RoundingMode;
class RoundNumber {
    public static void main(String[] args) throws Exception {
         BigDecimal Price = new BigDecimal("232.573");
         BigDecimal Tick = new BigDecimal("0.05");
         BigDecimal Price2 = Price.divide(Tick, 9, RoundingMode.HALF_EVEN);
         Price2 = Price2.setScale(0, RoundingMode.HALF_UP).multiply(Tick);
         System.out.println("Price: " + Price + ", Round: " + Tick + ", Converted: " + Price2);
    }
}

Output:


Price: 232.573, Round: 0.05, Converted: 232.55
Price: 232.533, Round: 0.05, Converted: 232.55
Price: 232.523, Round: 0.05, Converted: 232.50
Price: 232.5235455, Round: 0.005, Converted: 232.525
Price: 232.5235, Round: 0.5, Converted: 232.5
Price: 232.446, Round: 0.5, Converted: 232.5
Price: 232.246, Round: 0.5, Converted: 232.0
Price: 1455.546, Round: 0.05, Converted: 1455.55
Price: 1455.528, Round: 0.05, Converted: 1455.55
Price: 1455.524, Round: 0.05, Converted: 1455.50
Price: 232.246, Round: 5, Converted: 230
Price: 232.546, Round: 5, Converted: 235
Price: 232.546, Round: 10, Converted: 230
Price: 235.546, Round: 10, Converted: 240
Price: 35.546, Round: 100, Converted: 0
Price: 55.546, Round: 100, Converted: 100
Price: 1455.524, Round: 100, Converted: 1500
Price: 1355.524, Round: 100, Converted: 1400
Price: 1248.524, Round: 100, Converted: 1200
Price: 1455.546, Round: 0.10, Converted: 1455.50
Price: 1455.556, Round: 0.10, Converted: 1455.60

External Link

Tuesday, June 3, 2014

MySql Convert String to Date

STR_TO_DATE('Jun/03/2014', '%M/%d/%Y');  => 2014-06-03; [VALID]
STR_TO_DATE('2014-04-25', '%Y-%m-%d'); => 2014-06-25; [VALID]
STR_TO_DATE( '69/Jun/2014', '%d/%M/%Y' ) => NULL; [INVALID]
 
The formats that can be used are:
Format Description
%aAbbreviated weekday name
%bAbbreviated month name
%cMonth, numeric
%DDay of month with English suffix
%dDay of month, numeric (00-31)
%eDay of month, numeric (0-31)
%fMicroseconds
%HHour (00-23)
%hHour (01-12)
%IHour (01-12)
%iMinutes, numeric (00-59)
%jDay of year (001-366)
%kHour (0-23)
%lHour (1-12)
%MMonth name
%mMonth, numeric (00-12)
%pAM or PM
%rTime, 12-hour (hh:mm:ss AM or PM)
%SSeconds (00-59)
%sSeconds (00-59)
%TTime, 24-hour (hh:mm:ss)
%UWeek (00-53) where Sunday is the first day of week
%uWeek (00-53) where Monday is the first day of week
%VWeek (01-53) where Sunday is the first day of week, used with %X
%vWeek (01-53) where Monday is the first day of week, used with %x
%WWeekday name
%wDay of the week (0=Sunday, 6=Saturday)
%XYear of the week where Sunday is the first day of week, four digits, used with %V
%xYear of the week where Monday is the first day of week, four digits, used with %v
%YYear, four digits
%yYear, two digits
 

Saturday, May 10, 2014

Display Hibernate Sql Parameter Values

Configure Log4j in Hibernate

Create a file named 'log4j.properties' under main project, where 'hibernate.cfg.xml' exists with following contents. This configuration will log sql in a file, the location you specified and also log in your project IDE console.


# Log everything. Good for troubleshooting  
log4j.logger.org.hibernate=INFO
# Log all SQL DML statements as they are executed
log4j.logger.org.hibernate.SQL=TRACE
# Log all JDBC parameters(TRACE|INHERITED);
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.hql.ast.AST=info
# Log all SQL DDL statements as they are executed(INHERITED|WARN)
log4j.logger.org.hibernate.tool.hbm2ddl=INHERITED
log4j.logger.org.hibernate.hql=debug
log4j.logger.org.hibernate.cache=info
log4j.logger.org.hibernate.jdbc=debug   
# Log all second-level cache activity
log4j.logger.org.hibernate.cache=INHERITED
# Log all OSCache activity - used by Hibernate
log4j.logger.com.opensymphony.oscache=INHERITED
# Log transaction related activity
log4j.logger.org.hibernate.transaction=INHERITED
# Log all JDBC resource acquisition
log4j.logger.org.hibernate.jdbc=INHERITED
# Log all JAAS authorization requests
log4j.logger.org.hibernate.secure=INHERITED
# Root logger option  
log4j.rootLogger=INFO, CUSTOM_FILE, CUSTOM_STD_OUT  

# Define the file appender
log4j.appender.CUSTOM_FILE=org.apache.log4j.DailyRollingFileAppender
# Set the DatePattern (Every hour interval)
log4j.appender.CUSTOM_FILE.DatePattern='['yyyy-MM-dd-HH'].log'
# Set the DatePattern (Every minute interval)
#log4j.appender.CUSTOM_FILE.DatePattern='['yyyy-MM-dd-HH-mm'].log'
# Set the append to false, should not overwrite
log4j.appender.CUSTOM_FILE.Append=true
# Set the name of the file.
# It'll be created every hour/minute with different 
# filename(appnded with yyyy-MM-dd-HH/yyyy-MM-dd-HH-mm).
log4j.appender.CUSTOM_FILE.File=C:/tmp/debug.log
# Define the layout for file appender
log4j.appender.CUSTOM_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.CUSTOM_FILE.layout.conversionPattern= %d{HH:mm:ss} %m%n

# Direct log messages to stdout  
log4j.appender.CUSTOM_STD_OUT=org.apache.log4j.ConsoleAppender  
log4j.appender.CUSTOM_STD_OUT.Target=System.out  
log4j.appender.CUSTOM_STD_OUT.layout=org.apache.log4j.PatternLayout  
log4j.appender.CUSTOM_STD_OUT.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 
log4j.appender.CUSTOM_STD_OUT.layout.ConversionPattern=%d{ABSOLUTE} - %m%n

Execute following line of code when project start/run, before performing any CRUD operations:


ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL url = loader.getResource("log4j.properties");
System.out.println(url);
PropertyConfigurator.configure(url);

You have to include log4j-1.2.16.jar in your project

When you perform query, output would be like this in your IDE consle:


/* When perform HQL */
10:30:29,149 - HQL: SELECT MIN(id) FROM com.pkm.domains.User

/* When perform SQL */
10:30:29,149 - SQL: select min(user0_.id) as col_0_0_ from User user0_
10:30:29,149 - throwQueryException() : no errors
10:30:29,191 - select min(user0_.id) as col_0_0_ from User user0_ limit ?
10:30:29,226 - Found [14] as column [col_0_0_]
10:30:29,228 - select user0_.id as id0_0_, user0_.date_updated as date2_0_0_, user0_.email as email0_0_, user0_.first_name as first4_0_0_, user0_.last_name as last5_0_0_, user0_.phone_number as phone6_0_0_, user0_.area_code as area7_0_0_ from User user0_ where user0_.id=?
10:30:29,230 - binding parameter [1] as [BIGINT] - 14
10:30:29,238 - Found [2014-05-10 10:23:08.0] as column [date2_0_0_]
10:30:29,238 - Found [pritomkucse@gmail.com] as column [email0_0_]
10:30:29,238 - Found [Pritom] as column [first4_0_0_]
10:30:29,239 - Found [Kumar Mondal] as column [last5_0_0_]

/* Update Query */
10:30:29,261 - update User set date_updated=?, email=?, first_name=?, last_name=?, phone_number=?, area_code=? where id=?
10:30:29,262 - binding parameter [1] as [TIMESTAMP] - Sat May 10 10:30:29 ALMT 2014
10:30:29,263 - binding parameter [2] as [VARCHAR] - pritomkucse@gmail.com
10:30:29,263 - binding parameter [3] as [VARCHAR] - Pritom
10:30:29,264 - binding parameter [4] as [VARCHAR] - Kumar Mondal
10:30:29,264 - binding parameter [7] as [BIGINT] - 14

Download complete example. This example also contains hibernate interceptor logic

Friday, May 9, 2014

Java Replace Camel Case String With Another String In That Place

MainClass.java

package com.pkm;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainClass {
    public static String mainString = "PritomKumarMondal";
    
    public static void main(String[] args) {
        System.out.println("Method: getString()");
        System.out.println("\t" + getString());
        System.out.println("Method: getString2()");
        System.out.println("\t" + getString2());
        System.out.println("Method: getString3()");
        System.out.println("\t" + getString3());
        System.out.println("Method: getString4()");
        System.out.println("\t" + getString4());
        System.out.println("Method: getString5()");
        System.out.println("\t" + getString5());
    }
    
    public static String getString() {
        System.out.println("\t" + MainClass.mainString);
        return MainClass.mainString.replaceAll("[^a-zA-Z0-9]+", "").replaceAll(
                String.format("%s|%s|%s",
                "(?<=[A-Z])(?=[a-z])",
                "(?<=[^A-Z])(?=[A-Z])",
                "(?<=[A-Za-z])(?=[^A-Za-z])"
                ),
            "."
        ).toLowerCase();
    }
    
    public static String getString2() {
        System.out.println("\t" + MainClass.mainString);
        String string = MainClass.mainString.replaceAll(
                String.format("%s",
                "(?=[A-Z])(?=[A-Z])"
                ),
            "."
        ).toLowerCase();
        string = string.replaceAll("[^a-zA-Z0-9.]+", "");
        return string.startsWith(".") ? string.substring(1) : string;
    }
    
    public static String getString3() {
        System.out.println("\t" + MainClass.mainString);
        return MainClass.mainString.replaceAll(
                String.format("%s",
                "(?<=[^A-Z])(?=[A-Z])"
                ),
            "."
        ).toLowerCase().replaceAll("[^a-zA-Z0-9.]+", "");
    }
    
    public static String getString4() {
        System.out.println("\t" + MainClass.mainString);
        String str1 = MainClass.mainString;
        str1 = str1.replaceAll("[A-Z]+", ".$0").toLowerCase().replaceAll("[^a-zA-Z0-9.]+", "");
        str1 = str1.startsWith(".") ? str1.substring(1) : str1;
        return str1;
    }
    
    public static String getString5() {
        System.out.println("\t" + MainClass.mainString);
        String str1 = MainClass.mainString;
        Pattern pattern = Pattern.compile("[A-Z]+");
        Matcher matcher = pattern.matcher(str1);
        StringBuffer output = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(output, "." + matcher.group().toLowerCase());
        }
        matcher.appendTail(output);
        str1 = output.toString().replaceAll("[^a-zA-Z0-9.]+", "");
        return str1.startsWith(".") ? str1.substring(1) : str1;
    }
}

Output using: 'sUIKAm009Pritom KumarMondalUUID'


Method: getString()
 sUIKAm009Pritom KumarMondalUUID
 s.uika.m.009.p.ritom.k.umar.m.ondal.uuid
Method: getString2()
 sUIKAm009Pritom KumarMondalUUID
 s.u.i.k.am009.pritom.kumar.mondal.u.u.i.d
Method: getString3()
 sUIKAm009Pritom KumarMondalUUID
 s.uikam009.pritom.kumar.mondal.uuid
Method: getString4()
 sUIKAm009Pritom KumarMondalUUID
 s.uikam009.pritom.kumar.mondal.uuid
Method: getString5()
 sUIKAm009Pritom KumarMondalUUID
 s.uikam009.pritom.kumar.mondal.uuid

Output using: 'PritomKumarMondal'


Method: getString()
 PritomKumarMondal
 p.ritom.k.umar.m.ondal
Method: getString2()
 PritomKumarMondal
 pritom.kumar.mondal
Method: getString3()
 PritomKumarMondal
 pritom.kumar.mondal
Method: getString4()
 PritomKumarMondal
 pritom.kumar.mondal
Method: getString5()
 PritomKumarMondal
 pritom.kumar.mondal

Output using: 'Pritom KUMAR MonDAL'


Method: getString()
 Pritom KUMAR MonDAL
 p.ritom.kumarm.on.dal
Method: getString2()
 Pritom KUMAR MonDAL
 pritom.k.u.m.a.r.mon.d.a.l
Method: getString3()
 Pritom KUMAR MonDAL
 pritom.kumar.mon.dal
Method: getString4()
 Pritom KUMAR MonDAL
 pritom.kumar.mon.dal
Method: getString5()
 Pritom KUMAR MonDAL
 pritom.kumar.mon.dal

Java Regex to convert string to ascii code and ascii code to string back

MainClass.java


package com.pkm;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainClass {
    public static void main(String[] args) {
        String str1 = "&#40;=>THAI(คุณ), HINDI:(तुम मेरी हो), HANGERIO:(تو مال منی), CHINA:(您), ARBI(أنت), FARSI(شما)";
        System.out.println("\n\nOriginal :: " +str1);        
        Pattern pattern = Pattern.compile("([^\\x00-\\x7F])|([^A-Za-z0-9-_])");
        StringBuffer output = new StringBuffer();
        Matcher matcher = pattern.matcher(str1);
        while (matcher.find()) {
            String mString = matcher.group(0);
            String rep = String.format("[%d \\%s = %d]", 
                    mString.length(), mString,
                    (int) mString.charAt(0));
            String rep2 = String.format("&#%d;", (int) mString.charAt(0));
            matcher.appendReplacement(output, rep2);
        }
        matcher.appendTail(output);
        System.out.println("Output   :: " + output.toString());
        
        pattern = Pattern.compile("\\&\\#\\d{2,}\\;");
        matcher = pattern.matcher(output.toString());
        output = new StringBuffer();
        while (matcher.find()) {
            String mString = matcher.group(0);
            mString = mString.substring(2);
            mString = mString.substring(0, mString.length() - 1);
            mString = Character.toString((char) Integer.parseInt(mString));
            matcher.appendReplacement(output, mString);
        }
        matcher.appendTail(output);
        System.out.println("Output   :: " + output.toString());
    }
}

Output as follows:

Original :: &#40;=>THAI(คุณ), HINDI:(तुम मेरी हो), HANGERIO:(تو مال منی), CHINA:(您), ARBI(أنت), FARSI(شما)


Output   :: &#38;&#35;40&#59;&#61;&#62;THAI&#40;&#3588;&#3640;&#3603;&#41;&#44;&#32;HINDI&#58;&#40;&#2340;&#2369;&#2350;&#32;&#2350;&#2375;&#2352;&#2368;&#32;&#2361;&#2379;&#41;&#44;&#32;HANGERIO&#58;&#40;&#1578;&#1608;&#32;&#1605;&#1575;&#1604;&#32;&#1605;&#1606;&#1740;&#41;&#44;&#32;CHINA&#58;&#40;&#24744;&#41;&#44;&#32;ARBI&#40;&#1571;&#1606;&#1578;&#41;&#44;&#32;FARSI&#40;&#1588;&#1605;&#1575;&#41;


Output   :: &#40;=>THAI(คุณ), HINDI:(तुम मेरी हो), HANGERIO:(تو مال منی), CHINA:(您), ARBI(أنت), FARSI(شما)

Java code to convert string to ascii code and ascii code to string back