简体   繁体   English

如何将日期转换为DateTime或LocalDate Joda

[英]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 这是该问题的延续: 将字符串转换为joda datetime卡住

I am trying to perform one simple task, to take SQL SERVER ISO 8601 string and convert it to DateTime or LocalDate. 我正在尝试执行一个简单的任务,将SQL SERVER ISO 8601字符串转换为DateTime或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, ie, 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. 我非常绝望,我什至试图将String转换为Date(有效!),然后将Date转换为DateTime或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 这是我的字符串: 2014-01-01T00:00:00

also: 也:

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

Any ideas? 有任何想法吗?

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM