3

From web services, receiving time as 23:30:00. I want to convert it to the format of 12 hours. I want output something like 11:30 PM (after converting 23:30:00).

Chaudhary
  • 93
  • 1
  • 1
  • 10

9 Answers9

7

Use SimpleDateFormat to convert from One Time/Date Format to Another.

Log.v("12HRS Time", getFormatedDateTime("23:30:00", "HH:mm:ss", "hh:mm a"))

public static String getFormatedDateTime(String dateStr, String strReadFormat, String strWriteFormat) {

    String formattedDate = dateStr;

    DateFormat readFormat = new SimpleDateFormat(strReadFormat, Locale.getDefault());
    DateFormat writeFormat = new SimpleDateFormat(strWriteFormat, Locale.getDefault());

    Date date = null;

    try {
        date = readFormat.parse(dateStr);
    } catch (ParseException e) {
    }

    if (date != null) {
        formattedDate = writeFormat.format(date);
    }

    return formattedDate;
}

For SimpleDateFormat Reference:- https://developer.android.com/reference/java/text/SimpleDateFormat.html

Yugesh
  • 4,030
  • 9
  • 57
  • 97
3

Easiest way to get it by using date pattern - h:mm a, where

h - Hour in am/pm (1-12)
m - Minute in hour
a - Am/pm marker

Code snippet :

DateFormat dateFormat = new SimpleDateFormat("hh:mm a");

for more information see this link

Community
  • 1
  • 1
Zaki Pathan
  • 1,762
  • 1
  • 13
  • 26
2

Just use SimpleDateFormat

like this.....

public String GetTimeWithAMPMFromTime(String dt) {
        try {
            SimpleDateFormat inFormat = new SimpleDateFormat("HH:mm:ss");
            Date date = inFormat.parse(dt);
            SimpleDateFormat outFormat = new SimpleDateFormat("hh:mm a");
            String goal = outFormat.format(date);
            return goal;
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }

call the method...

String YOUR_TIME = GetTimeWithAMPMFromTime(WEB_SERVICE_TIME);
Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
1

Try SimpleDateFormat

Example

Date dateToFormat = new Date(someDate);
SimpleDateFormat dateFormatExpression = new SimpleDateFormat("hh:mm a");
String formattedDate = dateFormatExpression.format(dateToFormat);
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
1

I got answer just doing like this , i have tested it (y)

 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) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
mahmoud zaher
  • 544
  • 6
  • 9
0

Try this.

DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Awais Rafique
  • 466
  • 6
  • 17
0
android.text.format.DateFormat myDate = new android.text.format.DateFormat();
myDate.format("hh:mm a", new java.util.Date());

DateFormat

meks285
  • 29
  • 5
0
public static void convert(String time) throws Exception {
       try {       
           SimpleDateFormat t24 = new SimpleDateFormat("HH:mm");
           SimpleDateFormat t12 = new SimpleDateFormat("hh:mm a");
           Date date = t24.parse(time);
           System.out.println(t12.format(date));
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
0

One Line Code

String time = null;
try{
  time = new SimpleDateFormat("HH:mm").format(new SimpleDateFormat("HH:mm:ss").parse("your_time");
}catch{
}

in your_time you have to give input which you want to convert.

Ravi Mariya
  • 1,210
  • 15
  • 19
Omi
  • 1
  • 2
  • Thanks for wanting to contribute. It doesn’t seem to give the output asked for and also doesn’t seem to add anything that isn’t already in the other answers? – Ole V.V. Jun 21 '18 at 10:25
  • Furthermore I consider it poor advice to teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class as the first option and without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jun 21 '18 at 10:25
  • thanks for your suggestion @OleV.V. but i think is better solution for whom who are familiar with this class . – Omi Jun 21 '18 at 12:25