user3521165
user3521165

Reputation: 11

Formatting date from mm/dd/yyyy to MM DD, YYYY

For example it will format the date 2/15/2014 to February 15, 2014

I have tried a switch to get the month and that works out, but I cant figure out how to get the rest after.

This is what I have so far :

private void formatDate()
{
    String month = dateDeparture.substring(0, dateDeparture.indexOf('/'));
    switch(dateDeparture)
    {
    case 1:
        month = "January";
        break;
    case 2:
        month = "February";
        break;
    case 3:
        month = "March";
        break;
    case 4:
        month = "April";
        break;
    case 5:
        month = "May";
        break;
    case 6:
        month = "June";
        break;
    case 7:
        month = "July";
        break;
    case 8:
        month = "August";
        break;
    case 9:
        month = "September";
        break;
    case 10:
        month = "October";
        break;
    case 11:
        month = "November";
        break;
    default:
        month = "December";
        break;  
    }
    dateDeparture = month+" "+dateDeparture.substring(dateDeparture.indexOf('/'),  dateDeparture.lastIndexOf('/'))+ dateDeparture.substring(dateDeparture.lastIndexOf('/'));

}

Upvotes: 0

Views: 206

Answers (4)

Warren Dew
Warren Dew

Reputation: 8938

Best would be to use a date formatting class like SimpleDateFormat:

SimpleDateFormat numericDateFormatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = numericDateFormatter.parse(dateDeparture);
SimpleDateFormat mixedDateFormatter = new SimpleDateFormat("MMMMMMMMM d, yyyy");
String dateNew = mixedDateFormatter.format(date);

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347332

So long as you are using Java 7, you can use String in switch statements...

But, there are two problems with your current code...

One, you are using dateDeparture and not month

switch(dateDeparture)

And your case statements are using int instead of String

case 1:

Instead, you need to use something more like...

switch (month) {
    case "1":
    case "01":
        month = "January";
        break;
}

Now, because you month "might" be zero indexed, you need to take into account both occurrences...

If you're using Java 6 or eailer, you will need to convert the String value to an int...

int monthValue = Integer.parseInt(month);
switch (monthValue) {
    case 1:
        //...

A simpler approach would be to use the inbuilt APIs available to you...

Start by converting the String value to a Date value...

SimpleDateFormat in = new SimpleDateFormat("MM/dd/yyyy");
Date date = in.parse(dateDeparture);

Then format the date...

SimpleDateFormat out = new SimpleDateFormat("MMMM dd, yyyy");
String value = out.format(date);

Take a close look at SimpleDateFormat for more details...

Upvotes: 0

user2360745
user2360745

Reputation: 121

Do this...

//splits string based on / delimiter 
String[] MDY = dateDeparture.split("/");
int month = Integer.parseInt(MDY[0]);
int day = Integer.parseInt(MDY[1]);
int year = Integer.parseInt(MDY[2]);

then use your switch statement on month and format day and year to your liking.

Upvotes: 0

Biswajit_86
Biswajit_86

Reputation: 3739

Java has a class called SimpleDateFormat. Use that to achieve what you are doing.

Upvotes: 1

Related Questions