-2

I have this code I would like to know why it returns correct date

Festival f= (Festival) festival.get(0);
Date d=f.getSDate();


System.out.println(d.getYear());
System.out.println(d.getMonth());
System.out.println(d.getDate());


SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); // define your format.
String formattedDate = df.format(d);
System.out.println("New Date To Show::"+formattedDate);

the output is this

2019
2
27
New Date To Show::27/03/3919
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Ahmed
  • 7
  • 5
  • a formatter doesn't set a date, it merely shows the one you already have in another way. My first recommendation: don't use the Date class anymore, there are far better. – Stultuske Mar 28 '19 at 10:29
  • 1
    It's because the month (d.getMonth()) starts from zero and 2 is actually 3 – assaf.gov Mar 28 '19 at 10:29
  • 3
    I [cannot reproduce your problem](https://rextester.com/GYAH95097). Please post a reproducible issue. – Tim Biegeleisen Mar 28 '19 at 10:30
  • 1
    Months are `0` based in the `Date` class, and years are `1900` based. You seem to be creating the date incorrectly. – TiiJ7 Mar 28 '19 at 10:30
  • can you post getSDate() in Festival class ? – Mustafa Çil Mar 28 '19 at 10:31
  • 1
    As already stated: the month is 0 based, so both your outputs are correct. But the classes you use are deprecated, so you should switch to new alternatives instead: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#getMonth-- – Stultuske Mar 28 '19 at 10:31
  • 1
    side note: not use Date is outdated instead use new `java time api` – Akash Shah Mar 28 '19 at 10:46
  • 1
    A good article to check the java.time api https://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html – VSB Mar 28 '19 at 11:03
  • Related (only opposite): [Why does Java's Date.getYear() return 111 instead of 2011?](https://stackoverflow.com/questions/7215621/why-does-javas-date-getyear-return-111-instead-of-2011) – Ole V.V. Mar 28 '19 at 11:17
  • 2
    Not only is the `Date` class long outdated, the `getXx` methods are officially deprecated. Don’t use `Date`, and even if you do, *certainly* avoid the deprecated methods. Additionally it seems you’ve got a day in the wrong year from `f.getSDate()`. I recommend you use `LocalDate` and `DateTimeFormatter` from java.time and forget about `Date` and `SimpleDateFormat`. I promise, it will clear all confusion. – Ole V.V. Mar 28 '19 at 11:21
  • Thanks, @VinodSinghBist, for the good link. Ahmed, if you haven’t given up on `Date` by now, here’s a good blogpost on that class: [All about java.util.Date](https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/). – Ole V.V. Mar 28 '19 at 11:24

2 Answers2

2

Try to use this :

   Date date=new Date(); 
   // you can assign your return from the function here



    LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    System.out.println(localDate.getMonthValue());
    System.out.println(localDate.getDayOfMonth());
    System.out.println(localDate.getYear());


    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    String formattedDate = df.format(date);
    System.out.println("New Date To Show::"+formattedDate);
VSB
  • 317
  • 2
  • 10
  • Further details at: https://stackoverflow.com/questions/7182996/java-get-month-integer-from-date – VSB Mar 28 '19 at 11:03
  • On one hand it doesn’t solve the problem of `f.getSDate()` returning a date in year 3919. On the other hand we haven’t got any information that may help us help the asker solve the mentioned problem (except by changing the method’s return type to `LocalDate`). I’d additionally take the step and format the `LocalDate` using a `DateTimeFormatter`. – Ole V.V. Mar 28 '19 at 12:40
  • 1
    f.getSDate() returns a Date type object ( as per the question shows). you can replace the same on the code I shared and carry on with the required operations. – VSB Mar 28 '19 at 12:44
  • it still returns the year 3919 and changes the month value. :( – Ahmed Mar 29 '19 at 10:16
  • It *must* change the month value (otherwise it would be an error). March is 2 in `Date` and 3 in `LocalDate`, so it’s changed from 2 to 3. Your problem is you’re getting the wrong `Date` from `f.getSDate()`. @Ahmed – Ole V.V. Mar 29 '19 at 14:33
0

It's because the month (d.getMonth()) starts from zero and 2 is actually 3

assaf.gov
  • 517
  • 6
  • 19