【问题标题】:Changing Date and time Time formate from Json Response从 Json 响应更改日期和时间时间格式
【发布时间】:2016-03-15 13:34:36
【问题描述】:

如何从 json 响应中解析日期并在 textview 中设置。

我的 Json 响应:

{"createdate":"2016-03-14 04:00:01"}

我在 Textview 中想要的内容:

14 年 3 月下午 4:00

我的列表视图适配器:

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

}

我已获取我的回复,但我不知道如何以特定方式格式化日期。

欢迎提出建议

【问题讨论】:

标签: android json date date-format


【解决方案1】:

这样做

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

【讨论】:

  • 我有 listview.. 我能够获取响应.. 但是如何在我的 textview 中设置它?
  • 只需在您的适配器中添加上述功能...在将名称设置为文本时使用此功能
  • else 告诉我你是如何将 name 设置为 textview 的,我会告诉你如何使用上面的函数。
  • tvTime.setText(arraylist.get(position).get("createdate"));
  • createdate 是日期时间响应的名称
【解决方案2】:

使用以下代码

以下代码返回日历对象

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

【讨论】:

  • 我有 listview.. 我能够获取响应.. 但是如何在我的 textview 中设置它?
【解决方案3】:
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;
}

【讨论】:

    【解决方案4】:
    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";

    【讨论】:

      【解决方案5】:

      精确你的结果

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

      【讨论】:

        【解决方案6】:

        试试这个:

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

        它将转换您的日期格式并为您提供类似“时间 04:00:01 AM 03/14”的输出

        【讨论】:

        • 我有 listview.. 我能够获取响应.. 但是如何在我的 textview 中设置它?
        • 好吧,如果您使用的是 recyclerview,您可以执行以下操作 holder.tv_neme.setText(out);
        • @tiger : 这正是你想要的
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多