Change date or time format in Android

Droid By Me
3 min readAug 30, 2018

--

Each date or time has its own format and we can change its format by class SimpleDateFormat. SimpleDateFormat is a concrete class for formatting and parsing dates in local-sensitive manner. Its allow for formatting (date to text), parsing (text to date) and normalization.

Let’s get started for change format for date and time.

First create a method for parsing date, having parameters input date in string, format of that date and in which format you want that date.

public static String parseDate(String inputDateString, SimpleDateFormat inputDateFormat, SimpleDateFormat outputDateFormat) {
Date date = null;
String outputDateString = null;
try {
date = inputDateFormat.parse(inputDateString);
outputDateString = outputDateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return outputDateString;
}

Then call parseDate() method using required params. Let me show example. If you want to convert date of 2018–08–31 to Fri 31-Aug-2018 then do as below:

public static final SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

public static final SimpleDateFormat EEEddMMMyyyy = new SimpleDateFormat("EEE dd-MMM-yyyy", Locale.US);
private String outputDateStr = "";outputDateStr = parseDate("2018-08-31", ymdFormat, EEEddMMMyyyy);
Log.i("output_string", outputDateStr);

Output in logcat : Fri 31-Aug-2018

As same above, you can change time format from 14:30 to 2:30 PM:

public SimpleDateFormat HHmmFormat = new SimpleDateFormat("HH:mm", Locale.US);

public SimpleDateFormat hhmmampmFormat = new SimpleDateFormat("hh:mm a", Locale.US);
private String outputDateStr = "";outputDateStr = parseDate("14:30", HHmmFormat, hhmmampmFormat);
Log.i("output_string", outputDateStr);

Output in logcat : 2:30 PM

That’s it. Here are some more useful date format with example in comments as which date or time they will return when you use.

public SimpleDateFormat hhmmFormat = new SimpleDateFormat("hh:mm", Locale.US); // 01:55

public SimpleDateFormat HHmmFormat = new SimpleDateFormat("HH:mm", Locale.US); // 13:55

public SimpleDateFormat HHmmssFormat = new SimpleDateFormat("HH:mm:ss", Locale.US); // 13:55:22

public SimpleDateFormat ampmFormat = new SimpleDateFormat("a", Locale.US); // PM

public SimpleDateFormat ddMMyyyyFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.US); // 31-08-2018

public SimpleDateFormat ddMMyyyHHmmFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.US); // 31-08-2018 13:55

public SimpleDateFormat yyyyMMddFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US); // 2018-08-31

public SimpleDateFormat ddMMMyyyyhhmmampmFormat = new SimpleDateFormat("dd MMM yyyy, hh:mm a", Locale.US); // 31 Aug 2018, 01:55 PM

public SimpleDateFormat yyyMMddHHmmssFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); // 2018-08-31 01:55:22

public SimpleDateFormat EEEddMMMyyyyFormat = new SimpleDateFormat("EEE dd-MMM-yyyy", Locale.US); // Fri 31-Aug-2018

public SimpleDateFormat EEEddFormat = new SimpleDateFormat("EEE dd", Locale.US); // Fri 31

public SimpleDateFormat MMMMyyyyFormat = new SimpleDateFormat("MMMM yyyy", Locale.US); // August 2018

public SimpleDateFormat zoneFormat = new SimpleDateFormat("Z", Locale.US); // +0530

public SimpleDateFormat zoneShortFormat = new SimpleDateFormat("z", Locale.US); // GMT+05:30

public SimpleDateFormat zoneLongFormat = new SimpleDateFormat("zzzz", Locale.US); // India Standard Time

public SimpleDateFormat MMMFormat = new SimpleDateFormat("MMM", Locale.US); // Aug

public SimpleDateFormat MMFormat = new SimpleDateFormat("MM", Locale.US); // 08

public SimpleDateFormat ddFormat = new SimpleDateFormat("dd", Locale.US); // 31

public SimpleDateFormat EEEEFormat = new SimpleDateFormat("EEEE", Locale.US); // Friday

public SimpleDateFormat MMMddFormat = new SimpleDateFormat("MMM dd", Locale.US); // Aug 31

public SimpleDateFormat EEEEddMMMyyyyFormat = new SimpleDateFormat("EEEE dd MMM, yyyy", Locale.US); // Friday 31 Aug, 2018

public SimpleDateFormat ddMMMMyyyyFormat = new SimpleDateFormat("dd MMMM, yyyy", Locale.US); // 31 August, 2018

public SimpleDateFormat hhmmampmFormat = new SimpleDateFormat("hh:mm a", Locale.US); // 01:55 PM

public SimpleDateFormat MMddyyyyFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US); // 08/31/2018

--

--

Droid By Me
Droid By Me

Written by Droid By Me

By profession and passion : Android Developer

Responses (1)