FullCalendar 可以更新为从网络浏览器到服务器忽略TimeZone 吗?

发布于 2024-10-10 09:13:12 字数 757 浏览 4 评论 0原文

我正在使用与 Java Wicket Web 应用程序集成的 fullcalendar。

当网络浏览器的时区与服务器的时区不同时,我遇到日期问题。

例如:卡宴的网络浏览器 (UMT -3) 和巴黎的服务器 (UMT +1)。

我使用 fullcalendar 的“ignoreTimeZone=true”,因为我想保留网络浏览器用户时区的数据库日历。

当服务器初始化日历时:从巴黎发送到卡宴

服务器的事件发送如下事件

{"id":"53922","title":"0123456789","allDay":false,"start":"2011-01-06T09: 00:00.000+01:00"}

使用ignoreTimeZone=true 就可以了。日历上有 9 点。

为了验证,我用ignoreTimeZone=false进行了测试。我已将事件正确定位在 5 点钟位置 (9 - 4 = 5)。

当用户点击某一天创建新事件时,问题就来了!

对于 12 点时间,从网络浏览器发送的 JSON 值是正确的

allDay false 日期“2011-01-07T15:00:00.000Z” FeedbackFor“dayClick”

但是如果我想从ignoreTimeZone中获得等效的东西我更愿意 2011-01-07T12:00:00:000-03:00 格式或只是 2011-01-07T12:00:00.000

是否有可能有这种新的可能性?

预先感谢您的回复。

I'm using fullcalendar integrated with Java Wicket webapp.

I have a problem with date when timezone of webbrowser is different from timezone of server.

For example : webbrowser at Cayenne (UMT -3) and server at Paris (UMT +1).

I use "ignoreTimeZone=true" of fullcalendar because I want to keep in database calendar of timezone of the user of webbrowser.

When server initialize calendar : events sended from Paris to Cayenne

server send events like this

{"id":"53922","title":"0123456789","allDay":false,"start":"2011-01-06T09:00:00.000+01:00"}

With ignoreTimeZone=true it's ok. We have 9 o'clock in calendar.

To verify, I've tested with ignoreTimeZone=false. I have correctly event positioned at 5 o'clock (9 - 4 = 5).

Problem is coming when user click a day to create an new event !

For 12 o'clock time then JSON value sended from webbrowser is correct

allDay false
date "2011-01-07T15:00:00.000Z"
feedbackFor "dayClick"

But if I want to have equivalent from ignoreTimeZone I'd prefer to have
2011-01-07T12:00:00:000-03:00 format or just 2011-01-07T12:00:00.000

Is it possible to have this new possibility ?

Thanks by advance of your response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蛮可爱 2024-10-17 09:13:12

我的解决方案始终是从客户端的 FullCalendar 使用 UNIX 时间戳或 UTC 日期格式从服务器发送事件。

首先,使用 jquery-json 插件从客户端发送的事件类似于

var event ={"startDate" : startDate, "endDate" : endDate,"allDay" : allDay};
$.ajax({
  url : "${feedbackURL}", type: 'POST', contentType: 'application/json;charset=UTF-8'
 ,dataType: (($.browser.msie) ? "text" : "xml"), data : $.toJSON(event)
});

$.toJSON() 中序列化的事件将采用 UTC 格式 "yyyy-MM-dd'T'HH:mm:ss'Z'" 进行格式化

然后你可以使用gson和jodatime来解析UTC格式的日期

private static final DateTimeFormatter UTC_FORMAT = DateTimeFormat
        .forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.UTC);

public static final JsonDeserializer<Date> DATE_DESERIALIZER = new JsonDeserializer<Date>() {

    /**
     * @see org.apache.wicket.datetime.DateConverter#convertToObject(String,
     *      java.util.Locale)
     */
    @Override
    public Date deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context)
            throws JsonParseException {
        String value = json.getAsString().replace(".000", "");
        try {
            MutableDateTime dt = UTC_FORMAT.parseMutableDateTime(value);

            return dt.toDate();
        } catch (final Exception e) {
            LOG.debug("Date parsing error", e);
            throw new ConversionException(e);
        }
    }

};

其次,用org.apache.wicket.datetime.markup.html.form.DateTextFieldorg.apache.wicket显示日期.datetime.markup.html.basic.DateLabel

要处理客户端的时区问题,请将这些添加到您的应用程序中。

// always set your application's DateTimeZone to UTC
TimeZone.setDefault(TimeZone.getTimeZone("etc/UTC"));
DateTimeZone.setDefault(DateTimeZone.UTC);
// detect client's timezone in the WebClientInfo
getRequestCycleSettings().setGatherExtendedBrowserInfo(true);

最后,从服务器查询并使用 UNIX 时间戳将事件发送到客户端的 FullCalendar。

//Java
long toTimestamp(final Date date) {
return date.getTime() / 1000;
}

Date fromTimestamp(final long timestamp) {
return new Date(timestamp * 1000);
}

生成的json是这样的

[{"id":"1","title":"test1","allDay":true,"start":1299805200,"end":1299807000,"editable":false},
{"id":"2","title":"test2","allDay":false,"start":1299805200,"end":1299807000,"editable":true}]

My solution is always send events form server with UNIX timestamp or UTC dateformat from the client's FullCalendar.

First, the events sent from client using jquery-json plugin are like

var event ={"startDate" : startDate, "endDate" : endDate,"allDay" : allDay};
$.ajax({
  url : "${feedbackURL}", type: 'POST', contentType: 'application/json;charset=UTF-8'
 ,dataType: (($.browser.msie) ? "text" : "xml"), data : $.toJSON(event)
});

The event serialized in $.toJSON() will be formated with UTC fomat "yyyy-MM-dd'T'HH:mm:ss'Z'".

Then you can use gson and jodatime to parse the UTC format date

private static final DateTimeFormatter UTC_FORMAT = DateTimeFormat
        .forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.UTC);

public static final JsonDeserializer<Date> DATE_DESERIALIZER = new JsonDeserializer<Date>() {

    /**
     * @see org.apache.wicket.datetime.DateConverter#convertToObject(String,
     *      java.util.Locale)
     */
    @Override
    public Date deserialize(final JsonElement json, final Type typeOfT,
            final JsonDeserializationContext context)
            throws JsonParseException {
        String value = json.getAsString().replace(".000", "");
        try {
            MutableDateTime dt = UTC_FORMAT.parseMutableDateTime(value);

            return dt.toDate();
        } catch (final Exception e) {
            LOG.debug("Date parsing error", e);
            throw new ConversionException(e);
        }
    }

};

Second, display the date with org.apache.wicket.datetime.markup.html.form.DateTextField or org.apache.wicket.datetime.markup.html.basic.DateLabel.

To deal with Client's Timezone problem, add these to your Application

// always set your application's DateTimeZone to UTC
TimeZone.setDefault(TimeZone.getTimeZone("etc/UTC"));
DateTimeZone.setDefault(DateTimeZone.UTC);
// detect client's timezone in the WebClientInfo
getRequestCycleSettings().setGatherExtendedBrowserInfo(true);

Finally, query from server and send events to client's FullCalendar with UNIX Timestamp.

//Java
long toTimestamp(final Date date) {
return date.getTime() / 1000;
}

Date fromTimestamp(final long timestamp) {
return new Date(timestamp * 1000);
}

Produced jsons are like this

[{"id":"1","title":"test1","allDay":true,"start":1299805200,"end":1299807000,"editable":false},
{"id":"2","title":"test2","allDay":false,"start":1299805200,"end":1299807000,"editable":true}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文