-1

I want to convert the date format to string, but the string output is not coming

  DateFormat currentDate = new SimpleDateFormat("dd MM yyyy ");
  String currDateString = String.valueOf(currentDate);
  System.out.println(currDateString);
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
A.G
  • 489
  • 1
  • 6
  • 17
  • The DateFormat is stateless (unlike, say, the Calendar) – Maurice Perry Jan 20 '17 at 08:09
  • 3
    @MauricePerry : stateless? I don;t think so, there is lot of state in SimpleDateFormat (that's why it is not thread-safe) – rkosegi Jan 20 '17 at 08:12
  • @rkosegi SimpleDateFormat is a poor implementation of DateFormat. – Maurice Perry Jan 20 '17 at 08:14
  • 1
    @MauricePerry : yes, so why you said that abstract class (DateFormat) is stateless without knowing behavior of actual implementation? That is like saying that some interface is stateless ... – rkosegi Jan 20 '17 at 08:16
  • @rkosegi alright, let's instead say that it SHOULD be stateless – Maurice Perry Jan 20 '17 at 08:18
  • @MauricePerry It is impossible to implement the abstract class `DateFormat` in a stateless way because there are abstract setters with return type void (if you want any sensible implementation beyond no-op). – Meno Hochschild Jan 20 '17 at 11:08
  • @MenoHochschild that's not the point – Maurice Perry Jan 20 '17 at 13:16
  • @MauricePerry That is the point because finally no concrete class which is really used is abstract. `DateFormat` itself is just an abstract facade. And you will always have a concrete implementation at runtime i.e. a concrete subclass which will be stateful (and I explained in my previous comment why such a subclass cannot be stateless). – Meno Hochschild Jan 20 '17 at 15:11
  • @MenoHochschild OK, stateless is not the right word. What I meant is that it has no current date (unlike, say, the Calendar). – Maurice Perry Jan 30 '17 at 06:53

1 Answers1

8

To format current date you need to create a instance of Date which represents current date and then format it using SimpleDateFormat to string.

DateFormat dateFormat = new SimpleDateFormat("dd MM yyyy");
String currDateString = dateFormat.format(new Date());
System.out.println(currDateString);
ares
  • 4,283
  • 6
  • 32
  • 63
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Jan 21 '17 at 22:47
  • 24 01 2017 i need to convert it to 24th January 2017 ? – A.G Jan 24 '17 at 12:25
  • `new SimpleDateFormat("dd MM yyyy")` is the input format `new SimpleDateFomat('dd MMMM yyyy')` is output. You should refer to the [docs](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) – ares Jan 24 '17 at 13:01