Formatting user input Date.

Hi

I am having problem in changing the format of a user input date. I develop code with some methods for changing format and it is working. Only problem is displaying ?for example if month or date is ?st /Jan/ 2007?then it should appear ?1/01/2007?what I am getting is ?/1/2007? I will really appreciate for any help in getting the solution also if there is any way to use API to do so.

Following is my code for the above.

/**

*

*/

package test;

/**

* @author Destiny

*

*/

public class Date_Format {

String d_f = "05-05-2007"; //String date from user

int dd = Integer.parseInt( d_f.substring(0, 2));

int MM = Integer.parseInt(d_f.substring(3, 5));

String months[] = {

"Jan", "Feb", "Mar", "Apr",

"May", "Jun", "July", "Aug",

"Sep", "Oct", "Nov", "Dec"};

int yyyy = Integer.parseInt(d_f.substring(6, 10));

public void month_first(){

System.out.println(MM+"-"+dd+"-"+yyyy); // MM-dd-yyyy

}

public void year_first(){

System.out.println(yyyy+"-"+dd+"-"+MM); // yyyy-dd-MM

}

public void month_name_first(){

System.out.println(months[MM-1]+"/"+dd+"/"+yyyy); // (Jan or whatever)MM/dd/yyyy

}

public void month_name_second(){

System.out.println(dd+"/"+months[MM-1]+"/"+yyyy);// dd/(Jan or whatever)MM/yyyy

}

public static void main (String args[]){

Date_Format dd_f = new Date_Format();

dd_f.month_first();

dd_f.year_first();

dd_f.month_name_first();

dd_f.month_name_second();

}

}

/*

* Code End

*/

Thank You.

[1674 byte] By [Antimattera] at [2007-11-26 22:50:05]
# 1

take a look at SimpleDateFormat (http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html) and Calendar(http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html)

try {

// Some examples

DateFormat formatter = new SimpleDateFormat("MM/dd/yy");

Date date = (Date)formatter.parse("01/29/02");

formatter = new SimpleDateFormat("dd-MMM-yy");

date = (Date)formatter.parse("29-Jan-02");

// Parse a date and time; see also

// e317 Parsing the Time Using a Custom Format

formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");

date = (Date)formatter.parse("2002.01.29.08.36.33");

formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");

date = (Date)formatter.parse("Tue, 29 Jan 2002 22:14:02 -0500");

} catch (ParseException e) {

}

hth

java_2006a at 2007-7-10 12:11:02 > top of Java-index,Java Essentials,New To Java...