How do I change date time format in Android?

前端 未结 10 1803
故里飘歌
故里飘歌 2020-11-29 07:19

I am displaying the date and time in Android with this format:
2013-06-18 12:41:24

How can I change it to the following format?
18-j

相关标签:
10条回答
  • 2020-11-29 07:44

    First Create a Calendar object using your Date object. Then build a String using date, year, month and etc you need. then you can use it.

    You can get data using get() method in Calendar class.

    0 讨论(0)
  • 2020-11-29 07:47

    You have to set like this

     Date date=new Date("Your date "); 
    SimpleDateFormat formatter5=new SimpleDateFormat("required format");
    String formats1 = formatter5.format(date);
    System.out.println(formats1);
    
    0 讨论(0)
  • set answer like this to set date format

     Date date=new Date("Your date "); 
    SimpleDateFormat formatter5=new SimpleDateFormat("required format");
    
    0 讨论(0)
  • 2020-11-29 07:52

    Use this code like below:

    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");
    String date = formatter.format(Date.parse("Your date string"));
    

    Hope it will help you.

    0 讨论(0)
  • 2020-11-29 07:59
    public class MainActivity extends AppCompatActivity {
        private Date oneWayTripDate;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            String date ="2017-05-05 13:58:50 ";
            SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat output = new SimpleDateFormat("MMMM dd,yyyy @hh:mm:ss aa");
            try {
                oneWayTripDate = input.parse(date);  // parse input
            } catch (ParseException e) {
                e.printStackTrace();
           }
           Log.e("===============","======currentData======"+output.format(oneWayTripDate);
        }
    }
    
    0 讨论(0)
  • 2020-11-29 08:00

    We can use this format for convert the date. Pass the date as parameter

      public String formatdate(String fdate)
    {
        String datetime=null;
        DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy");
        SimpleDateFormat d= new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date convertedDate = inputFormat.parse(fdate);
            datetime = d.format(convertedDate);
    
        }catch (ParseException e)
        {
    
        }
        return  datetime;
    
    
    }
    
    0 讨论(0)
提交回复
热议问题