4

I'm now developing gwt project that want to add month to the given date. gwt doesn't support Calendar class so how can I do this ? Date is in "dd.MM.yyyy" format

public Date addMonth(Date d, int months){
}

mine implementation is so long that I need help. Thanks

[EDIT]

addMonth() means just add months to the given date's month. If it is 01.06.2010 adding 4 months is 01.10.2010

for 31 Jan and add 1 it will 28. Feb

3 Answers3

12

you can use CalendarUtil

import com.google.gwt.user.datepicker.client.CalendarUtil;
...
// Now
Date d = new Date();
// Now + 2 months
CalendarUtil.addMonthsToDate(d, 2);
Naaooj
  • 910
  • 8
  • 15
3

Take look at How to do calendar operations in Java GWT? How to add days to a Date?

Community
  • 1
  • 1
keuleJ
  • 3,418
  • 4
  • 30
  • 51
0

How about this? Ofcourse you will have to refine this based on how many days in a month and stuff but you get the basic idea. I am not familiar with gwt but I assumed it doesn't support joda time.

This is a rather crude solution. But without using the Calendar class or joda-time this is the only way I can think of for now.

        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy"); //today is 02.02.2011
        Date baseDate = null;
        try {
            baseDate = df.parse(df.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long oneMonthTime = baseDate.getTime()+30*24*60*60; //30 should change based on which month you are on
        System.out.println(df.format(oneMonthTime)); //prints 03.02.2011
CoolBeans
  • 20,654
  • 10
  • 86
  • 101