The Artima Developer Community
Sponsored Link

Java Answers Forum
Formating in Java

11 replies on 1 page. Most recent reply: Jun 21, 2002 10:47 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 11 replies on 1 page
kevin

Posts: 9
Nickname: kev
Registered: Jun, 2002

Formating in Java Posted: Jun 19, 2002 6:12 PM
Reply to this message Reply
Advertisement
Hello everybody. Nice to join this forum. I have some question related to the formating in Java. I hope everybody here can help me.
The case is like this...
1. I want to format a date data type from database to show in an html plain text to the specified format I want. When I read the class tutorial I just get know I should use the DateFormat combine with Date class in the package java.text and java.util. but, I don't know how to implement it.
Ex: the format I get from database is 2002-06-20, and I want to show it as 20th June 2002. Can I do it?
2. I want to format a decimal number. Such as a number like 25000.6425. I would like it be format to 25,000.6425. But, I don't know how to do it.

I hope anyone can help me...
Thank a lot.


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Formating in Java Posted: Jun 19, 2002 8:21 PM
Reply to this message Reply
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(formatNumber(25000.6425));
		System.out.println(formatNumber(233354550.89996425));
		System.out.println(formatNumber(222455000.6425));
		System.out.println(formatNumber(1234.65));
		System.out.println(formatNumber(2544000.6425));
		System.out.println(formatNumber(25899999000.6465767367868325));
		
	}
	
	public String formatNumber(double d){
		return NumberFormat.getInstance().format(d);
	}
	
	public String formatDecimal(double d){
		DecimalFormat formatter = new DecimalFormat();
		formatter.setGroupingSize(3);
		StringBuffer buffer = new StringBuffer();
		buffer = formatter.format(d,buffer,new FieldPosition(NumberFormat.Field.DECIMAL_SEPARATOR));
		return buffer.toString();
	}
	
	/**	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;
	}
 
}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Formating in Java Posted: Jun 19, 2002 8:23 PM
Reply to this message Reply
On my machine, this gives:

>java FormatStuff
1st June 2002
1st June 2002
2nd June 2002
2nd June 2002
3rd June 2002
3rd June 2002
14th March 2002
30th May 2002
25,000.642
233,354,550.9
222,455,000.642
1,234.65
2,544,000.642
25,899,999 ,000.647

kevin

Posts: 9
Nickname: kev
Registered: Jun, 2002

Re: Formating in Java Posted: Jun 19, 2002 9:14 PM
Reply to this message Reply
Thank a again charles.
You just gave me a solution to handle the date format.
But, by the way the NumberFormat isn't what I mean. I want to translate the number such as 25000,6545 and I really want it show 25,000.6545 without any round up. It should be exactly show all the last digits after decimal point.
Perhaps you can help me to search some solution.
Thank a lot for the date format. I like it.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Formating in Java Posted: Jun 19, 2002 9:45 PM
Reply to this message Reply
All you have to do is split the number into two parts, the integer part and fraction part.
Leave the fraction part alone.

Format the integer part as in the program and then concatenate that string with the string value of the fraction part.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Formating in Java Posted: Jun 19, 2002 10:03 PM
Reply to this message Reply

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;
}

}

kevin

Posts: 9
Nickname: kev
Registered: Jun, 2002

Re: Formating in Java Posted: Jun 19, 2002 11:21 PM
Reply to this message Reply
Its mean there is no any special formatting class to do so - like ###,###,###.#####
If we can insert that pattern perhaps the number will be formated automatically as the pattern.
But, for this mean while I will use urs way - to split of between integer part and fractional part.
Thank you anyway.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Formating in Java Posted: Jun 20, 2002 3:42 PM
Reply to this message Reply
try this:




public String formatNumber(double d){
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setParseIntegerOnly(true);
double integerPart = Math.floor(d);
String part1 = formatter.format(integerPart);
System.out.println(part1);

String decimal = String.valueOf(d);
String part2 = decimal.substring(decimal.indexOf("."));
System.out.println(part2);
System.out.println(part1 + part2);

return part1 + part2;
}

kevin

Posts: 9
Nickname: kev
Registered: Jun, 2002

Re: Formating in Java Posted: Jun 20, 2002 6:02 PM
Reply to this message Reply
I think I have found something more interesting. I hope you can review too.

Look at this code:
public String formatNumber(double d, int val) {
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMaximumFractionDigits(val);
String result = formatter.format(d);
return result;
}
I think this will look well.

Babu Reddy

Posts: 7
Nickname: babureddy
Registered: Jun, 2002

Re: Formating in Java Posted: Jun 20, 2002 8:48 PM
Reply to this message Reply
You can use the above number format with some extra additions ie
formatter.setMinimumFractionDigits(val);
so that even for the numbers with out fractions
values it will print up to a decimal of Val
ie 25000 will be printed as 25000.000( if val =3)
much more is available like
setGroupingUsed(boolean) in order to format the number with or with out , ie
if we set it to false result is
25000.000
if we set it to true result is
25,000.000 this can also be made according to the locale

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Formating in Java Posted: Jun 21, 2002 2:19 PM
Reply to this message Reply
Yeah I saw all that.

The java api is full of all kinds of formatting things.
If you haven't downloaded the java docs to refer to while you are developing, it would be a good idea.

When I compose code, i have a text editor window open right alongside a browser window with the java docs open in the next one. I am constantly referring to the java docs.

A lot of java programming can be done by simply looking at the java docs for a class and seeing what kind of objects a method needs as input, and copying and pasting from the java docs to the other. (Control c to copy, control-v to paste in windows)

I am always searching through all the classes and indexes for possible other methods and classes that may work even easier.

If an existing java class does not do exactly what you want it to, then as a programmer, you make up your own, test it, and use it. Of course you can always subclass a java class and override behavior to a specific way you want. That's the beauty of the language.

Many times the parent class of a java class will have other methods that a java class inherits. The java docs make it easy to link to them with a web browser.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Formating in Java Posted: Jun 21, 2002 10:47 PM
Reply to this message Reply
> When I compose code, i have a text editor window open
> right alongside a browser window with the java docs
> open in the next one. I am constantly referring to
> the java docs.

If you have one of the nifty code editors like NetBrain's (the erstwhile IntelliJ) IDEA, Borland's JBuilder, IBM's Eclipse, Sun's Forte (I think) and so on, you get this as you type, without having a browser open. Of course, I still have and frequently use the html Java Docs as well.

Flat View: This topic has 11 replies on 1 page
Topic: Byte Manipulation Previous Topic   Next Topic Topic: Accessing mailbox from landline phone

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use