0

Facebook returns me a String like:

06/15/1992

How can I convert this string date into a Java Date object?

I tried the following but it returns "Wed Jan 15 00:06:00 UTC 1992" which is not the correct date.

 String t = "06/15/1992";
 SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy");
 Date test = dateFormat.parse(t);
Michael
  • 32,527
  • 49
  • 210
  • 370
  • Read the javadoc for `SimpleDateFormat` again... – fge Jun 15 '13 at 15:03
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 18 '17 at 19:19

5 Answers5

3

You need to deduct 1 from the month field , as months are indexed from 0. So "06/15/1992" should be "05/15/1992" to get the correct result. according to the Javadoc:

A month is represented by an integer from 0 to 11; 0 is January, 1 is February, and so forth; thus 11 is December.

And use SimpleDateFormat with correct format string : M is for month , m is for minute.

String t = "05/15/1992";
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date test = dateFormat.parse(t);
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
2

Use this:

 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

instead of

 SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy");

As the SimpleDateFormat describes the meaning of letters as

M : Month in year
m : Minute in hour

For more about SimpleDateFormat follow this: http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

Should be

"MM/dd/yyyy"

m is for minute.

Devolus
  • 21,661
  • 13
  • 66
  • 113
0

tl;dr

LocalDate.parse( 
    "06/15/1992" , 
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) 
)

Details

The other Answers are correct. Formatting patterns are case-sensitive. You used mm rather than MM.

java.time

The modern approach is with the java.time classes that supplant the troublesome old date-time classes.

The LocalDate class represents a date-only value without time-of-day and without time zone.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
LocalDate ld = LocalDate.parse( "06/15/1992" , f );

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

First, you need MM (month), not mm (minutes).

Facebook could return dates in a different format. Here is the graph documentation:

The person's birthday. This is a fixed format string, like MM/DD/YYYY. However, people can control who can see the year they were born separately from the month and day so this string can be only the year (YYYY) or the month + day (MM/DD)

https://developers.facebook.com/docs/graph-api/reference/user

Your code should check for which date format Facebook provided you.

    String[] dateParts = birthday.split(birthday, "/");
    try {
        switch (dateParts.length) {
            case 2:
                // A fully formed date: MM/dd/yyyy
                Date dob = new SimpleDateFormat("MM/dd/yyyy, Locale.US).parse(birthday));
                break;
            case 1:
                // Only a day and month: mm/dd.
                String month = dateParts[0];
                String day = dateParts[1];
                // Do something intelligent with the information.
                break;
            case 0:
                // Only a year: yyyy
                // Do something
            default:
                // Unexpected format. Give up!
                return null;
        }
    } catch (ParseException ex) {
        Log.e(LOG_TAG, "getAge: Couldn't parse FB date: " + birthday, ex);
        return null;
    }
Thomas Fischer
  • 1,394
  • 2
  • 12
  • 25