dsb
dsb

Reputation: 2527

How to convert Date to DateTime or LocalDate Joda

I'm like two days on this problem. This is in continuation from this question: convert string to joda datetime gets stuck

I am trying to perform one simple task, to take SQL SERVER ISO 8601 string and convert it to DateTime or LocalDate. I tried everything, but when it comes to the actual conversion with the debugger the program cursor just goes for thinking and never comes back, i.e., the program is stuck there.

I'm so desperate that I even tried to convert that String to Date (which works!) and then convert that Date to DateTime or LocalDate. All had the same result, on the line of the conversion the program looks like stuck in some endless loop (and the fan starts working like crazy).

Here is my so simple function:

    public static LocalDate SQLServerIso8601StringToLocalDate(String date) {

        LocalDate dateToReturn = null;
        DateFormat df = new SimpleDateFormat(CONSTS_APP_GENERAL.SQL_SERVER_DATE_FORMAT);
        try {
            Date tempDate = (Date) df.parse(date);
            DateTime dt = new DateTime(tempDate);
            dateToReturn = new LocalDate(tempDate);

        } catch (ParseException e) {
            // strToReturn = strSqlDate;
        }
        return dateToReturn;

/*      
        date = date.replace('T', ' ');
        return SimpleIso8601StringToDateTime(date);
*/
        //return LocalDate.parse(date, DateTimeFormat.forPattern(CONSTS_APP_GENERAL.SQL_SERVER_DATE_FORMAT));

/*      DateTimeFormatter df = DateTimeFormat.forPattern(CONSTS_APP_GENERAL.SQL_SERVER_DATE_FORMAT_WITH_TZ);
        return df.parseDateTime(date);
*/  }

This is my String: 2014-01-01T00:00:00

also:

public static final String SQL_SERVER_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";

Any ideas?

Upvotes: 0

Views: 1064

Answers (1)

Ran Adler
Ran Adler

Reputation: 3707

public static Date getDateFromString(String format, String dateStr) {
    DateFormat formatter = new SimpleDateFormat(format);
    Date date = null;
    try {
        date = (Date) formatter.parse(dateStr);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return date;
}

Upvotes: 1

Related Questions