Problem Description
We need various small utilities regardign numbers and dates.
This code snippet has included some of the common conversions for convenience.
This code snippet has included some of the common conversions for convenience.
Solution Description
There are two separate classes "DateUtils" and "NumberUtils". MyMain class shows the usage.
Code Snippet
NumberUtils.java
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class NumberUtils {
/**
* Method takes Object as parameter and returns decimal number.
* if argument is float or double and contains tailing zeros
* it removes them. If argument is float or double then no change in return type.
* Change the Format of the Number by changing the String Pattern
*/
public static String changeToDecimalFormat(Object number) {
BigDecimal bdNumber = new BigDecimal(number.toString());
bdNumber = bdNumber.stripTrailingZeros(); //Returns a BigDecimal with any trailing zero's removed
String pattern = "###,##0.0###########"; //To apply formatting when the number of digits in input equals the pattern
DecimalFormat newFormat = new DecimalFormat(pattern, new DecimalFormatSymbols(Locale.US));
return newFormat.format(bdNumber);
}
/* Method takes Object as parameter and removes commas from the parameter */
public static double removeCommasFromNumber(Object number) {
try {
StringBuffer inputNo = new StringBuffer(number.toString());
if (inputNo.length() > 0) {
while (inputNo.indexOf(",") != -1) {
inputNo.deleteCharAt(inputNo.indexOf(","));
}
} else {
return 0.0;
}
return Double.parseDouble(inputNo.toString());
} catch (NumberFormatException e) {
return 0.0;
}
}
/* Some times its required to have a fixed set of decimal places for a
* number. We can set that by changing the precision number for a particular
* input BigDecimal Input String
*/
public static String changeToRequiredDecimals(String bigDecimalString,
int precision) {
String newFormattedString = null;
String afterDecimal = null;
if (bigDecimalString == null || bigDecimalString.length() == 0) {
return "0.0";
}
if (bigDecimalString.contains(".")) {
afterDecimal = bigDecimalString.substring(bigDecimalString
.indexOf(".") + 1);
int length = Math.abs((afterDecimal.length() - precision));
if (afterDecimal.length() < precision) {
newFormattedString = bigDecimalString;
for (int i = 0; i < length; i++) {
newFormattedString = newFormattedString + "0";
}
} else if (afterDecimal.length() > precision) {
newFormattedString = bigDecimalString.substring(0,
bigDecimalString.length() - length);
if (precision == 0) {
newFormattedString = newFormattedString.substring(0,
newFormattedString.indexOf("."));
} else {
newFormattedString = bigDecimalString;
}
} else {
if (precision > 0)
newFormattedString = bigDecimalString + ".";
else
newFormattedString = bigDecimalString;
for (int i = 0; i < precision; i++) {
newFormattedString = newFormattedString + "0";
}
}
}
return newFormattedString;
}
}
DateUtils.java
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
public static Date getNthFromCurrentDate(int field, int nValue) {
Calendar current = Calendar.getInstance();
current.setLenient(true);
current.add(field, nValue);
return current.getTime();
}
public static Date convertStringToDate(String dateString, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(dateString);
} catch (ParseException e) {
return null;
}
}
public static Date convertStringToTimestamp(String dateString, String pattern) {
return new Timestamp(convertStringToDate(dateString, pattern).getTime());
}
}
MyMain.java
import java.math.BigDecimal;
/**
*
*/
public class MyMain {
/**
*Method
* @param args
*/
public static void main(String[] args) {
System.out.println(DateUtils.convertStringToDate("11.10.2010", "dd.MM.yyyy"));
System.out.println(DateUtils.convertStringToTimestamp("11.10.2010", "dd.MM.yyyy"));
System.out.println(DateUtils.getNthFromCurrentDate(1,2));
int intVar = 10;
double doubleVar = 10.504000;
float floatVar = 343534534348.5687654F;
String commaString = "343,534,535,000.0";
BigDecimal bdNumber = new BigDecimal("1234.8765");
System.out.println(NumberUtils.changeToDecimalFormat(new Integer(intVar)));
System.out.println(NumberUtils.changeToDecimalFormat(new Double(doubleVar)));
System.out.println(NumberUtils.changeToDecimalFormat(new Float(floatVar)));
System.out.println(NumberUtils.removeCommasFromNumber(commaString));
System.out.println(NumberUtils.changeToRequiredDecimals(bdNumber.toString(), 8));
}
}
No comments:
Post a Comment