How can I get Date in MM/DD/YY format from Timestamp

后端 未结 7 1198
暗喜
暗喜 2020-12-16 11:27

I want to get the Date in MM/DD/YY format from a timestamp.

I have used the below method but it does not gives proper output

final Calen         


        
7条回答
  •  一向
    一向 (楼主)
    2020-12-16 12:08

    What's wrong:

    Calendar.DAY_OF_MONTH, Calendar.MONTH etc are static constants used to access those particular fields. (They will remain constant, no matter what setTimeInMillis you provide.)


    How to solve it:

    To get those particular fields you can use the .get(int field)-method, like this:

    Log.d("Month--",""+cal.get(Calendar.MONTH));
    

    As others have pointed out there are more convenient methods for formatting a date for logging. You could use for instance the SimpleDateFormat, or, as I usually do when logging, a format-string and String.format(formatStr, Calendar.getInstance()).

提交回复
热议问题