Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT Date equivalent to java.util.Calendar

Tags:

java

calendar

gwt

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

like image 949
Babajide Prince Avatar asked Apr 27 '12 13:04

Babajide Prince


3 Answers

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

like image 114
koma Avatar answered Oct 15 '22 12:10

koma


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)

like image 38
Thomas Broyer Avatar answered Oct 15 '22 11:10

Thomas Broyer


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);
like image 20
user1335794 Avatar answered Oct 15 '22 12:10

user1335794