# Java - Problem with Formatting Date



## r3drock3t88 (Jan 13, 2007)

Hey all, what I have to do is have the user input their date in the format "09/22/1988", and I need it to be formatted into the format "Saturday, September 22, 1988", then outputted. This is what I have so far.

// Prompts user for birthday
temp = JOptionPane.showInputDialog("Enter your birthday(mm/dd/yyyy): ");
// Formats the given date
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
temp2 = sdf.format(temp);

JOptionPane.showMessageDialog(null, "You were born on: " + temp);

I tried that out, and got an error because I'm using the .format function with a string. I do not know how to convert my "temp" string that the user inputs into a date, having it formatted, and then have it converted back into a string so I can output it yet again.

Am I using the wrong code? Or can I make this work? Any suggestions are MUCH appreciated ,I've been working on this for a few days now. 

Thank you all in advance!


----------



## Chicon (Jul 29, 2004)

Hi r3drock3t88,

This should solve your problem ! 


```
[SIZE=2]
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

public class ShowDate {
    
    public static void main(String[] args) {
       String temp = JOptionPane.showInputDialog("Enter your birthday(mm/dd/yyyy): ");
       // I'm assuming the user's entered date as being valid
       int month = Integer.parseInt(temp.substring(0,2));
       int day = Integer.parseInt(temp.substring(3,5));
       int year = Integer.parseInt(temp.substring(6));
       // I invoke an instance of Calendar in order to get a Date object
       // (Remark : the first month of the year = 0)
       Calendar cal = Calendar.getInstance();
       cal.set(year, month - 1, day);
       Date date = cal.getTime();
       // Transformation of the date
       SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
       JOptionPane.showMessageDialog(null, "You were born on: " + sdf.format(date));       
    }    
    
}
[/SIZE]
```


----------



## r3drock3t88 (Jan 13, 2007)

Hi Chicon! Thanks for the quick response, Unfortunately, this is for a class that I am in and we never learned about that just yet. My teacher my get a little fishy as to how i managed that one. I believe the class he wants us to use is java.util.date class. any other suggestions? Thanks again for the help! Its much appreciated


----------



## JimmySeal (Sep 25, 2007)

Never used these classes, but from looking at the Javadocs, I would think you need to:

Create another instance of SimpleDateFormat for the input format (mm/dd/yyyy).
Date foo = sdf2.parse(temp);
temp2 = sdf.format(foo);


----------



## r3drock3t88 (Jan 13, 2007)

Hmm, the sdf.parse(temp); keeps giving me an error: "Unhandled exception type ParseException" Any idea as to why im getting this error? :-/ What a bummer.


----------



## JimmySeal (Sep 25, 2007)

parse() throws an exception. You have to contain it in a try{}catch{} structure

try{
Date foo = sdf2.parse(temp);
temp2 = sdf.format(foo);
}
catch(Exception e){
System.err.println("Exception: " + e);
}


----------



## r3drock3t88 (Jan 13, 2007)

Ah, jimmy you're a genius, that got rid of my parse error, but now I get this error when I run the program. 

"Exception: java.text.ParseException: Unparseable date: 1988-09-22".

Here is my new code that I am using...

temp = JOptionPane.showInputDialog("Enter your birthday(yyyy-MM-dd): ");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy, MM, dd");
// Formats the given date
SimpleDateFormat sdf2 = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
try{
Date bday = sdf.parse(temp);
temp2 = sdf2.format(bday);
JOptionPane.showMessageDialog(null, "You were born on: " + temp2);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"Exception: " + e);
}

Any ideas why? Thanks again! This is a huge help.


----------



## Chicon (Jul 29, 2004)

I've tested this one and it works !


```
[SIZE=2]
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;

public class ShowDate {
    
    public static void main(String[] args) {
       String temp = JOptionPane.showInputDialog("Enter your birthday ( mm-dd-yyyy ) : ");

       SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
        Date date;
        try {
            date = sdf.parse(temp);
            sdf = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
            JOptionPane.showMessageDialog(null, "You were born on " + sdf.format(date));
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
    }    
}
[/SIZE]
```


----------



## r3drock3t88 (Jan 13, 2007)

Wow, it's amazing that I forgot one little thing and the entire program didn't run. All I missed was I put commas instead of "-" in the first date format. Thanks a lot ! You all were so much help. Finally I can finish this thing up !!


----------

