0

I want to show the current date in my application like this: Thu, May 2, 2013

I already have the following code to get the current date

Calendar c = Calendar.getInstance();

Time time = new Time();
time.set(c.get(Calendar.DAY_OF_MONTH), c.get(Calendar.MONTH),
             c.get(Calendar.YEAR));

How can I format this Time object to the string I need?

Serge S.
  • 4,855
  • 3
  • 42
  • 46
Robby Smet
  • 4,649
  • 8
  • 61
  • 104
  • Try using SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); For the detailed explanation of the values uses use [this link](http://download.java.net/jdk7/archive/b123/docs/api/java/util/Formatter.html) – Ashwini Bhangi May 02 '13 at 11:47
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Most of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Oct 01 '18 at 00:37

5 Answers5

4

This does what you want

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
String strDate = sdf.format(cal.getTime());
System.out.println("Current date in String Format: " + strDate);

Where strDate can be displayed in your textView or whatever

rcbevans
  • 7,101
  • 4
  • 30
  • 46
2

Maybe you can use it.

This example displays the names of the weekdays in short form with the help of DateFormatSymbols().getWeekdays() method of DateFormatSymbols class.

import java.text.*;
import java.util.*;
public class Main {
   public static void main(String[] args) {
      Date dt = new Date(1000000000000L);

      DateFormat[] dtformat = new DateFormat[6];
      dtformat[0] = DateFormat.getInstance();
      dtformat[1] = DateFormat.getDateInstance();
      dtformat[2] = DateFormat.getDateInstance(DateFormat.MEDIUM);
      dtformat[3] = DateFormat.getDateInstance(DateFormat.FULL);
      dtformat[4] = DateFormat.getDateInstance(DateFormat.LONG);
      dtformat[5] = DateFormat.getDateInstance(DateFormat.SHORT);

      for(DateFormat dateform : dtformat)
         System.out.println(dateform.format(dt));
  }
}

output:

9/9/01 7:16 AM
Sep 9, 2001
Sep 9, 2001
Sunday, September 9, 2001
September 9, 2001
9/9/01

Source

Castiblanco
  • 1,200
  • 4
  • 13
  • 32
1

Use this

SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM dd,yyyy");
String formattedDate = formatter.format(new Date(System.currentTimeMillis()));              
Log.e("formattedDate",formattedDate);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Arun C
  • 9,035
  • 2
  • 28
  • 42
0
SimpleDateFormat dateformat= new SimpleDateFormat("dd,MM,yyyy");

String strdate = dateformat.format(new Date(System.currentTimeMillis())); 

Oops, I'm a bit slow.

T.C.
  • 133,968
  • 17
  • 288
  • 421
0

I will suggest to use java.text.SimpleDateFormat instead.

    public static void main(String[] args) {
    Date date=new Date();
    String format = new SimpleDateFormat("EEE,MMM d,yyyy ").format(date);
    System.out.println(format);
}