相当于 java.util.Calendar 的 GWT 日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10351893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GWT Date equivalent to java.util.Calendar
提问by Babajide Prince
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 .
我想获取一周中特定日期的日期,例如 Wednessday。即如果今天的日期是第 27 个星期五,则 Wednessday 实际上是第 25 个。
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 ?
这里的挑战是java.util.Calendar
GWT 客户端不支持,是否有可能的解决方案而不必将此代码移动到服务器端?
Thanks
谢谢
回答by koma
com.google.gwt.user.datepicker.client.CalendarUtil
is what you are looking for, I guess;
com.google.gwt.user.datepicker.client.CalendarUtil
我猜这就是你要找的东西;
回答by Thomas Broyer
Easiest would probably be to use Joda-time or similar, but all GWT ports of JodaTime have been abandonned: Date time library for gwt
最简单的可能是使用 Joda-time 或类似的,但 JodaTime 的所有 GWT 端口都已被放弃: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)
(最后一行可以替换为CalendarUtil.addDaysToDate(d, wed_delta)
,d
就地修改)
回答by user1335794
using DateTimeFormat
, it allows format a java.util.Date into string or format a string into Date.
using DateTimeFormat
,它允许将 java.util.Date 格式化为字符串或将字符串格式化为 Date。
Date date = new Date();
DateTimeFormat format = DateTimeFormat.getFormat("EEEE");
String day = format.parse(date);
回答by foal
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)
您可以在 bitbucket ( https://bitbucket.org/JRS/open-gwt)上使用 open/gwt 项目中的日历模拟和 org.jresearch.commons.gwt.shared.tools.Dates 实用程序类
The emulation and utility class based on ftr-gwt-library project (https://code.google.com/p/ftr-gwt-library/)
基于 ftr-gwt-library 项目的仿真和实用程序类 ( https://code.google.com/p/ftr-gwt-library/)