简体   繁体   中英

How to Format Date Time in Android

I have an android application that returns an Entry date Formatted like this 2014-08-26T16:23:30.803 I need it to display 08/16/2014 04:23 pm

Here is my code

items.add(new ListViewItem()
{{                      
Ticket =  json_data.optString("TicketID");
Desc = json_data.getJSONObject("Location").optString("LocationName"); 
Status = json_data.optString("Status_Desc");
Poster = json_data.optString("Lastposter");                         
Time = json_data.optString("Entrydate");
}});
public static void main(String args[]) throws ParseException  {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        SimpleDateFormat sdp = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
        System.out.println(sdp.format(sdf.parse("2014-08-26T16:23:30.803")));
    }

prints:

08/26/2014 04:23 PM
SimpleDateFormat sd = new SimpleDateFormat("MM'/'dd'/'yyyy hh:mm a");
sd.format(new Date());
SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");//dd/MM/yyyy
Date date= new Date("2009-09-22 16:47:08");
String strDate = sdfDate.format(date);

use this

SimpleDateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy HH:mm a");
String dateInString = " 2014-08-26T16:23:30.803";
try {

    Date date = formatter.parse(dateInString);
    System.out.println(date);
    System.out.println(formatter.format(date));

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

it helps you

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