Sunday, July 12, 2020

How to get the Date with Month Name and Year In Java or Android

How to get the Date with Month Name and Year In Java or Android


In Java And Android we can get proper Date with Month name.
The following method return Date like "2 Jan 2020".
You just need to pass miliSeconds as parameter.


public static String getDateWithMonthName(long miliseconds)
{
         GregorianCalendar gc=new GregorianCalendar();
         gc.setTimeInMillis(miliseconds);

         int day=gc.get(Calendar.DAY_OF_MONTH);
         int month=gc.get(Calendar.MONTH)+1;
         int year=gc.get(Calendar.YEAR);
         String date=day+" "+getMonth(month)+" "+year;
         return date;
}


public static String getMonth(int month)
{
 switch(month)
 {
      case 1:
         return "Jan";
      case 2:
         return "Feb";
      case 3:
         return "Mar";

      case 4:
         return "April";

      case 5:
         return "May";

      case 6:
         return "June";

      case 7:
         return "July";

      case 8:
         return "Aug";

      case 9:
         return "Sep";

      case 10:
         return "Oct";

      case 11:
         return "Nov";

      case 12:
         return "Dec";

   }
 return "";
}

No comments:

Post a Comment