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-27 06:06:30";
StringTokenizer token = new StringTokenizer(startTime);
String date1 = token.nextToken();
String time1 = token.nextToken();
and in time1 I am getting the result 06:06:30,
Can I re-store it in another variable of type String as follows?
String displayValue = "06:06 AM";
And if time1 variable has the value of
String time = 16:00:00;
then it should be converted to:
String displayValue = "04:00 PM";
Try this..
1 Assuming you need to show the current time in the format 09:30 PM. This would be a fairly easy approach. Even if you don't require it for the current time, you should be able to use the below DateFormat for your requirement.
2 Note: The following formatter can be used to display the same time in 24-hour format (21:30).
3 However, if you want to construct the same format as in my first point, it is best to use the following code as Java discourages the use of StringTokenizer. You can read about it here, http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html
Hope this would help you, thanks!
On Android you also have DateFormat
I got answer just doing like this.
I recommend using a DateFormat, like SimpleDateFormat
I am tired today and I feel like I am missing something in this code so I might amend it later, but it does work and it doesn't (directly) call the deprecated Date class.
output 2.00 a.m.
output 2.00 AM