-2

I have a java util Date object and I need to write it in the format "dd-MMM-yyyy", but not as a String ( it can be sql or util Date, it does not matter ) and I do not understand what I am doing wrong because instead of "Nov" for month, I get "11". The right output is given only when I format to String...

Can anyone help me ? This is what I tried so far and the reason I do not want a String is because I need to use this date as a parameter to run a database query and the field type in sql is Date.

        Date currentDate = new Date(Calendar.getInstance().getTimeInMillis());  output value: Thu Nov 14 17:21:44 EET 2019
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
        String formattedDate = simpleDateFormat.format(currentDate);    output value: 14-Nov-2019
        java.util.Date date = simpleDateFormat.parse(formattedDate);    output value: Thu Nov 14 00:00:00 EET 2019
        java.sql.Date sqlStartDate = new java.sql.Date(date.getTime()); output value: 2019-11-14
Maria1995
  • 355
  • 3
  • 9
  • 1
    `java.util.Date` or `java.sql.Date` objects do not represent formatted dates but simply dates that can be formatted to strings if you apply a format style to them. A *formatted* date is a string. – forpas Nov 14 '19 at 15:31
  • 1
    A Date is just a long integer, a count of milliseconds since the epoch. It doesn't "have" any format until you convert it to a string. It is the default Date.toString() method that is giving you the format with "11" as the month. –  Nov 14 '19 at 15:33
  • and is there any way I could get that "Nov" instead of "11" ? – Maria1995 Nov 14 '19 at 15:34
  • See the ```SimpleDateFormat``` class, which is for formatting a Date into a String. –  Nov 14 '19 at 15:39
  • The 3d line of your code returns what you expect. – forpas Nov 14 '19 at 15:50
  • Is this not other way around to duplicate @sotirios Delimanolis – Deadpool Nov 14 '19 at 16:12
  • @Deadpool Maybe I misunderstood. I'm going off another-dave's comment that clarifies that a Date doesn't have a format. Louis' answer in the duplicate explains that. – Sotirios Delimanolis Nov 14 '19 at 16:15
  • @Deadpool Did you mean this question should be used as a duplicate to close that one? – Sotirios Delimanolis Nov 14 '19 at 16:15
  • sorry, I mean if i understand OP want to convert `util.Date` to `LocalDate` something like this @SotiriosDelimanolis https://stackoverflow.com/questions/21242110/convert-java-util-date-to-java-time-localdate But the above duplicate shows how to convert date string to `util.Date` – Deadpool Nov 14 '19 at 16:17
  • @Deadpool Yeah, we can add that, but I think their misunderstanding is about the nature of these date classes, ie. they do not have formats. – Sotirios Delimanolis Nov 14 '19 at 16:22
  • @Maria, given the comments above, please clarify your question. – Sotirios Delimanolis Nov 14 '19 at 16:23
  • 1
    You i agree with this `the nature of these date classes, ie. they do not have formats.` @SotiriosDelimanolis – Deadpool Nov 14 '19 at 16:28
  • I recommend you don’t use any of the `Date` classes, `Calendar` nor `SimpleDateFormat`. Those classes are poorly designed and long outdated, `SimpleDateFormat` in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 14 '19 at 17:33

1 Answers1

0

The question is confused, you can use a common Date source i.e. Calendar and then use it as you need. If you need to set a parameter I think you should set it via preparedstatement like this:

Calendar c=Calendar.getInstance();
java.sql.Date sqlDate=new java.sql.Date(c.getTimeInMillis());
preparedStatement.setDate(1, sqlDate);

if you want to see the formatted String you can for sure use it with simpledateformat.

java.util.Date utilDate = c.getTime();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate = simpleDateFormat.format(utilDate);
AndreaTaroni86
  • 535
  • 9
  • 24