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);
}
}