Dude
Dude

Reputation: 173

How to convert time in 12 hr format?

I'm getting call time through a cursor and want to display it in 12 hr format instead of 24 hr format.

Here is my code to get time from a cursor

String callDate = managedCursor.getString(dateIndex);
Date callDayTime = new Date(Long.valueOf(callDate));

and set this call day time to text view by

lastintreactionvalueTV.setText(callDayTime+"");

it's showing it like

Thu Jun 26 14:36:24 EDT 2014

What should I do to convert it into 12 hr format?

Upvotes: 0

Views: 106

Answers (2)

Sreejith SP
Sreejith SP

Reputation: 171

Use this,

 SimpleDateFormat read = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat write = new SimpleDateFormat("MMMM-dd-yy");
    String op = write.format(read.parse("2014-05-17 15:45:56"));

//

 SimpleDateFormat read = new SimpleDateFormat("input format");
    SimpleDateFormat write = new SimpleDateFormat("output format");
    String op = write.format(read.parse("your date as in input format"));

Upvotes: 0

Carlos Verdes
Carlos Verdes

Reputation: 3147

Use SimpleDateFormat to set the format that you need, for example:

    Date callDayTime  = new Date();
    DateFormat sdf= new SimpleDateFormat("EEE, MMM dd KK:mm:ss a z yyyy",new Locale("en"));
    System.out.println(sdf.format(callDayTime) );

This will output:

Thu, Jun 26 08:54:51 AM CEST 2014

Upvotes: 2

Related Questions