6

I want to get the date of a particular day of the week e.g Wednessday. i.e If today's date is 27th Friday, Wednessday would infact be the 25th .

I could achieve this by

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
String thisWed = String.format("%tF%n", calendar)

The challenge here is that java.util.Calendar is not supported in GWT client side, Is there a possible solution without having to move this code to server side ?

Thanks

George Kagan
  • 5,543
  • 8
  • 45
  • 49
Babajide Prince
  • 552
  • 1
  • 10
  • 25
  • Duplicates earlier http://stackoverflow.com/questions/7009655/how-to-use-java-util-calendar-in-gwt – cellepo Oct 23 '14 at 17:33

4 Answers4

16

com.google.gwt.user.datepicker.client.CalendarUtil is what you are looking for, I guess;

aug
  • 10,442
  • 6
  • 70
  • 90
koma
  • 6,417
  • 2
  • 25
  • 52
2

Easiest would probably be to use Joda-time or similar, but all GWT ports of JodaTime have been abandonned: Date time library for gwt

The most performant (no third-party dependency) would be to do the calculation yourself:

Date d = new Date();
int dow = d.getDay();
int wed_delta = 3 - dow;
wed = new Date(d.getTime() + (86400 * wed_delta));

(the last line could be replaced with CalendarUtil.addDaysToDate(d, wed_delta), modifying d in-place)

Community
  • 1
  • 1
Thomas Broyer
  • 64,173
  • 7
  • 89
  • 163
  • Hi thomas, First glance at your logic, It looks OK but when I ran it, I get `Fri Apr 27 18:25:48 CAT 2012` . With my code above, I would get `Last Wednessday: 2012-04-25`. I expect to get the last Wednesday of that week of new Date(). – Babajide Prince Apr 27 '12 at 16:25
1

using DateTimeFormat, it allows format a java.util.Date into string or format a string into Date.

Date date = new Date();
DateTimeFormat format = DateTimeFormat.getFormat("EEEE");
String day = format.parse(date);
user1335794
  • 1,062
  • 6
  • 12
0

You can use the Calendar emulation and org.jresearch.commons.gwt.shared.tools.Dates utility class from open/gwt project on bitbucket (https://bitbucket.org/JRS/open-gwt)

The emulation and utility class based on ftr-gwt-library project (https://code.google.com/p/ftr-gwt-library/)

foal
  • 665
  • 7
  • 20