Monday 11 June 2018

android - how to get date format like yyyy-mm-dd am in java




I need to get date format like:



"yyyy-mm-dd am"


or




"yyyy-mm-dd pm"


In Java for Android



I do not need current time, only yyyy-mm-dd plus am/pm



how to make it directly ?



i use the method below to get current date:




    private static String getDate(){
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
return sdf.format(c.getTime());
}

Answer



You should use these below lines of code...There is only one problem with the SimpleDateFormat is that when ever you change the device language to another language than SimpleDateFormat's string also changes with the language which can create problem in yours application..Therefore i suggest you if you do not want to change the date string according the application language changes than Use below concept which become helpful for you Or Just simply use SimpleDateFormat for not changing the application language to another langauge.




     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.YEAR) + "-" +
(ci.get(Calendar.MONTH) + 1) + "-" +
ci.get(Calendar.DAY_OF_MONTH)+" "+AM_PM;

System.out.println("time=========================================="+CiDateTime);


Output:-




 time==========================================2015-5-20 PM

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...