GWT 中日期格式化 ,处置Date
GWT的view中不能用java原生的DateFormat 必须使用gwt封装的格式化方法,方法如下
import com.google.gwt.i18n.client.DateTimeFormat;
Date date=new Date();
DateTimeFormat sdf = DateTimeFormat.getFormat("yyyy-MM-dd");
DateTimeFormat sdf2 = DateTimeFormat.getFormat("yyyy-MM");
String sStr=sdf2.format(date)+"-01";
String eStr= sdf.format(date);
String[] s = {sStr,eStr};
dateRangeField.setValue(s);
参考源码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.iloosen.imall.client.platform.module.stats.view; 2 3 4 import java.util.ArrayList; 5 import java.util.Date; 6 import java.util.List; 7 8 import org.apache.commons.lang.StringUtils; 9 10 import com.extjs.gxt.ui.client.data.BaseModelData; 11 import com.extjs.gxt.ui.client.data.BasePagingLoader; 12 import com.extjs.gxt.ui.client.data.BaseTreeModel; 13 import com.extjs.gxt.ui.client.data.DataField; 14 import com.extjs.gxt.ui.client.data.HttpProxy; 15 import com.extjs.gxt.ui.client.data.LoadConfig; 16 import com.extjs.gxt.ui.client.data.LoadEvent; 17 import com.extjs.gxt.ui.client.data.ModelData; 18 import com.extjs.gxt.ui.client.data.ModelType; 19 import com.extjs.gxt.ui.client.data.PagingLoadResult; 20 import com.extjs.gxt.ui.client.event.BaseEvent; 21 import com.extjs.gxt.ui.client.event.ComponentEvent; 22 import com.extjs.gxt.ui.client.event.Events; 23 import com.extjs.gxt.ui.client.event.Listener; 24 import com.extjs.gxt.ui.client.event.LoadListener; 25 import com.extjs.gxt.ui.client.store.ListStore; 26 import com.extjs.gxt.ui.client.widget.Label; 27 import com.extjs.gxt.ui.client.widget.MessageBox; 28 import com.extjs.gxt.ui.client.widget.form.TextField; 29 import com.extjs.gxt.ui.client.widget.grid.ColumnConfig; 30 import com.extjs.gxt.ui.client.widget.grid.ColumnModel; 31 import com.extjs.gxt.ui.client.widget.grid.Grid; 32 import com.extjs.gxt.ui.client.widget.layout.MarginData; 33 import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem; 34 import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar; 35 import com.google.gwt.core.client.GWT; 36 import com.google.gwt.http.client.RequestBuilder; 37 import com.google.gwt.i18n.client.DateTimeFormat; 38 import com.iloosen.imall.client.commons.WidgetCreateUtil; 39 import com.iloosen.imall.client.components.BaseButton; 40 import com.iloosen.imall.client.components.BaseContentPanel; 41 import com.iloosen.imall.client.components.ColumnConfigExt; 42 import com.iloosen.imall.client.components.IMallRequestBuilder; 43 import com.iloosen.imall.client.components.PagingToolBarExt; 44 import com.iloosen.imall.client.components.data.JsonPagingLoadResultReaderExt; 45 import com.iloosen.imall.client.components.event.CellClickEvent; 46 import com.iloosen.imall.client.components.selector.DateRangeField; 47 import com.iloosen.imall.client.constant.yljkc.IYljkcCoinLog; 48 import com.iloosen.imall.client.platform.module.stats.presenter.StatsExchangeCoinPresenter; 49 /** 50 * 51 * @ClassName: CouponManagerView 52 * @Description: 购物券管理批次管理View 53 * @author ly 54 * @date 2015年10月22日 上午9:24:02 55 * 56 */ 57 public class StatsExchangeCoinView extends BaseContentPanel implements StatsExchangeCoinPresenter.Display{ 58 59 private static final String KEY = "key"; 60 protected BasePagingLoader loader; 61 protected Label countLabel; 62 private BaseButton btnSearch; 63 protected Grid<ModelData> listGrid; 64 private TextField<String> txtSearch; 65 private BaseButton btnOutput; 66 private DateRangeField dateRangeField ;//时间段 67 68 public StatsExchangeCoinView() { 69 super(false, false); 70 setBodyBorder(false); 71 setBorders(false); 72 initToolBar(); 73 initGrid(); 74 } 75 76 /** 77 * 初始化 toolBar 的按钮 78 */ 79 private void initToolBar() { 80 btnSearch = new BaseButton("搜 索", "find"); 81 btnOutput = new BaseButton("导出", "excel"); 82 dateRangeField = WidgetCreateUtil.createDateRangeField("start", "end", "选择日期"); 83 Date date=new Date(); 84 DateTimeFormat sdf = DateTimeFormat.getFormat("yyyy-MM-dd"); 85 DateTimeFormat sdf2 = DateTimeFormat.getFormat("yyyy-MM"); 86 String sStr=sdf2.format(date)+"-01"; 87 String eStr= sdf.format(date); 88 String[] s = {sStr,eStr}; 89 dateRangeField.setValue(s); 90 91 txtSearch = new TextField<String>(); 92 txtSearch.setName("searchField"); 93 txtSearch.setAutoWidth(true); 94 txtSearch.setAutoHeight(true); 95 txtSearch.setTitle("登录名或用户名"); 96 txtSearch.setEmptyText("登录名或用户名"); 97 //添加工具 98 getToolBar().setLayoutData(new MarginData(10, 10, 10, 10)); 99 getToolBar().setHeader("59豆兑换消费统计"); 100 getToolBar().add(dateRangeField); 101 getToolBar().add(btnOutput); 102 getToolBar().add(new FillToolItem()); 103 getToolBar().add(txtSearch); 104 getToolBar().add(btnSearch); 105 } 106 107 /** 108 * 初始化 grid 109 */ 110 protected void initGrid() { 111 String url = GWT.getHostPageBaseURL() + "/admin/modules/yljkc/yljkcCoin/findPage.json"; 112 RequestBuilder builder = new IMallRequestBuilder(RequestBuilder.GET, url); 113 builder.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 114 HttpProxy<String> proxy = new HttpProxy<String>(builder); 115 116 ModelType type = new ModelType(); 117 type.setTotalName("total"); 118 type.setRoot("result"); 119 /**用户ID*/ 120 // DataField sysUserIdField = new DataField(IYljkcCoinLog.SYS_USER_ID, IYljkcCoinLog.SYS_USER_ID); 121 // DataField sysUserIdField = new DataField("loginId", "loginId"); 122 // sysUserIdField.setType(Integer.class); 123 //type.addField(sysUserIdField); 124 type.addField("loginId"); 125 type.addField(IYljkcCoinLog.SYS_USER_NAME); 126 type.addField(IYljkcCoinLog.USER_MOBILE); 127 type.addField(IYljkcCoinLog.TRANSACTION_START_AMOUNT); 128 type.addField(IYljkcCoinLog.TRANSACTION_END_AMOUNT); 129 type.addField(IYljkcCoinLog.TRANS_IN_AMOUNT); 130 type.addField(IYljkcCoinLog.PAY_FAIL_IN_AMOUNT); 131 type.addField(IYljkcCoinLog.OTHER_IN_AMOUNT); 132 type.addField(IYljkcCoinLog.DEDUCT_OUT_AMOUNT); 133 type.addField(IYljkcCoinLog.PAY_OUT_AMOUNT); 134 type.addField(IYljkcCoinLog.TO_MONEY); 135 type.addField(IYljkcCoinLog.START_TIME_STR); 136 type.addField(IYljkcCoinLog.END_TIME_STR); 137 138 JsonPagingLoadResultReaderExt<PagingLoadResult<ModelData>> reader = new JsonPagingLoadResultReaderExt<PagingLoadResult<ModelData>>(type); 139 loader = new BasePagingLoader<PagingLoadResult<ModelData>>(proxy, reader); 140 loader.setRemoteSort(true); 141 loader.addLoadListener(new LoadListener() { 142 public void loaderBeforeLoad(LoadEvent le) { 143 LoadConfig config = le.getConfig(); 144 parameters(config); 145 } 146 }); 147 int pageSize = 25; 148 149 150 ListStore<ModelData> listStore = new ListStore<ModelData>(loader); 151 PagingToolBar pagingToolBar = new PagingToolBarExt(pageSize); 152 pagingToolBar.bind(loader); 153 154 countLabel = new Label(); 155 pagingToolBar.insert(countLabel, 13); 156 /** 157 * ColumnConfig 158 * 设置显示页面的表头 159 */ 160 List<ColumnConfig> configs = new ArrayList<ColumnConfig>(); 161 162 //ColumnConfig sysUserIdConfig =new ColumnConfigExt(IYljkcCoinLog.SYS_USER_ID, "用户id", 90); 163 ColumnConfig sysUserIdConfig =new ColumnConfigExt("loginId", "登录名", 90); 164 configs.add(sysUserIdConfig); 165 166 ColumnConfig sysUserNameConfig = new ColumnConfigExt(IYljkcCoinLog.SYS_USER_NAME, "用户名", 150); 167 configs.add(sysUserNameConfig); 168 169 ColumnConfig userMobileConfig = new ColumnConfigExt(IYljkcCoinLog.USER_MOBILE, "手机号", 100); 170 configs.add(userMobileConfig); 171 172 ColumnConfig transactionStartAmountConfig = new ColumnConfigExt(IYljkcCoinLog.TRANSACTION_START_AMOUNT, "期初余额", 100); 173 configs.add(transactionStartAmountConfig); 174 ColumnConfig transactionEndAmountConfig = new ColumnConfigExt(IYljkcCoinLog.TRANSACTION_END_AMOUNT, "期末余额", 100); 175 configs.add(transactionEndAmountConfig); 176 177 ColumnConfig transInAmountConfig = new ColumnConfigExt(IYljkcCoinLog.TRANS_IN_AMOUNT, "59豆兑换入账", 100); 178 configs.add(transInAmountConfig); 179 ColumnConfig payFailInAmountConfig = new ColumnConfigExt(IYljkcCoinLog.PAY_FAIL_IN_AMOUNT, "支付失败入账", 100); 180 configs.add(payFailInAmountConfig); 181 ColumnConfig otherInAmountConfig = new ColumnConfigExt(IYljkcCoinLog.OTHER_IN_AMOUNT, "其他入账", 100); 182 configs.add(otherInAmountConfig); 183 ColumnConfig deductOutAmount = new ColumnConfigExt(IYljkcCoinLog.DEDUCT_OUT_AMOUNT, "批量扣减出账", 100); 184 configs.add(deductOutAmount); 185 ColumnConfig payOutAmount = new ColumnConfigExt(IYljkcCoinLog.PAY_OUT_AMOUNT, "健康币支付出账", 100); 186 configs.add(payOutAmount); 187 ColumnConfig toMoney = new ColumnConfigExt(IYljkcCoinLog.TO_MONEY, "健康币抵扣金额", 100); 188 configs.add(toMoney); 189 190 ColumnConfig couponStartTimeConfig = new ColumnConfigExt(IYljkcCoinLog.START_TIME_STR, "开始日期", 100); 191 configs.add(couponStartTimeConfig); 192 193 ColumnConfig couponEndTimeConfig = new ColumnConfigExt(IYljkcCoinLog.END_TIME_STR, "结束日期", 100); 194 configs.add(couponEndTimeConfig); 195 ColumnModel cm = new ColumnModel(configs); 196 197 listGrid = new Grid<ModelData>(listStore, cm); 198 listGrid.setStripeRows(true); 199 listGrid.disableTextSelection(false); 200 listGrid.setLoadMask(true); 201 202 this.getMainPanel().getAriaSupport().setLabelledBy(this.getMainPanel().getId()); 203 this.getMainPanel().add(listGrid); 204 this.getMainPanel().setBottomComponent(pagingToolBar); 205 } 206 207 protected void parameters(LoadConfig config) { //NOSONAR 208 if (txtSearch.getValue() != null) { 209 config.set(txtSearch.getName(), txtSearch.getValue()); 210 } 211 if (dateRangeField.getValue() != null && !dateRangeField.getStartTimeString().equals("") && !dateRangeField.getEndTimeString().equals("")) { 212 config.set("startDate", dateRangeField.getStartTimeString()); 213 config.set("endDate", dateRangeField.getEndTimeString()); 214 } 215 } 216 217 @Override 218 public void setClickGridCellHandler(Listener<CellClickEvent> listener) { 219 listGrid.addListener(CellClickEvent.TYPE,listener); 220 } 221 222 @Override 223 public Grid<ModelData> getListGrid() { 224 return listGrid; 225 } 226 227 @Override 228 public void reLoadGrid() { 229 // RestUtil.sendRestRequest("/admin/modules/yljkc/yljkcCoin/findPage.json", getSearchModel(), RestUtil.REST_GET, new BaseCallBack() { 230 // @Override 231 // protected void onSuccess(Request request, Response response) { 232 // String text = response.getText(); 233 // JSONObject json = (JSONObject) JSONParser.parseStrict(text); 234 // JSONObject result = (JSONObject) json.get("result"); 235 // ModelData md = ModelUtil.json2ModelData(result, new ModelUtil.ModelCreator() { 236 // public ModelData create() { 237 // return new BaseTreeModel(); 238 // } 239 // }); 240 // //countLabel.setText(getCouponBatchCountInfo(md)); 241 // } 242 // }); 243 listGrid.getStore().getLoader().load(); 244 } 245 // /** 246 // * 获取购物券的统计信息 247 // * @param m 248 // * @return 249 // */ 250 // public String getCouponBatchCountInfo(ModelData m){ 251 // return "<Font style='font-size: 13px;padding:5px 10px;'>" + 252 // "共生成 " + (m != null ?((Double) m.get("couponCount")).intValue() : 0) + " 张" + 253 // " 总金额¥:<Font style='font-weight:bold'>" + (m != null ?((Double) m.get("totalAmount")).intValue() : 0) + 254 // "</Font> 充值总金额¥:<Font style='color:green'>" + (m != null ? ((Double)m.get("bindAmount")).intValue() : 0) + 255 // "</Font> 剩余总金额¥:<Font style='color:red'>" + (m != null ? ((Double)m.get("unbindAmount")).intValue() : 0) + "</Font></Font>"; 256 // } 257 /** 258 * 添加搜索条件 259 * @return 260 */ 261 262 private BaseModelData getSearchModel() {//NOSONAR 263 BaseModelData searchModel = new BaseModelData(); 264 if (txtSearch.isEnabled()) { 265 if (txtSearch.getValue() != null) { 266 searchModel.set(txtSearch.getName(), txtSearch.getValue()); 267 } 268 if (dateRangeField.getValue() != null && !dateRangeField.getStartTimeString().equals("") && !dateRangeField.getEndTimeString().equals("")) { 269 searchModel.set("startDate", dateRangeField.getStartTimeString()); 270 searchModel.set("endDate", dateRangeField.getEndTimeString()); 271 } 272 } 273 return searchModel; 274 } 275 @Override 276 public void addSearchListener(Listener<? extends ComponentEvent> listener) { 277 btnSearch.addListener(Events.OnClick, listener); 278 } 279 280 @Override 281 public void addOutputListener(Listener<BaseEvent> listener) { 282 btnOutput.addListener(Events.OnClick, listener); 283 } 284 285 @Override 286 public List<Object> getTime() { 287 List<Object> time = new ArrayList<Object>(); 288 if(dateRangeField.getValue() != null && !dateRangeField.getStartTimeString().equals("") && !dateRangeField.getEndTimeString().equals("")){ 289 time.add(dateRangeField.getStartTimeString()); 290 time.add( dateRangeField.getEndTimeString()); 291 if(txtSearch.getValue()!=null){ 292 time.add(txtSearch.getValue()); 293 } 294 return time; 295 } 296 MessageBox.alert("系统信息", "请选择日期项再确定操作!", null); 297 return null; 298 } 299 300 }