I am getting date from calendar in UI in the format as dd/mm/yyyy. But i am unable to save it in that format as I need date value as 22-Jul-18. Is there any simple way I can get the data in jsp as 22-July-18 format directly. Though I can use enum and may be do some modifications in my coding to change it. But it will be very helpful if anyone can help me with this . Thanks in advance.
I am getting date from calendar in UI in the format as dd/mm/yyyy. But i want it in 12-Jul-18 format
Asked
Active
Viewed 588 times
0
-
[SimpleDateFormat](https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) should be helpful for the work – soufrk Jun 26 '18 at 07:35
-
1https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html – achAmháin Jun 26 '18 at 07:35
-
If you are saving to a database, use the database’s `date` type and avoid saving as a string. If you do need to save as a string (for example in a text file), it’s recommended to use the ISO 8601 standard format: `2018-07-22`. Of course, if your format cannot be changed, you need to go with it (and cross your fingers that you don’t get year 2000 problems or code that fails when run on a computer with a non-English locale). – Ole V.V. Jun 26 '18 at 08:08
-
You need to show the code you're working with. Preferably a Minimal, Complete and Verifiable example: https://stackoverflow.com/help/mcve – kumesana Jun 26 '18 at 08:09
-
I tried but it was giving error as Cannot format given Object as Date. I am using Oracle and in my table the data type is DATE. And in my entity class it is of String type. Also i tried to change the data type of my date field in my hibernate entity class as Date(java.util.Date) from String. But then I am getting error in my UI as Bad request amd no error in console. – Mani Jun 26 '18 at 08:13
-
`LocalDate.parse("22/07/2018", DateTimeFormatter.ofPattern("dd/MM/uuuu")).format(DateTimeFormatter.ofPattern("dd-MMM-uu", Locale.ROOT))` – Ole V.V. Jun 26 '18 at 08:40
1 Answers
1
You can use this:
String date = new SimpleDateFormat("dd-MMM-yy", Locale.US).format(new Date());
System.out.println(date);
Output:
26-Jun-18
PS: as @Ole V.V. already mentioned it is outdated approach to get it with SimpleDateFormat
. Instead you can use this sample:
import java.time.*;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yy");
String formatDateTime = LocalDateTime.now().format(formatter);
System.out.println(formatDateTime);
Output:
26-Jun-18

Andrei Suvorkov
- 5,559
- 5
- 22
- 48
-
Just out of curiosity, what is the Calendar for, rather than just new Date() ? – kumesana Jun 26 '18 at 07:51
-
Yes, you are right, now is more elegant, thank you for pointing it )) – Andrei Suvorkov Jun 26 '18 at 07:53
-
1Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jun 26 '18 at 08:05
-
2Come to think of it, sorry that I was nitpicking about creating a Date directly when there are more direct issues: The month is wanted in short form, so it should be 3 Ms instead of 4. And to represent the year you need lowercase ys. An uppercase Y represents "week year", that is to say, the year of the week of the specified date. Remember weeks are sequences of 7 days, and they tend to overlap between two years. One of these two years is the one that the week "belongs" to. Uppercase Y represents that year. It's incorrect. You need lowercase y, the year the current date is in. – kumesana Jun 26 '18 at 08:06
-
1@OleV.V. thank you for the comment, I have added also the example with `DateTimeFormatter` – Andrei Suvorkov Jun 26 '18 at 08:20
-
1@kumesana I have edited an answer, I have had MMMM beacuse of `Is there any simple way I can get the data in jsp as 22-July-18 format directly`. For YY and yy thank you very much! – Andrei Suvorkov Jun 26 '18 at 08:28
-
I am learning and trying to build a small application using JSP, Spring and hibernate. In this application I made a jsp page where I am getting event name, event date etc. So, in UI from calender I am selecting the date. – Mani Jun 26 '18 at 08:32
-
Thx. Since the asker’s date doesn’t have time of day `LocalDate` is a better choice than `LocalDateTime` (that the modern API has so relatively many types is both its strength and its challenge when learning to use it). – Ole V.V. Jun 26 '18 at 08:42
-
@Ole Thank you. It works But issue is it takes current local date and work fine. When I am selecting future date for the event and passing in a string. How can i do that? – Mani Jun 26 '18 at 09:01
-
@Mani I have added an original concerned with a date without time of day as you. The format is not the same — can you modify the solution to fit your situation? I particularly recommend the two answers using `java.time`: [this one](https://stackoverflow.com/a/20821770/5772882) and [this one](https://stackoverflow.com/a/45331724/5772882). To get the month as abbreviation you need three `M`s, `MMM` and you need to specify locale. – Ole V.V. Jun 26 '18 at 09:14
-
See also [my comment under your question](https://stackoverflow.com/questions/51037315/i-am-getting-date-from-calendar-in-ui-in-the-format-as-dd-mm-yyyy-but-i-want-it#comment89068358_51037315). – Ole V.V. Jun 26 '18 at 09:17
-
@Ole Thank you so much for your help. My code is working perfectly now. Also, thank you all other folks for all your suggestions. – Mani Jun 26 '18 at 14:35