I am getting date value from database like \"2013-02-27 06:06:30\" using StringTokenizer I will get time separately like below
String startTime = \"2013-02
Selected answer had an issue of showing wrong time. If your time is 12:30:00 it shows 12:30 AM instead 12:30 PM. The below code will help to overcome the issue.
SimpleDateFormat sdf = new SimpleDateFormat("KK:mm:ss");
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
Date dt1 = null;
try {
dt1 = sdf.parse("12:00:00");
Log.i("Time in Am Pm ", sdfs.format(dt1));
} catch (ParseException e) {
e.printStackTrace();
}
Try this..
Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);
Try this
String time = "22:35";
try {
SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
Date dateObj = sdf.parse(time);
System.out.println(dateObj);
System.out.println(new SimpleDateFormat("K:mm").format(dateObj));
} catch (final ParseException e) {
e.printStackTrace();
}
Trace out this link http://developer.android.com/reference/java/text/SimpleDateFormat.html
Just use the following pattern you will get your expected result:
try {
String time = "06:06:30";
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date dt = sdf.parse(time);
SimpleDateFormat sdfs = new SimpleDateFormat("hh:mm a");
String formatedTime = sdfs.format(dt);
Log.v("parseTime", formatedTime);
} catch (ParseException e) {
e.printStackTrace();
}
Happy coding :)