简体   繁体   中英

How to convert HH:mm:ss to hh:mm AM/PM in android

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

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

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

Try SimpleDateFormat

Example

Date dateToFormat = new Date(someDate);
SimpleDateFormat dateFormatExpression = new SimpleDateFormat("hh:mm a");
String formattedDate = dateFormatExpression.format(dateToFormat);

尝试这个。

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

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);
android.text.format.DateFormat myDate = new android.text.format.DateFormat();
myDate.format("hh:mm a", new java.util.Date());

DateFormat

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM