2014-03-12 58 views
0

我就像在这个问题上的两天。这是从这个问题的延续:convert string to joda datetime gets stuck如何将日期转换为DateTime或LocalDate Joda

我想执行一个简单的任务,采取SQL SERVER ISO 8601字符串并将其转换为DateTime或LocalDate。我尝试了一切,但是当涉及到使用调试器进行的实际转换时,程序光标只是用于思考而不会返回,即程序停留在此处。

我真的很绝望,我甚至试图将该字符串转换为日期(它工作!),然后将该日期转换为DateTime或LocalDate。所有的结果都一样,在转换线上,程序看起来像停留在一些无限循环(并且风扇开始疯狂地工作)。

这是我这么简单的功能:

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); 
*/ } 

这是我的字符串:2014-01-01T00:00:00

也:

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

任何想法?

+0

你想说'新的LocalDate(tempDate);'让你的电脑永远忙于工作,等于具体的java.util.Date作为输入吗? –

回答

1
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; 
} 
+0

谢谢。转换为日期的作品。但我需要它在DateTime或LocalDate中,并且这个卡住了程序。 – dsb

+0

public static String getDate(Date date,String dateFormat){ \t \t DateFormat formatter = new SimpleDateFormat(dateFormat); \t \t return formatter.format(date); \t} –

+0

是你需要的吗?你有日期对象,你可以做任何你想做的事情 –

相关问题