How to transform currentTimeMillis to a readable date format ?

How to transform currentTimeMillis to a readable date format ?

Asked on December 28, 2018 in Java.
Add Comment


  • 4 Answer(s)

    Try this code, this should works.

    long yourmilliseconds = System.currentTimeMillis();
    SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
    Date resultdate = new Date(yourmilliseconds);
    System.out.println(sdf.format(resultdate));
    
    Answered on December 28, 2018.
    Add Comment

    Use this code in android,

    DateFormat.getInstance().format(currentTimeMillis);
    

    The Date function is negleted, so instead of use that we have to use Dateformat class.

       DateFormat.getDateInstance().format(new Date(0));
       DateFormat.getDateTimeInstance().format(new Date(0));
       DateFormat.getTimeInstance().format(new Date(0));
    

    Yields,

    Dec 31, 1969
    Dec 31, 1969 4:00:00 PM
    4:00:00 PM 12:00:00 AM
    
    Answered on December 28, 2018.
    Add Comment
    DateFormat.getInstance().format(currentTimeMillis);

    Moreover, Date is deprecated, so use DateFormat class.

       DateFormat.getDateInstance().format(new Date(0));  
       DateFormat.getDateTimeInstance().format(new Date(0));  
       DateFormat.getTimeInstance().format(new Date(0));  

    The above three lines will give:

    Dec 31, 1969  
    Dec 31, 1969 4:00:00 PM  
    4:00:00 PM  12:00:00 AM
    Answered on January 16, 2019.
    Add Comment

    the program to convert milliseconds to a Date Format in Java:

    // Java program to convert milliseconds
    // to a Date format
     
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    public class SectoDate {
        public static void main(String args[])
        {
     
            // milliseconds
            long miliSec = 3010;
     
            // Creating date format
            DateFormat simple = new SimpleDateFormat("dd MMM yyyy HH:mm:ss:SSS Z");
     
            // Creating date from milliseconds
            // using Date() constructor
            Date result = new Date(miliSec);
     
            // Formatting Date according to the
            // given format
            System.out.println(simple.format(result));
        }
    }
    Answered on January 16, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.