tags:

views:

58

answers:

1

Hi all, I'm taking a class in Java and I need to convert a string to a date format (dd/MM/yyyy). I have been using the SimpleDateFormat to format my input, but it is showing the time, timezone and day of the week the date falls. Here is a snippet of my code:

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 
Date date = new Date(); 
do{ 
    y = JOptionPane.showInputDialog(null, 
                                    "Please enter the vehicle's registration date",
                                    "Year?", 
                                    JOptionPane.QUESTION_MESSAGE); 
    try { 
        date = df.parse(y); 
        check = true; 
    } 
    catch (ParseException e) { 
        check = false; 
    } 
} 
while (check == false); 
return date;

Anyone know how I can keep the format to just the date (e.g. 12/3/2000)? Thanks

+2  A: 

Just format it accordingly using SimpleDateFormat#format().

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

The java.util.Date object contains information about both date and time. If you want only the date part in a human representable format, then you need to format it into a String. Invoking Date#toString() as you would get when doing System.out.println(dateObject) would only return the date in format dow mon dd hh:mm:ss zzz yyyy. Also see the linked javadoc.

BalusC