All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.prowidesoftware.swift.utils.SwiftFormatUtils Maven / Gradle / Ivy

The newest version!
/* 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 */
package com.prowidesoftware.swift.utils;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Currency;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.prowidesoftware.swift.model.BIC;

/**
 * This class provides methods to convert field components to objects. 
 * It handles for example; dates, currencies and general functions defined in the SWIFT standard.
 * 
    *
  • <DATE1> MMDD *
  • <DATE2> YYMMDD *
  • <DATE3> YYMM *
  • <DATE4> YYYYMMDD *
  • <YEAR> YYYY *
  • <AMOUNT> ###,### (digits with comma as decimal separator) *
  • <TIME2> HHmmss *
  • <TIME3> HH[mm] *
* * @author www.prowidesoftware.com * @since 6.0 * @version $Id: SwiftFormatUtils.java,v 1.8 2013/04/19 16:21:15 zubri Exp $ */ public class SwiftFormatUtils { private static transient final java.util.logging.Logger log = java.util.logging.Logger.getLogger(SwiftFormatUtils.class.getName()); /** * Parses a DATE2 string (accept dates in format YYMMDD) into a Calendar object. * @param strDate string to parse * @return parsed date or null if the argument did not matched the expected date format */ public static Calendar getDate2(String strDate) { if (strDate != null && strDate.length() == 6) { return getCalendar(strDate, "yyMMdd"); } else { return null; } } /** * Parses a Calendar object into a DATE2 string. * @param date Calendar to parse * @return parsed date or null if the calendar is null */ public static String getDate2(Calendar date) { return getCalendar(date, "yyMMdd"); } /** * Parses a DATE1 string (accept dates in format MMDD) into a Calendar object. * @param strDate string to parse * @return parsed date or null if the argument did not matched the expected date format */ public static Calendar getDate1(String strDate) { if (strDate != null && strDate.length() == 4) { return getCalendar(strDate, "MMdd"); } else { return null; } } /** * Parses a Calendar object into a DATE1 string. * @param date Calendar to parse * @return parsed date or null if the calendar is null * @since 6.4 */ public static String getDate1(Calendar date) { return getCalendar(date, "MMdd"); } /** * Parses a DATE3 string (accept dates in format YYMM) into a Calendar object. * @param strDate string to parse * @return parsed date or null if the argument did not matched the expected date format */ public static Calendar getDate3(String strDate) { if (strDate != null && strDate.length() == 4) { return getCalendar(strDate, "yyMM"); } else { return null; } } /** * Parses a Calendar object into a DATE1 string. * @param date Calendar to parse * @return parsed date or null if the calendar is null * @since 6.4 */ public static String getDate3(Calendar date) { return getCalendar(date, "yyMM"); } /** * Parses a DATE4 string (accept dates in format YYYYMMDD) into a Calendar object. * @param strDate string to parse * @return parsed date or null if the argument did not matched the expected date format */ public static Calendar getDate4(String strDate) { if (strDate != null && strDate.length() == 8) { return getCalendar(strDate, "yyyyMMdd"); } else { return null; } } /** * Parses a Calendar object into a DATE1 string. * @param date Calendar to parse * @return parsed date or null if the calendar is null * @since 6.4 */ public static String getDate4(Calendar date) { return getCalendar(date, "yyyyMMdd"); } /** * Parses a YEAR string (accept dates in format YYYY) into a Calendar object. * @param strDate string to parse * @return parsed date or null if the argument did not matched the expected date format */ public static Calendar getYear(String strDate) { if (strDate != null && strDate.length() == 4) { return getCalendar(strDate, "yyyy"); } else { return null; } } /** * Parses a Calendar object into a YYYY string. * @param date Calendar to parse * @return parsed date or null if the calendar is null * @since 6.4 */ public static String getYear(Calendar date) { return getCalendar(date, "yyyy"); } /** * Parses a value into a java Number (BigDecimal) using the comma for decimal separator. * @param amount to parse * @return Number of the parsed amount or null if the number could not be parsed */ public static Number getNumber(String amount) { Number number = null; if (amount != null) { try { DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator(','); DecimalFormat df = new DecimalFormat("00.##", symbols); df.setParseBigDecimal(true); number = df.parse(amount); } catch (ParseException e) { log.log(java.util.logging.Level.WARNING, "Error parsing number", e); } } return number; } /** * Parses a Number into a SWIFT string number ####,## where trunked zero decimals and mandatory decimal separator. *
    *
  • Example: 1234,
  • *
  • Example: 1234,56
  • *
* @param number to parse * @return Number of the parsed amount or null if the number is null */ public static String getNumber(Number number) { if (number != null) { DecimalFormatSymbols symbols = new DecimalFormatSymbols(); symbols.setDecimalSeparator(','); DecimalFormat df = new DecimalFormat("0.##", symbols); df.setParseBigDecimal(true); df.setDecimalSeparatorAlwaysShown(true); String formatted = df.format(number); String result = StringUtils.replaceChars(formatted, '.', ','); return result; } return null; } /** * @param hhmm hour and minutes * @return a Calendar set with the given hour and minutes */ public static Calendar getHhmm(String hhmm) { if (hhmm != null && hhmm.length() == 4) { return getCalendar(hhmm, "HHmm"); } else { return null; } } private static Calendar getCalendar(String value, final String format) { if (value != null) { try { final SimpleDateFormat sdf = new SimpleDateFormat(format); sdf.setLenient(false); final Date d = sdf.parse(value); final Calendar cal = new GregorianCalendar(); cal.setTime(d); return cal; } catch (ParseException e) { log.log(java.util.logging.Level.WARNING, "Could not parse '"+value+"' with pattern '"+format+"'"); } } return null; } /** * @since 6.4 */ private static String getCalendar(Calendar date, final String format) { if (date != null) { return DateFormatUtils.format(date.getTime(), format); } return null; } /** * @param hhmmss hour, minutes and seconds * @return a Calendar set with the given hour, minutes and seconds */ public static Calendar getTime2(String hhmmss) { if (hhmmss != null && hhmmss.length() == 6) { return getCalendar(hhmmss, "HHmmss"); } else { return null; } } /** * Parses a Calendar object into a TIME2 string. * @param date Calendar to parse * @return parsed time or null if the calendar is null * @since 6.4 */ public static String getTime2(Calendar date) { return getCalendar(date, "HHmmss"); } /** * @param hhmmss hour, minutes and seconds * @return a Calendar set with the given hour, or and hour and minutes */ public static Calendar getTime3(String hhmmss) { if (hhmmss != null) { if (hhmmss.length() == 2) { return getCalendar(hhmmss, "HH"); } else if (hhmmss.length() == 4) { return getCalendar(hhmmss, "HHmm"); } } return null; } /** * Parses a Calendar object into a TIME3 string. * @param date Calendar to parse * @return parsed time or null if the calendar is null * @since 6.4 */ public static String getTime3(Calendar date) { return getCalendar(date, "HHmm"); } /** * @param string with a single character * @return the Character for the given string */ public static Character getSign(String string) { if (StringUtils.isNotEmpty(string)) { return Character.valueOf(string.charAt(0)); } return null; } /** * @param string with an offset * @return a Calendar set with hour and minutes from the offset */ public static Calendar getOffset(String string) { final Calendar result = getHhmm(string); return result; } /** * Parses a Calendar object into a offset string. * @param date Calendar to parse * @return parsed time or null if the calendar is null * @since 6.4 */ public static String getOffset(Calendar date) { return getCalendar(date, "HHmm"); } /** * @param string with a currency code * @return a Currency initialized from the parameter code */ public static Currency getCurrency(String code) { return Currency.getInstance(code); } /** * Gets the currency code from the parameter Currency. * @param currency Currency to use * @return the string with the currency code * @since 6.4 */ public static String getCurrency(Currency currency) { return currency.getCurrencyCode(); } /** * @param string with a BIC code * @return a BIC initialized from the parameter code */ public static BIC getBIC(String code) { return new BIC(code); } /** * Gets the code from the parameter BIC. * @param bic BIC to use * @return the string with the BIC code * @since 6.4 */ public static String getBIC(BIC bic) { return bic.getBic(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy