22

I have to use java.util.Calendar in GWT entry point, but I got error while running the application, that is because GWT is not able to find source code, is there anyway I could fix this issue.

Thanks in advance!!!

Joel
  • 7,401
  • 4
  • 52
  • 58
Delli Kilari
  • 988
  • 3
  • 14
  • 31

6 Answers6

23

java.util.Calendar is not an emulated class. You can find a list of emulated classes here:

http://code.google.com/webtoolkit/doc/latest/RefJreEmulation.html

I would strongly advise not to use any date/calendar library on the client side. You will probably only get yourself into trouble. In fact, even though java.sql.Date and java.util.Date are emulated in GWT, I would not use these either. If you're looking to use Calendar, then chances are you want to support timezones on the client. Using the emulated Date classes, you will somehow have to convert the client's Date from the browser's timezone to some target timezone (GMT or whatever the user defined in user preferences?). This will most definitely be error prone. Using GWT's TimeZone adds other issues. For instance, how do you map between java TimeZones and GWT TimeZones?

I recommend doing all date manipulation AND formatting on the server. On the client, you can simply use a date/month/year triplet. The server can have an association between user (or organization) and java.util.TimeZone (or joda timezone). Using the triplet and timezone you can easily create the specific instance in time.

nogridbag
  • 3,521
  • 4
  • 38
  • 51
18

You may be able to use com.google.gwt.user.datepicker.client.CalendarUtil.

Churro
  • 4,166
  • 3
  • 25
  • 26
6

No there is no way to use the java.util.Calendar in GWT because there is no equivalent in JavaScript. But there is an accepted feature request for it. Maybe you will find some hints in the comments of the request.

flavian
  • 28,161
  • 11
  • 65
  • 105
Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
3

The following shows how to use Joda Time to extract any date information from a Java Date type with Joda Times format() function and use it to build a new Date() using Joda Time's parse() function.

static DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss.SSS");
static DateTimeFormat datefmt = DateTimeFormat.getFormat("yyyy-MM-dd ");
static DateTimeFormat timefmt = DateTimeFormat.getFormat("HH:mm:ss.SSS");

public Date getDateTime(Date date, Date time) {
    String datetime = datefmt.format(date) + timefmt.format(time);
    return dtf.parse(datetime);
}
Joel
  • 7,401
  • 4
  • 52
  • 58
0

For those who prefer implementing the math directly, here is a solution for how to add days to an existing date object without using Calendar or any deprecated functions:

private Date addDays(Date dateIn, int numDays)
{
   long milisPerDay = 86400000;

   // convert the dateIn to milliseconds
   long dateInMilis = dateIn.getTime();

   // add numDays to the date
   dateInMilis = dateInMilis + (numDays * milisPerDay);

   return new Date(dateInMilis);
}


Here's how to get the current year as a String (very useful for Copyright notices)

long time = System.currentTimeMillis();
long milisPerYear = new BigInteger("31536000000").longValue();
String currentYear = String.valueOf((int) Math.floor(time / milisPerYear) + 1970);

Finding the constants such as number of miliseconds in a day, year, etc. is easy with Google. The tricky part is adding months, since the number of milliseconds in a month is going to depend on the month. If there's any demand for it, I'd be happy to write a function and post it here in order to add and subtract months.

emery
  • 8,603
  • 10
  • 44
  • 51
  • This may work fine as long as you don't care about DST transition periods. Here's the output of your method (all dates in New York's TZ): addDays(2013-11-03T01:00, 1) = 2013-11-04T00:00. Notice, the hour field is off by one. If you simply wanted to change the date, it's easier to use a trusted library like Joda. Both LocalDateTime.plusDays and DateTime.plusDays return the expected result (2013-11-04T01:00) – nogridbag Oct 09 '13 at 20:44
  • Totally agree with @nogridbag, except that Joda isn't supported by GWT either. Good Date classes for GWT aren't readily available. – Geoffrey Wiseman Nov 03 '14 at 20:18
0

Have you tried adding actual code of the class to your project? Some Java SDK classes compile well even though they are not in JRE white list.

okrasz
  • 3,866
  • 24
  • 15