简体   繁体   中英

Convert MySQL DATETIME to Specific Format In Android TextView

I recently started learning java and how to build android apps. I'm currently working on an app which uses a CustomListAdapter to display MySQL data parsed via json into a ListView . The problem I am running into is how to change the way a TextView displays the DATETIME from my database. As it is now, it shows the complete YYYY-MM-DD HH:MM:SS format, and I'm actually just trying to display HH:MM A . I've seen other people ask similar questions and get responses to use the SimpleDateFormat class, however, I am not understanding how to fit into my code. due_time is the column from my database that is being parsed, which I also named the TextView id in my layout.

This was a snippet suggested by someone else to use:

SimpleDateFormat df1 = new SimpleDateFormat("YYYY-MM-DD");
SimpleDateFormat df2 = new SimpleDateFormat("DD-MM-YYYY");
try{
    Date d = df1.parse(DateString);
    System.out.println("new date format " + df2.format(d));
}catch(Exception e){
   e.printStackTrace();
}

If that would work, I'm assuming I would insert due_time in there somewhere, then plug the snippet into the getView method of my CustomListAdapter , shown below. Am I on the right track?

    @Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    TextView due_time = (TextView) convertView.findViewById(R.id.due_time);

    // getting order data for the row
    Order o = orderItems.get(position);

    // Due Time
    due_time.setText(o.getDueTime());

    return convertView;
}

Hi you can try this,

public static String convertFormat(String inputDate) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    Date date = null;
    try {
        date = simpleDateFormat.parse(inputDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }


    if (date == null) {
        return "";
    }

    SimpleDateFormat convetDateFormat = new SimpleDateFormat("hh:mm a");

    return convetDateFormat.format(date);
}

you can call this method from your getView method like this and set in a textView

due_time.setText(convertFormat(o.getDueTime()));

Kotlin Version:

fun getDisplayDateTime(dateTimePhp: String): String {
    try {
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss",     Locale.getDefault())
        val date = simpleDateFormat.parse(dateTimePhp)
        val convetDateFormat = SimpleDateFormat("dd-MM-yyyy hh:mm a", Locale.getDefault())
        return convetDateFormat.format(date)
    } catch (e: Exception) {
        return ""
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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