Question

I accept the user input date in yyyy/mm/dd format. When I try to convert using SimpleDateFormat it shows "null";

my code for conversion is:

SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/mm/dd"); 
java.util.Date dateOfBirth = dateofbirthFormat.parse(birthdate);

why is it showing null?

Was it helpful?

Solution

MM not mm, MM is for months, mm is for minutes

    SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/MM/dd"); 
    try {
        java.util.Date dateOfBirth = dateofbirthFormat.parse("1986/12/11");
        System.out.println(dateOfBirth);
    } catch (ParseException e) {
    }

OTHER TIPS

By yyyy/mm/dd I guest you mean years/Months/days. So you may try using
yyyy/MM/dd (note upcase use of MM).

SimpleDateFormat dateofbirthFormat = new SimpleDateFormat("yyyy/MM/dd"); java.util.Date dateOfBirth = dateofbirthFormat.parse(birthdate); .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top