GWT Date equivalent to java.util.Calendar

I want to get the date of a specific day of the week, for example, Wednessday. ie If today is the date of the 27th of Friday, Wednessday will infact be 25th.

I could achieve this

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

The problem here is that java.util.Calendar not supported on the client side of the GWT, is there a possible solution without having to move this code to the server?

thanks

+6
source share
4 answers

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

+16
source

The easiest way would be to use Joda time or the like, but all the GWT ports for JodaTime have been left: Time-time library for gwt

The most productive (without third-party dependencies) would be the calculation itself:

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

(the last line can be replaced with CalendarUtil.addDaysToDate(d, wed_delta) , changing d in place)

+2
source

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

 Date date = new Date(); DateTimeFormat format = DateTimeFormat.getFormat("EEEE"); String day = format.parse(date); 
+1
source

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

Emulation class and utilities based on the ftr-gwt-library project ( https://code.google.com/p/ftr-gwt-library/ )

0
source

Source: https://habr.com/ru/post/914339/


All Articles