50

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";
Android learner
  • 1,871
  • 4
  • 24
  • 36
  • //Capitalize AM-PM// fun String.capitalizeTime(): String { return this.replace("am", "AM").replace("pm", "PM") } – Stefan Nov 16 '22 at 11:35

10 Answers10

72

Try this..

Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);
Murugan
  • 1,441
  • 1
  • 12
  • 22
38

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();
}
shadygoneinsane
  • 2,226
  • 1
  • 24
  • 47
Android learner
  • 1,871
  • 4
  • 24
  • 36
22

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!
maohieng
  • 1,646
  • 1
  • 19
  • 26
15

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

dharmendra
  • 7,835
  • 5
  • 38
  • 71
10
Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);

output 2.00 a.m.

 Date dt = new Date(date1);
 SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
 String time1 = sdf.format(dt);

output 2.00 AM

Aakash
  • 5,181
  • 5
  • 20
  • 37
saravanan
  • 1,082
  • 1
  • 15
  • 30
8

I recommend using a DateFormat, like SimpleDateFormat

try {
    String timeLong = "2013-02-27 06:06:30";
    String timeShort = "16:06 AM";
    SimpleDateFormat formatLong = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    SimpleDateFormat formatShort = new SimpleDateFormat("hh:mm aa", Locale.US);
    Log.v("out", formatShort.format(formatLong.parse(timeLong)));
    Log.v("out", formatShort.format(formatShort.parse(timeShort)));
} catch (ParseException e) {
    e.printStackTrace();
}

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.

Sam
  • 86,580
  • 20
  • 181
  • 179
6

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.

Calendar cal = Calendar.getInstance();
DateFormat outputFormat = new SimpleDateFormat("KK:mm a");
String formattedTime = outputFormat.format(cal.getTime());

2 Note: The following formatter can be used to display the same time in 24-hour format (21:30).

new SimpleDateFormat("HH:mm");

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!

    String startTime = "2013-02-27 21:06:30";
    String[] parts = startTime.split(" ");

    DateFormat outputFormat = new SimpleDateFormat("KK:mm a");
    SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm:ss");


    try {
        Date dt = parseFormat.parse(parts[1]);
        System.out.println(outputFormat.format(dt));
    } catch(ParseException exc) {
        exc.printStackTrace();
    }
Hamzeen Hameem
  • 2,360
  • 1
  • 27
  • 28
4

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 :)

Monir Zzaman
  • 459
  • 4
  • 7
  • It seems to repeat what is in the accepted answer (which BTW isn’t fully correct)? Also in 2019 consider not using `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Instead you may add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Mar 20 '19 at 13:51
2

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();
    }
Nikhil
  • 911
  • 15
  • 28
1

On Android you also have DateFormat

yoah
  • 7,180
  • 2
  • 30
  • 30