Java program to print date in the format: 25th November 2011

A forum to discuss Java programming related issues and concerns.
no avatar
ganesh
 
Posts: 1
Joined: Mon Nov 26, 2012 5:13 am

Java program to print date in the format: 25th November 2011

by ganesh » Mon Nov 26, 2012 5:16 am

You are required to write a Java class, called Date, that can be used to represent dates.
The date object should be represented as three integers for day, month and year. The year must be stored in full, example, 2004 and not 04.
Provide appropriate constructors, get() and set() methods for day, month, year. getYear() should return the year in full ( 1998, 2004 etc. ) and getMonth() should return a String representing the name of the month. Finally, you should provide a toString() method that returns a String representation of the date in full, for example,
22nd December, 1999
12th January, 2006
The day should be followed by an appropriate suffix on the day, ‘th’, ‘st’, ‘nd’ or ‘rd’
Provide a main() method that tests your class thoroughly.

Source filename: DateEx.java

PLEASE HELP ME OUT !! ITS URGENT

no avatar
ajitpaswan
 
Posts: 11
Joined: Mon Dec 24, 2012 2:41 am
Location: India

by ajitpaswan » Tue Dec 25, 2012 5:04 am

import java.util.Calendar;

public class DateDemo {
private int date;
private int month;
private int year;

public int getDate() {
return date;
}

public void setDate(int date) {
this.date = date;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String toString() {
String modifiedMonth = getMonthInString(month);
if(date == 1 || date == 31) {
return "\n "+date+ "st "+ modifiedMonth+" "+year;
} else if(date == 2) {
return "\n "+date+ "nd "+ modifiedMonth+" "+year;
} else if(date == 3) {
return "\n "+date+ "rd "+ modifiedMonth+" "+year;
}
return "\n "+date+ "th "+ modifiedMonth+" "+year;
}

public String getMonthInString(int month) {
String[] monthName = {"January", "February",
"March", "April", "May", "June", "July",
"August", "September", "October", "November",
"December"
};
Calendar cal = Calendar.getInstance();
cal.set(0, month, 0);
String monthInString = monthName[cal.get(Calendar.MONTH)];
return monthInString;
}

public static void main(String args[]) {
DateDemo dateDemo = new DateDemo();
int date = 25, month = 11, year = 2012;
dateDemo.setDate(date);
dateDemo.setMonth(month);
dateDemo.setYear(year);
System.out.println(dateDemo);
}
}

Return to JAVA Lobby

cron