How can I get current date in Android?

前端 未结 26 1892
[愿得一人]
[愿得一人] 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:16

    This is the code i used:

                 Date date = new Date();  // to get the date
                 SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); // getting date in this format
                 String formattedDate = df.format(date.getTime());
                 text.setText(formattedDate);
    
    0 讨论(0)
  • 2020-11-27 11:21

    You can use following code to get a date in the format you want.

    String date = String.valueOf(android.text.format.DateFormat.format("dd-MM-yyyy", new java.util.Date()));
    
    0 讨论(0)
  • 2020-11-27 11:21

    This is the code I used:

    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);
    
    // Set current date into textview
    tvDisplayDate.setText(new StringBuilder()
        .append(month + 1).append("-") // Month is 0 based, add 1
        .append(day).append("-")
        .append(year).append("   Today is :" + thursday ) );
    
    // Set current date into datepicker
    dpResult.init(year, month, day, null);
    
    0 讨论(0)
  • 2020-11-27 11:23
     public String giveDate() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
        return sdf.format(cal.getTime());
     }
    
    0 讨论(0)
  • 2020-11-27 11:23

    In Kotlin

    https://www.programiz.com/kotlin-programming/examples/current-date-time

    fun main(args: Array<String>) {
    
    val current = LocalDateTime.now()
    
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
    val formatted = current.format(formatter)
    
    println("Current Date and Time is: $formatted")}
    
    0 讨论(0)
  • 2020-11-27 11:25
    SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
    String date = df.format(Calendar.getInstance().getTime());
    
    0 讨论(0)
提交回复
热议问题