1

I have a little issue in Java Calendar setting. The problem is I have this code and it returns "1980.09.12.".

I can change the year and month to whatever I want, but can't change the day.

public class printableBeosztas extends HttpServlet {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd.");
    Calendar calHetfo;

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        setDates();
        response.getWriter().append(sdf.format(calHetfo.getTime());
    }

    public void setDates() {
        calHetfo = Calendar.getInstance();
        calHetfo.set(Calendar.MONTH, 8);
        calHetfo.set(Calendar.YEAR, 1980);
        calHetfo.set(Calendar.DAY_OF_MONTH, 3);
    }
}

Any idea?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Andor
  • 155
  • 1
  • 1
  • 12
  • 1
    I have used the setDates() it works as expected. Where you have been using this? And how you used SimpleDateFormat in your code? – Gaurav Srivastav Jun 13 '18 at 17:27
  • It is a servlet i want to use to list all the shift data from a date to a date. It is usually from monday to friday and i generate the other date from this calHetfo with day_of_week. Everything is working fine except this day_of_month. I use sdf just to transform Calendar to dateformat. – Andor Jun 13 '18 at 17:40
  • [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash May 15 '21 at 17:04

1 Answers1

4

It can be more easier if you are using java.time API of Java 8+, where you can use LocalDate.of and set whatever you want :

LocalDate ld = LocalDate.of(1980, Month.SEPTEMBER, 12);

To format the date you can use :

String format = ld.format(DateTimeFormatter.ofPattern("yyyy.MM.dd."));
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • Thanks, it's working. But still don't know why my code didn't work – Andor Jun 13 '18 at 17:49
  • I don't see any problem in your code did it gives you an error? – Youcef LAIDANI Jun 13 '18 at 17:50
  • No error, just not the expected result. I could change everything except the day – Andor Jun 13 '18 at 17:54
  • 1
    I try your code and it work fine with me check the [idone demo](https://ideone.com/Z2iDC4) – Youcef LAIDANI Jun 13 '18 at 18:02
  • Thanks, I checked. But believe me, it didn't work (used the same technic somewhere else with success), and i couldn't figure out why... Thanks your time, effort and solution! – Andor Jun 13 '18 at 18:35
  • You are welcome @Andor we are here to help each other ;) – Youcef LAIDANI Jun 13 '18 at 18:36
  • 1
    @Andor Whatever the mysterious cause of your issue, it is moot as those old date-time classes really are a wretched mess that should be avoided. Best to forget about them, not waste anymore time trying to figure them out, and move on. The *java.time* classes replace them entirely, incl `java.util.Date`, `java.util.Calendar`, `java.sql.Timestamp`, `java.sql.Date`, etc. As of Java 8 and JDBC 4.2, there is no need to ever touch the legacy classes again. For interfacing with old code not yet updated to *java.time*, you can convert back-and-forth via new conversion methods added to the old classes. – Basil Bourque Jun 14 '18 at 04:25
  • @Andor - *Thanks, it's working. But still don't know why my code didn't work*. -It may be because of `Locale`. [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573). – Arvind Kumar Avinash May 15 '21 at 17:05