How to format date and time in Android?

前端 未结 24 1309
面向向阳花
面向向阳花 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:26

    I use it like this:

    public class DateUtils {
        static DateUtils instance;
        private final DateFormat dateFormat;
        private final DateFormat timeFormat;
    
        private DateUtils() {
            dateFormat = android.text.format.DateFormat.getDateFormat(MainApplication.context);
            timeFormat = android.text.format.DateFormat.getTimeFormat(MainApplication.context);
        }
    
        public static DateUtils getInstance() {
            if (instance == null) {
                instance = new DateUtils();
            }
            return instance;
        }
    
        public synchronized static String formatDateTime(long timestamp) {
            long milliseconds = timestamp * 1000;
            Date dateTime = new Date(milliseconds);
            String date = getInstance().dateFormat.format(dateTime);
            String time = getInstance().timeFormat.format(dateTime);
            return date + " " + time;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:27

    Date format class work with cheat code to make date. Like

    1. M -> 7, MM -> 07, MMM -> Jul , MMMM -> July
    2. EEE -> Tue , EEEE -> Tuesday
    3. z -> EST , zzz -> EST , zzzz -> Eastern Standard Time

    You can check more cheats here.

    0 讨论(0)
  • 2020-11-22 01:27

    The android Time class provides 3 formatting methods http://developer.android.com/reference/android/text/format/Time.html

    This is how I did it:

    /**
    * This method will format the data from the android Time class (eg. myTime.setToNow())   into the format
    * Date: dd.mm.yy Time: hh.mm.ss
    */
    private String formatTime(String time)
    {
        String fullTime= "";
        String[] sa = new String[2];
    
        if(time.length()>1)
        {
            Time t = new Time(Time.getCurrentTimezone());
            t.parse(time);
            // or t.setToNow();
            String formattedTime = t.format("%d.%m.%Y %H.%M.%S");
            int x = 0;
    
            for(String s : formattedTime.split("\\s",2))
            {   
                System.out.println("Value = " + s);
                sa[x] = s;
                x++;
            }
            fullTime = "Date: " + sa[0] + " Time: " + sa[1];
        }
        else{
            fullTime = "No time data";
        }
        return fullTime;
    }
    

    I hope thats helpful :-)

    0 讨论(0)
  • 2020-11-22 01:29

    This code would return the current date and time:

    public String getCurrDate()
    {
        String dt;
        Date cal = Calendar.getInstance().getTime();
        dt = cal.toLocaleString();
        return dt;
    }
    
    0 讨论(0)
  • 2020-11-22 01:29

    Avoid j.u.Date

    The Java.util.Date and .Calendar and SimpleDateFormat in Java (and Android) are notoriously troublesome. Avoid them. They are so bad that Sun/Oracle gave up on them, supplanting them with the new java.time package in Java 8 (not in Android as of 2014). The new java.time was inspired by the Joda-Time library.

    Joda-Time

    Joda-Time does work in Android.

    Search StackOverflow for "Joda" to find many examples and much discussion.

    A tidbit of source code using Joda-Time 2.4.

    Standard format.

    String output = DateTime.now().toString(); 
    // Current date-time in user's default time zone with a String representation formatted to the ISO 8601 standard.
    

    Localized format.

    String output = DateTimeFormat.forStyle( "FF" ).print( DateTime.now() ); 
    // Full (long) format localized for this user's language and culture.
    
    0 讨论(0)
  • 2020-11-22 01:31

    In my opinion, android.text.format.DateFormat.getDateFormat(context) makes me confused because this method returns java.text.DateFormat rather than android.text.format.DateFormat - -".

    So, I use the fragment code as below to get the current date/time in my format.

    android.text.format.DateFormat df = new android.text.format.DateFormat();
    df.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());
    
    or
    
    android.text.format.DateFormat.format("yyyy-MM-dd hh:mm:ss a", new java.util.Date());
    

    In addition, you can use others formats. Follow DateFormat.

    0 讨论(0)
提交回复
热议问题