[ACCEPTED]-How to convert string to time in android?-time

Accepted answer
Score: 24

Read SimpleDateFormat documentation (parse&format 1 methods and pattern syntax).

http://developer.android.com/reference/java/text/SimpleDateFormat.html

String time = "530pm";
SimpleDateFormat dateFormat = new SimpleDateFormat("hmmaa"); 
SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm aa");
try {
    Date date = dateFormat.parse(time);

    String out = dateFormat2.format(date);
    Log.e("Time", out);
} catch (ParseException e) {
}
Score: 6
final String NEW_FORMAT = " hh:mm";
final String OLD_FORMAT = "hh:mm:ss";
 String formatDate;
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
            Date d = null;
            try {
                d = sdf.parse(Value);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            sdf.applyPattern(NEW_FORMAT);
            formatDate=sdf.format(d);

0

More Related questions