Convert time value to format “hh:mm Am/Pm” using Android

111,169

Solution 1

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();
}

Solution 2

Try this..

Date dt = new Date(date1);
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");
String time1 = sdf.format(dt);

Solution 3

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!

Solution 4

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

Solution 5

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

Share:
111,169

Related videos on Youtube

Android learner
Author by

Android learner

I'm a developer in Android and interested in exploring more into the Android World.I am here to learn/share knowledge which will help me to grow as a quality developer.

Updated on July 09, 2022

Comments

  • Android learner
    Android learner almost 2 years

    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
    Android learner about 11 years
    Thanks for replay. I tried but no use. Its showing java.lang.IllegalArgumentException.
  • android learner
    android learner over 9 years
    I was getting confusion hw i can convert 18:00:00 PM to 06:00:00 PM.. Thank you..By using "KK:mm:ss aa" i got result :)
  • Bhavik Mehta
    Bhavik Mehta almost 9 years
    This is the simplest solution. Thanks
  • Bhavik Mehta
    Bhavik Mehta almost 9 years
    This answer shows wrong time if your time is xxxx-xx-xx 12:30:00 it shows 12:30 AM instead 12:30 PM
  • Mehul Ranpara
    Mehul Ranpara over 8 years
    @BhavikMehta Yes, I am facing the same issue.
  • Bhavik Mehta
    Bhavik Mehta over 8 years
    @MehulRanpara : there are a lot of solutions already given on StackOverFlow, just search around
  • Multidots Solutions
    Multidots Solutions over 7 years
    Thank you. so much for help
  • pb123
    pb123 about 6 years
    Should be SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a",Locale.US);
  • Nikhil
    Nikhil over 5 years
    I also had the same issue as Bhavik Metha. See my answer for the sollution
  • Ole V.V.
    Ole V.V. over 5 years
    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 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.
    Ole V.V. over 5 years
    While the formatting is correct, your parsing isn’t. I tried with startTime = "2013-02-27 12:06:30"; and expected 12:06 PM. But I got Time Display: 12:06 AM.
  • KSR
    KSR over 4 years
    Excellent solution.Thanks.
  • Hissaan Ali
    Hissaan Ali over 4 years
    @androidlearner you might be using HH:mm:ss aa . Instead use hh:mm:ss aa for 12 hour AM-PM format