public class ApplTest { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { int year = 1988 , year2 = 1999; // Data For Date int month = 11 , month2 = 9; // Data For Date int day = 9 , day2 = 12; // Data For Date Date start_date = new Date(year,month,day); Date end_date = new Date(year2,month2,day2); Coupon coupon = new Coupon(); coupon.setAmount(0); coupon.setEndDate(end_date); coupon.setStartDate(start_date); coupon.setId(1234); coupon.setImage("electricity.bmp"); coupon.setMessage("Hello from coupon"); coupon.setPrice(32); coupon.setTitle("Only for couple"); coupon.setType(CouponType.CAMPING); new Validation().validate(coupon); } static class Validation { public boolean validate(Coupon coupon) throws NoSuchFieldException, IllegalAccessException { Field[] fields = coupon.getClass().getDeclaredFields(); for(Field field: fields){ Annotation[] annotations = field.getDeclaredAnnotations(); for(Annotation annotation:annotations){ System.out.println(annotation); } field.setAccessible(true); System.out.println(field); String name = field.getName(); System.out.println(field.get(coupon)); } return true; } } } 

Console output:

 private long beans.Coupon.id 1234 private java.lang.String beans.Coupon.title Only for couple private java.sql.Date beans.Coupon.startDate 3888-12-09 private java.sql.Date beans.Coupon.endDate 3899-10-12 private int beans.Coupon.amount 0 private java.lang.String beans.Coupon.message Hello from coupon private double beans.Coupon.price 32.0 private java.lang.String beans.Coupon.image electricity.bmp private beans.CouponType beans.Coupon.type CAMPING 

When displaying the Date information, I get something that is not clear, who can explain why?

    2 answers 2

    Just look at the official documentation :

     public Date(int year, int month, int day) Deprecated. instead use the constructor Date(long date) Constructs a Date object initialized with the given year, month, and day. The result is undefined if a given argument is out of bounds. 

    Actually, here:

     Parameters: year - the year minus 1900; must be 0 to 8099. (Note that 8099 is 9999 minus 1900.) month - 0 to 11 day - 1 to 31 
    • Pass in one parameter? From what I see there is one parameter, how can I SET the full date of the month and day to pass? Can I have a code sample? - Maks.Burkov
    • @ Mark.Burkov, the second part of my message, after "Actually, here:", there is a description of the parameters of the constructor that you use. It needs to transfer the year you need minus 1900, a month from 0 to 11, a day from 1 to 31. In your case, you should write Date start_date = new Date(88, 10, 9); and Date end_date = new Date(99, 8, 12); . However, you have already found java.time which should be used. - iksuy
      int year = 1988 , year2 = 1999; int month = 11 , month2 = 9; int day = 9 , day2 = 12; System.out.println(LocalDate.of(year,month,day)); System.out.println(LocalDate.of(year2,month2,day2)); 

    Simple solution Using the Locale Date class!

    Tutorial Date API Java 8