Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Appending a superscript Ordinal Indicator to a String

843785Jul 10 2008 — edited Jul 11 2008
Hi

I've been looking on the forums for a solution to this problem, and as I can't find one I thought I'd issue a request for assistance.

Basically, what I am trying to achieve is that I am trying to write a class that accepts an integer, and will print out that integer with the correct Ordinal Indicator as if it was a date.

An example to clarify, if the object is given a 1, the toString method of that object should return 1st. If it recieves a 2 it should be 2nd, a 3 gives 3rd etc.

My problem is that I would like the st, nd, rd or th to be superscript. Normally I'd look for the unicode value and append it to the string. But I've been unable to find the correct unicode values. The closet I've found is the code for an Ordinal Indicator (00AA) which is aparantly a combination unicode, so should be combined to give the required result. But I am unable to get it to work correctly. Every combination I've tried has resulted in a normal th or whatever and a small superscript square.

Has anyone got any suggestions about how I use combination Unicode values in Strings? Or a better way to acheive what I require?

Many Thanks.

Ruanae.

Below is a very basic example of my current class, when run will print out a set of values to the command line, should allow you to see what I've got, and what the issue is. Thanks again
public class SpecificDayDate {

	private Integer date;
	
	public SpecificDayDate(Integer date)
	{
		this.date = date;
	}

	public Integer getDate() {
		return date;
	}

	public void setDate(Integer date) {
		this.date = date;
	}
	
	public String toString()
	{
		String dateString = String.valueOf(getDate());
		
		if (dateString.length() == 1)
		{
			if (getDate() == 1)
			{
				dateString = dateString+"st\u00AA";
			}
			else if (getDate() == 2)
			{
				dateString = dateString+"nd\u00AA";
			}
			else if (getDate() == 3)
			{
				dateString = dateString+"rd\u00AA";
			}
			else
			{
				dateString = dateString+"th\u00AA";
			}
		}
		else
		{
			if (dateString.charAt(1)== '1' && getDate() != 11)
			{
				dateString = dateString+"st\u00AA";
			}
			else if (dateString.charAt(1)== '2' && getDate() != 12)
			{
				dateString = dateString+"nd\u00AA";
			}
			else if (dateString.charAt(1)== '3' && getDate() != 13)
			{
				dateString = dateString+"rd\u00AA";
			}
			else
			{
				dateString = dateString+"th\u00AA";
			}
		}
		
		return dateString;
	}
	
	public static void main(String[] args)
	{
		for (int i = 1; i < 31; i++)
		{
			SpecificDayDate sdd = new SpecificDayDate(i);
			System.out.println("Value: "+i+" becomes: "+sdd);
		}
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2008
Added on Jul 10 2008
2 comments
852 views