How can I get current date in Android?

前端 未结 26 1919
[愿得一人]
[愿得一人] 2020-11-27 10:24

I wrote the following code

Date d = new Date();
CharSequence s  = DateFormat.format(\"MMMM d, yyyy \", d.getTime());

But is asking me para

相关标签:
26条回答
  • 2020-11-27 11:10

    A simple tweak to Paresh's solution:

    Date date = Calendar.getInstance().getTime();
    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    String formattedDate = df.format(date);
    
    0 讨论(0)
  • 2020-11-27 11:11

    This is nothing to do with android as it is java based so you could use

    private String getDateTime() { 
       DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
       Date date = new Date(); 
       return dateFormat.format(date); 
    }
    
    0 讨论(0)
  • 2020-11-27 11:12

    just one line code to get simple Date format :

    SimpleDateFormat.getDateInstance().format(Date())
    

    output : 18-May-2020

    SimpleDateFormat.getDateTimeInstance().format(Date())
    

    output : 18-May-2020 11:00:39 AM

    SimpleDateFormat.getTimeInstance().format(Date())
    

    output : 11:00:39 AM

    Hope this answer is enough to get this Date and Time Format ... :)

    0 讨论(0)
  • 2020-11-27 11:13

    try this,

    SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
    Date myDate = new Date();
    String filename = timeStampFormat.format(myDate);
    
    0 讨论(0)
  • 2020-11-27 11:15
    Calendar cal = Calendar.getInstance();      
    Calendar dt = Calendar.getInstance(); 
    dt.clear();
    dt.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DATE)); 
    return dt.getTime();        
    
    0 讨论(0)
  • 2020-11-27 11:16

    The below code displays the both time and date

    Calendar cal = Calendar.getInstance();
    cal.getTime().toString();
    
    0 讨论(0)
提交回复
热议问题