Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: Formating in Java
|
Posted: Jun 19, 2002 10:03 PM
|
|
import java.text.*; import java.util.*;
public class FormatStuff{ public FormatStuff(){ doStuff(); } public static void main(String[] args){ new FormatStuff(); } public void doStuff(){ //format I get from database is 2002-06-20, and I want to show it as 20th June 2002 System.out.println(formatDateString("2002-06-01")); System.out.println(formatDateString("2002-6-1")); System.out.println(formatDateString("2002-06-02")); System.out.println(formatDateString("2002-6-2")); System.out.println(formatDateString("2002-06-03")); System.out.println(formatDateString("2002-6-3")); System.out.println(formatDateString("2002-03-14")); System.out.println(formatDateString("2002-05-30")); //format a decimal number. Such as a number like 25000.6425. //I would like it be format to 25,000.6425.
System.out.println(formatDecimal(25000.6425)); System.out.println(formatDecimal(233354550.89996425)); System.out.println(formatDecimal(222455000.6425)); System.out.println(formatDecimal(1234.65)); System.out.println(formatDecimal(2544000.6425)); System.out.println(formatDecimal(25899999000.6465767367868325)); } public String formatNumber(double d){ return NumberFormat.getInstance().format(d); } public String formatDecimal(double d){ double integerPart = Math.floor(d); double fractionPart = d - integerPart; //System.out.println("double: " + d); //System.out.println("integerPart: " + integerPart); //System.out.println("fractionPart: " + fractionPart); DecimalFormat formatter = new DecimalFormat(); formatter.setGroupingSize(3); StringBuffer buffer = new StringBuffer(); buffer = formatter.format(integerPart,buffer,new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR)); return buffer.toString() + String.valueOf(fractionPart); } /** Assumes string input is like 2002-06-20. */ public String formatDateString(String dateString) { String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; String suffix = "th"; String[] parts = dateString.split("-"); if (parts[2].compareTo("1") == 0){ suffix = "st"; }else if (parts[2].compareTo("01") == 0){ suffix = "st"; parts[2] = "1"; }else if (parts[2].compareTo("2") == 0){ suffix = "nd"; }else if (parts[2].compareTo("02") == 0){ suffix = "nd"; parts[2] = "2"; }else if (parts[2].compareTo("3") == 0){ suffix = "rd"; }else if (parts[2].compareTo("03") == 0){ suffix = "rd"; parts[2] = "3"; }else if (parts[2].compareTo("04") == 0){ parts[2] = "4"; }else if (parts[2].compareTo("05") == 0){ parts[2] = "5"; }else if (parts[2].compareTo("06") == 0){ parts[2] = "6"; }else if (parts[2].compareTo("07") == 0){ parts[2] = "7"; }else if (parts[2].compareTo("08") == 0){ parts[2] = "8"; }else if (parts[2].compareTo("09") == 0){ parts[2] = "9"; } return parts[2] + suffix + " " + months[toInt(parts[1]) - 1] + " " + parts[0]; } private int toInt(String s){ int n = -1; try{ n = Integer.parseInt(s); }catch(NumberFormatException nfe){ System.err.println("NumberFormatException: " + nfe.getMessage()); } return n; }
}
|
|