How to format date in Android in dd/mm/yy 00:00AM

2019-09-07 19:55发布

问题:

How to format my date in Android. I have String which gets date in below format

String format = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

Output

25 Nov 2013 17:39:00

I want this to be converted in below format

dd/mm/yy 5:39PM

I am using below code to get Date. Also how to get that format into String ?

回答1:

Using SimpleDateFormat class you can do it.

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy hh:mm");
String format = sdf.format(Calendar.getInstance().getTime());


回答2:

please try follwing answer

      SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
     java.util.Date date = null;
     try 
    {
    date = form.parse("2011-03-27T09:39:01.607");
   }
  catch (ParseException e) 
  {

   e.printStackTrace();
   }
   SimpleDateFormat postFormater = new SimpleDateFormat("MMMMM dd, yyyy");
   String newDateStr = postFormater.format(date);


回答3:

I got it, This has to go like this

 Date today = new Date();
                SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy");
                String date = DATE_FORMAT.format(today);

                DATE_FORMAT = new SimpleDateFormat("dd/MM/yy  hh:mm:ss aa");
                date = DATE_FORMAT.format(today);