-1

I am trying to get current date from system in a format like 27-mar-2018 in Android Application. I have written some for this but it is not working as it is returning date like "27-012-2018". I have tried like dd-mon-yyyy also but it is throwing error saying this format is not supported. What is the correct pattern i should use to get current date in desirable format.

Thanks in advance!

My Code:

public class MainActivity extends AppCompatActivity {

RelativeLayout rldj;
RelativeLayout rlh;
RelativeLayout rlau;
RelativeLayout rldb;
RelativeLayout rlht;
RelativeLayout rlth;
RelativeLayout rltoi;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rldj = findViewById(R.id.rldj);
    rlh = findViewById(R.id.rlh);
    rlau = findViewById(R.id.rlau);
    rldb = findViewById(R.id.rldb);
    rlht = findViewById(R.id.rlht);
    rlth = findViewById(R.id.rlth);
    rltoi = findViewById(R.id.rltoi);


    rlh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, getDate(), Toast.LENGTH_SHORT).show();
        }
    });

}
private String getDate() {
    DateFormat dateFormat = new SimpleDateFormat("dd-mmm-yyyy");
    Date date = new Date();
    return dateFormat.format(date);
}
}
Shivam Tiwari
  • 550
  • 1
  • 7
  • 22

6 Answers6

2

Try this, it will provide exactly what you want

private String getDate() {
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    Date date = new Date();
    return dateFormat.format(date).toLowerCase();
}
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
1

Instead of dd-mmm-yyyy use dd-MMM-yyyy And Replace dateFormat.format(date) to dateFormat.format(date).toUpperCase()

Saurabh Vadhva
  • 511
  • 5
  • 12
1

make change to date format change below method...

private String getDate() {
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    Date date = new Date();
    return dateFormat.format(date);
}
1

use below method

private String getDate()
{


    android.text.format.DateFormat df = new android.text.format.DateFormat();
   return (String) df.format("yyyy-MMM-dd", new java.util.Date());


}
ashish
  • 307
  • 4
  • 15
1

For date converting you can use below code :

 public static String dateFormat(String dateStr) {
            String inputFormat="dd-MM-yyyy"
            String outputFormat="dd MMM yyyy"
            String finalDate = "";
            SimpleDateFormat inputSDF = new SimpleDateFormat(inputFormat);
            SimpleDateFormat outputSDF = new SimpleDateFormat(outputFormat);
            try {
                Date date = inputSDF.parse(dateStr);
                finalDate = outputSDF.format(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            Log.v("ffiinalDDateeEE", "finalDate: " + finalDate);


            return finalDate;
        }
Hkh
  • 357
  • 1
  • 10
0

Using dd-MMM-2018 worked for me but it still not returning date as i wanted it is returning like 27-Mar-2018 and I wanted it as 27-mar-2018. I have made some changes based on your suggestions and now I am getting it perfectly(27-mar-2018). Thankyou all for your help. Here is the updated code:

private String getDate() {
    DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
    Date date = new Date();
    return dateFormat.format(date).toLowerCase();
}
Shivam Tiwari
  • 550
  • 1
  • 7
  • 22