How to format date and time in Android?

前端 未结 24 1766
面向向阳花
面向向阳花 2020-11-22 00:51

How to format correctly according to the device configuration date and time when having a year, month, day, hour and minute?

24条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 01:04

    Back to 2016, When I want to customize the format (not according to the device configuration, as you ask...) I usually use the string resource file:

    in strings.xml:

    %1$td/%1$tm/%1$tY
    

    In Activity:

    Log.d(TAG, "my custom date format: "+getString(R.string.myDateFormat, new Date()));
    

    This is also useful with the release of the new Date Binding Library.

    So I can have something like this in layout file:

    
    

    And in java class:

        MovieDetailViewModel vm = new MovieDetailViewModel();
        vm.setReleaseDate(new Date());
    

提交回复
热议问题