Convert time value to format “hh:mm Am/Pm” using Android
Try this..
Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);
I got answer just doing like this.
startTime = "2013-02-27 21:06:30";
StringTokenizer tk = new StringTokenizer(startTime);
String date = tk.nextToken();
String time = tk.nextToken();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
Date dt;
try {
dt = sdf.parse(time);
System.out.println("Time Display: " + sdfs.format(dt)); // <-- I got result here
} catch (ParseException e) {
e.printStackTrace();
}
First you don't need to use StringTokenizer
to get the string time. Just pass your startTime
like this:
// Get date from string
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormatter.parse(startTime);
// Get time from date
SimpleDateFormat timeFormatter = new SimpleDateFormat("h:mm a");
String displayValue = timeFormatter.format(date);
// Done!