I have a Java Agent which I need to maintain (dev was done by someone else), with the following code samples extracted:

String type = (String) types.nextElement();
DemByDate dd = (DemByDate) client.typeDemande.get(type);

Vector v = (Vector) dd.demandes;

Calendar calendar = Calendar.getInstance();

calendar.set(dd.d2.getYear(), dd.d2.getMonth(), dd.d2.getDate());                       

int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

dd.d2 = new Date(dd.d2.getYear(), dd.d2.getMonth(), maxDay);
dd.d1 = new Date(dd.d1.getYear(), dd.d1.getMonth(), 1);
XML("<typeBesoin>" + type + "</typeBesoin>");
XML("<nbre>" + v.size() + "</nbre>");
XML("<periodeDebut>" + format.format(dd.d1) + "</periodeDebut>");
XML("<periodeFin>"+ format.format(dd.d2) + "</periodeFin>");

class DemByDate {
    Vector demandes;
    Date d1;
    Date d2;

I need to replace the .getYear() and .getMonth() accordingly, but so far, trying something like (ignore quotes) "calendar.set(dd.d2.calendar.year, dd.d2.month, dd.d2.date;"
results in a red underlined line of code, despite having for e.g. for year, int year = calendar.get(Calendar.YEAR);

In my editor I am having the getYear etc crossed out with a horizontal line across it, same goes for the line

dd.d2 = new Date(dd.d2.getYear(), dd.d2.getMonth(), maxDay); 

whereby the Date, getYear and getMonth are crossed out.    

Anyone can help me with this pls?

The Date class was there in the very first Javas, but it was a poor piece of design - it doesn't handle time zones etc, wich makes it essentially useless for anything real. It was replaced by the Calendar interface, which has an implementation in the GregorianCalendar class. This is what you should today use for anything date-related.
If you have legacy code that will break if you change the Date instances, then your best solution may be use Calendar to do all the day/month/year stuff, then convert to Date at the last possible moment using

new Date(myCalendarInstance.getTimeInMillis();

You may want to know that is all about to change again. Java 8 comes with a complete new date/time API that greatly enhances and fixes support for dates, times, intervals etc. Depending on your support requirements for different Java versions it may be possible/better to postpone any Date-related changes until you can use the Java 8 classes?


I've managed to overcome this issue using the code example below:

    String pattern = "dd/MM/yyyy";
    SimpleDateFormat format = new SimpleDateFormat(pattern);                
    String patternYear = "yyyy";
    SimpleDateFormat formatYear = new SimpleDateFormat(patternYear);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.