Search code examples
javaandroiddatetimesimpledateformatdate-format

Android SimpleDateFormat returning dots in am and pm


I am using the following code to get the current time as a String in Android.

public String getTime(){

   Date date = new Date();
   String strTimeFormat = "hh:mm a";
   DateFormat dateFormat = new SimpleDateFormat(strTimeFormat);
   String formattedTime= dateFormat.format(date);
   return formattedTime;
}

The response on few mobiles I get properly as expected but on few mobiles, I am getting dots in a.m. or p.m.

for example 08:02 p .m .

this is how I am getting the data while I am supposed to get it like 08:02 pm

my question is how to resolve this issue?


Solution

  • Try this code. Hope it works !

    private String getTimeStamp() {
                DateFormat df = new SimpleDateFormat("hh:mm:ss aaa", Locale.ENGLISH);
                return df.format(Calendar.getInstance().getTime());
            }