user1283885 user1283885 - 2 months ago 11x
Java Question

Changing YYYY/MM/DD -> MM/DD/YYYY java

I wish to change my date formatting to MM/DD/YYYY, currently it is in YYYY/MM/DD.

I tried researching it, but to my irony, it is always the other way around. Now one might say try it backwards try working from there, but it didn't work.

My class for calling all the things:

import java.util.*;
import java.text.*;

class Driver {
public static void main (String[] args) {
Kid kid;
Node list = new Node();

kid = createKid("Lexie", 2.6, "11/5/2009");
insertEnd(list, kid);
kid = createKid ("Sally", 2.3, "4/8/2009");
insertEnd(list, kid);
kid = createKid ("Joe", 2.7, "6/16/2009");
insertEnd(list, kid);
kid = createKid ("Bob", 2.2, "1/16/2009");
insertEnd(list, kid);
kid = createKid ("Tom", 3.1, "8/16/2009");
insertEnd(list, kid);
} //end main method

public static Kid createKid(String name, double height, String date) {
return new Kid(name, height, date);

} //end class

import java.util.*;
import java.text.SimpleDateFormat;
class Kid {
String name;
double height;
GregorianCalendar bDay;

* Second constructor for kid
* Setting instances to equal the constructors of this
* @param 1: Setting n (aka name, but it was taken) to equal the instance var of name
* @param 2: Setting h (aka height, but it was taken) to equal the instance var of height
* @param 3: Setting date to equal the instance var of bDay with some modifications
public Kid (String n, double h, String date) {
StringTokenizer st = new StringTokenizer(date, "/"); = n;
this.height = h;
this.bDay = new GregorianCalendar(Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));

* public String toString() {
* Converting Java language to English language
public String toString() {

return ( + ", Height: " + this.height + "ft., Born: "
+ this.bDay.get(Calendar.DATE) + "/" + this.bDay.get(Calendar.MONTH)
+ "/" + this.bDay.get(Calendar.YEAR));

} //end class

By the way, the Simple Date Format class and Date Format class I am unfamiliar with and have unsuccessfully tried to implement them.


Just use SimpleDateFormat to convert String to Date. No need to hassle with painful Calendar API.

String dateString = "2012/06/05";
Date date = new SimpleDateFormat("yyyy/MM/dd").parse(dateString);

Use this Date object throughout your code instead. Whenever you need to present the Date object to humans, just use another SimpleDateFormat:

String dateString = new SimpleDateFormat("MM/dd/yyyy").format(date);