2016-03-15 65 views
0

如何從json響應中解析日期並在textview中設置。更改日期和時間來自Json響應的時間合成器

我的JSON響應:

{ 「CREATEDATE」: 「2016年3月14日4時00分01秒」}

我想在我的TextView什麼:

4:00 PM 03/14

我的列表視圖適配器:

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.HashMap; 

public class ListViewAdapter extends BaseAdapter { 

    Context ctx; 
    ArrayList<HashMap<String, String>> arraylist; 
    LayoutInflater inflater; 

    TextView tvPlaceName, tvTime; 
    String longitude, latitude; 

    String out; 

    ListViewAdapter(Context ctx, ArrayList<HashMap<String, String>> arraylist) { 

     this.ctx = ctx; 
     this.arraylist = arraylist; 
    } 


    @Override 
    public int getCount() { 
     return arraylist.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return arraylist.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     inflater = (LayoutInflater) ctx 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View itemView = inflater.inflate(R.layout.listitem, parent, false); 

     tvPlaceName = (TextView) itemView.findViewById(R.id.tvPlaceName); 
     tvTime = (TextView) itemView.findViewById(R.id.tvTime); 



     // Log.d("uname", arraylist.get(position).get("Username")); 
     tvPlaceName.setText(arraylist.get(position).get("alert_message")); 
     tvTime.setText(arraylist.get(position).get("createdate")); 


     longitude = arraylist.get(position).get("longitude"); 
     latitude = arraylist.get(position).get("latitude"); 


     return itemView; 
    } 



    private String convertTime(String time) { 

     SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm"); 
     SimpleDateFormat format1 = new SimpleDateFormat("hh:mm aa MM-dd"); 
     java.util.Date date = null; 

     try { 
      date = format.parse(time); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     String convertedDate = format1.format(date); 

     return convertedDate; 
    } 

} 

我已經提取了我的回覆,但我不知道如何以特定的方式合成日期。

建議是熱忱歡迎

+1

的可能的複製[解析日期字符串到一些Java對象](http://stackoverflow.com/questions/8854780/parse-date-string-to-some-java-object) – Aizen

+0

檢查我編輯的問題..如何格式化日期時間和設置textview – tiger

回答

3

做這樣

tvTime.setText(convertTime(arraylist.get(position).get("createdate"))); 

使用下面的代碼

private String convertTime(String time) { 

     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     SimpleDateFormat format1 = new SimpleDateFormat("hh:mm aa MM-dd"); 
     java.util.Date date = null; 

     try { 
      date = format.parse(time); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

     String convertedDate = format1.format(date); 

     return convertedDate; 
    } 
+0

我有listview ..我能夠獲取響應..但如何在我的textview中設置? – tiger

+0

只需在您的適配器中添加上述功能...並將名稱設置爲文本時使用此功能 –

+0

可以編輯您的答案並顯示我嗎?PLZ – tiger

1

使用下面的代碼

下面的代碼返回日曆對象

public static Calendar StringDateConverter(String date) { 
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    df.setTimeZone(TimeZone.getTimeZone("UTC")); 
    try { 
     return dateToCalendar(df.parse(date.toUpperCase())); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

private static Calendar dateToCalendar(Date date) { 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(date); 
     return calendar; 
    } 

使用此代碼日曆事件轉換爲字符串

public static String ConvertDateToDate(Calendar calendar) { 
    DateFormat df = new SimpleDateFormat("HH:mmaa MM/dd", Locale.getDefault()); 
    return df.format(calendar.getTime()); 
} 
+0

dateToCalendar =無法解析方法.. – tiger

+0

@tiger請檢查更新的內容 –

+0

我有listview ..我能夠獲取響應..但如何在我的textview中設置? – tiger

1
private String getFormate(String date) throws ParseException { 
    Date d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH).parse(date); 
    Log.d("Date", String.valueOf(d)); 
    Calendar cal = Calendar.getInstance(); 
    cal.setTime(d); 
    String monthName = new SimpleDateFormat("hh:mmaa MM/yyyy").format(cal.getTime()); 
    return monthName; 
} 

,並以此來讓你的格式

 try { 
      String myFormate = getFormate(d); 
      Log.d("date", myFormate); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 

這裏dString d = "2016-03-14 04:00:01";

1

試試這個:

SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm:ss a MM/dd"); 
SimpleDateFormat hh_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

try { 
    Date dat1 =hh_mm_ss.parse(arraylist.get(position).get("createdate")); 
        String out = dateFormat2.format(dat1); 
        tvTime.setText(out); 
       } catch (ParseException e) { 
        e.printStackTrace(); 
       } 

它將轉換您的日期格式,給你喜歡的輸出「時間上午4時00分01秒03/14」

+0

我有listview ..我能夠獲取響應..但如何設置在我的textview? – tiger

+0

好,如果你使用recyclerview,你可以做這樣的事情holder.tv_neme.setText(out); –

+0

@tiger:這是你想要的精靈 –

1

精確的結果

SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mma MM/dd"); 
      SimpleDateFormat hh_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

      try { 
           Date dat1 = hh_mm_ss.parse("2016-03-14 04:00:01"); 
           String out = dateFormat2.format(dat1); 
           System.out.println(out); 
          } catch (ParseException e) { 
           e.printStackTrace(); 
          } 
1
public String convertDateFormat() { 
     String formatedDate =""; 
     SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

     SimpleDateFormat outputFormat = new SimpleDateFormat("HH:mmaa MM/dd"); 

     Date date = null; 
     try { 
      date = inputFormat.parse(parseJsonAndGetCreateDateString());    
      formatedDate = outputFormat.format(date); 

     } catch (java.text.ParseException e) { 
      e.printStackTrace(); 
     } 


     return formatedDate; 
    } 

public String parseJsonAndGetCreateDateString(){ 
String response = "{\"createdate\":\"2016-03-14 04:00:01\"}"; 
String createDate = ""; 
try{ 

JSONObject obj = new JSONObject(response); 
createDate = obj.getString("createdate"); 

}catch(Exception e){ 
e.printStackTrace(); 
} 
return createDate; 
}