1

How can I format a Date String to pattern dd-MMM-yyyy and return the value as a date.?

I've used SimpleDateFormat("dd-MMM-yyyy").parse(date) but the return value is like Sun Oct 07 00:00:00 GMT+06:00 2018 but I need it like 07-Oct-2018

Any easy way to format and return as a date to dd-MMM-yyyy pattern.?

  • Look at the first output of [this](https://kodejava.org/how-do-i-format-a-date-into-ddmmyyyy/) example. The other examples are for different kinds of formatting. – miguelarc Nov 06 '18 at 16:28
  • 1
    `SimpleDateFormat("dd-MMM-yyyy").format(date)` – m0skit0 Nov 06 '18 at 16:29
  • @miguelarc, @m0skit0 They are returning `String` not `Date` I need the return type as `Date` – Mehedi Hasan Chonchol Nov 06 '18 at 16:31
  • Date obj does not have a format. You get a formatted date only when you convert it to string. – a0x2 Nov 06 '18 at 16:35
  • @AmEénÁhsAn Ok. Why I'm getting two different type of date format like `Mon Oct 01 00:00:00 BDT 2018` and `Sun Oct 07 00:00:00 GMT+06:00 2018` – Mehedi Hasan Chonchol Nov 06 '18 at 16:37
  • 1
    You are mixing things. The output that you are seeing is the full structure representation of the class [Date](https://docs.oracle.com/javase/7/docs/api/java/util/Date.html). You can't simply convert the output of a class ot what you desire. However, you can use the `Date.get()` method to build the string that you desire. You can do something like `String date = Date.get(Calendar.DAY_OF_MONTH).toString() + Date.get(Calendar.MONTH).toString() + Date.get(Calendar.YEAR).toString()` – miguelarc Nov 06 '18 at 16:38
  • Possibly (more precisely) duplicate of [want current date and time in “dd/MM/yyyy HH:mm:ss.SS” format](https://stackoverflow.com/questions/8745297/want-current-date-and-time-in-dd-mm-yyyy-hhmmss-ss-format) – Ole V.V. Nov 07 '18 at 17:25

1 Answers1

0

You need to use DateFormat.parse(String), that will return a new Date object. For more information DateFormat