Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change date time format in Android?

Tags:

android

I am displaying the date and time in Android with this format:
2013-06-18 12:41:24

How can I change it to the following format?
18-jun-2013 12:41 pm

like image 679
Anil M H Avatar asked Jun 25 '13 06:06

Anil M H


People also ask

How do I convert a date to a specific format?

Convert date to different format with Format CellsSelect the dates you want to convert, right click to select Format Cells from context menu. 2. In the Format Cells dialog, under Number tab, select Date from Category list, and then select one format you want to convert to from the right section.


1 Answers

Here is working code

public String parseDateToddMMyyyy(String time) {     String inputPattern = "yyyy-MM-dd HH:mm:ss";     String outputPattern = "dd-MMM-yyyy h:mm a";     SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);     SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);      Date date = null;     String str = null;      try {         date = inputFormat.parse(time);         str = outputFormat.format(date);     } catch (ParseException e) {         e.printStackTrace();     }     return str; } 

Documentation: SimpleDateFormat | Android Developers

like image 70
deadfish Avatar answered Sep 22 '22 07:09

deadfish