Android将字符串转换为日期

时间:2015-10-07 11:49:23

标签: android

我是以字符串格式从JSON Response获取日期,但是现在我希望将该日期格式化为日期格式。如何将此 strActiondate:= 9/28/2015 6:52:41 PM 转换为日期,以便我可以插入到Sq-lite数据库中。

我试过这种方式

strActiondate = jobjVessels.getString("actionDate");                

Log.e("strActiondate ", " = " + strActiondate);
try{
    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    Date netDate = (new Date(Long.parseLong(strActiondate)));
    Log.e("netDate "," = "+netDate);

}
catch(Exception ex){
    ex.printStackTrace();
}

我希望日期格式相同= 9/28/2015 6:52:41 PM

2 个答案:

答案 0 :(得分:1)

DateFormatMM/dd/yyyy对此日期无效:9/28/2015 6:52:41 PM

你需要一个匹配此字符串的好SimpleDateFormat

a   Am/pm marker        Text    PM
H   Hour in day (0-23)  Number  0
m   Minute in hour      Number  30
s   Second in minute    Number  55

所以这个:

String strActiondate = "9/28/2015 6:52:41 PM"; 
DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
Date netDate = sdf.parse(strActiondate);
System.out.println(netDate);

将输出:

Mon Sep 28 06:52:41 CEST 2015

在您的代码中:

try{
    DateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a");
    Date netDate = sdf.parse(strActiondate);       
    Log.d("netDate "," = "+netDate);  // log.e is an error... so better d or i
}

答案 1 :(得分:0)

使用此代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" id="cbAllYears" text="All Years" runat="server" />All
<input type="checkbox" Text="Jan" Value="1" class="month" />Jan
<input type="checkbox" Text="Feb" Value="2" class="month" />Feb
<input type="checkbox" Text="Mar" Value="3" class="month" />Mar
<input type="checkbox" Text="Apr" Value="4" class="month" />Apr
<input type="checkbox" Text="May" Value="5" class="month" />May
<input type="checkbox" Text="Jun" Value="6" class="month" />Jun
<input type="checkbox" Text="Jul" Value="7" class="month" />Jul
<input type="checkbox" Text="Aug" Value="8" class="month" />Aug
<input type="checkbox" Text="Sep" Value="9" class="month" />Sep
<input type="checkbox" Text="Oct" Value="10" class="month" />Oct
<input type="checkbox" Text="Nov" Value="12" class="month" />Nov
<input type="checkbox" Text="Dec" Value="12" class="month" />Dec