-1

How do you format correctly according to the device configuration a date and time when having year, month, day, hour and minute? for example I want to display 29 July, 2015, 10:30 Am, according to my time zone

abdul khan
  • 843
  • 2
  • 7
  • 24

3 Answers3

2

You can use this method to format the datetime... u can replace new java.util.Date() with any datetime variable...

 android.text.format.DateFormat df = new android.text.format.DateFormat();
 df.format("dd-MMM-yyyy hh:mm aa", new java.util.Date());
Rishad Appat
  • 1,786
  • 1
  • 15
  • 30
1
String strDateTime = "29 July, 2015, 10:30 Am";
SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM, yyyy, hh:mm a");
strDateTime = sdf.format(Calendar.getInstance().getTime());
holder.txtTime.setText(strDateTime);

Please integrate the above code, that works fine for me.

Any help, do let me know.

Parth Bhayani
  • 1,894
  • 3
  • 17
  • 35
0

Please Find the below solution

Calendar ci = Calendar.getInstance();
 String AM_PM;

  if(ci.get(Calendar.AM_PM)==0)
    {
         AM_PM ="AM";
    }
    else
    {
         AM_PM ="PM";
    }
    String CiDateTime = "" +  ci.get(Calendar.DAY_OF_MONTH)+" "+
                              (ci.get(Calendar.MONTH) + 1)+","+
                               ci.get(Calendar.YEAR) + "-" + 
       + "," +getCurrentTime()
       +" "+AM_PM;

Call the method

private String getCurrentTime()
{
  int hrsRight = 0;
  Calendar c = Calendar.getInstance();
  int hrs = c.get(Calendar.HOUR);
  int min = c.get(Calendar.MINUTE);
  if (hrs>12) 
  {
   hrsRight = hrs - 12;
  }
  else
 {
 hrsRight = hrs;
 }
  return String.valueOf(hrsRight)+":"+String.valueOf(min);
 }
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103