如何使用GWT将日期时间的字符串转换为日期? [英] How to convert String of datetime to date using GWT?

查看:137
本文介绍了如何使用GWT将日期时间的字符串转换为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在mysql中,我有一个time_entered类型为datetime(示例数据:2012-06-20 16:00:47)的字段。我也有一个方法getTimeEntered(),它返回值为String。我想以这种格式2012-06-20使用GWT的DateTimeFormat显示日期。

In mysql, i have a field time_entered of type datetime (sample data: 2012-06-20 16:00:47). I also have a method, getTimeEntered(), that returns the value as String. I want to display the date in this format 2012-06-20 using DateTimeFormat from GWT.

这里是我的代码:

String date = aprHeaderDW.getTimeEntered();
DateTimeFormat fmt = DateTimeFormat.getFormat("MM-dd-yyyy");
dateEntered.setText("" + fmt.format(date));

问题是,format方法不接受参数作为String。所以如果只有一种方法,我可以将日期从字符串转换为日期类型,它可能会工作。我尝试过类型转换,但没有工作。

The problem is, the format method doesn't accept arguments as String. So if there's only a way I could convert the date from String to Date type, it could probably work. I tried typecasting but didn't work.

推荐答案

你应该可以使用 DateTimeFormat

Date date = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
String dateString = DateTimeFormat.getFormat("yyyy-MM-dd").format(date);

否则有一个

Otherwise there is a light-weight version of SimpleDateFormat that supports this pattern.

这篇关于如何使用GWT将日期时间的字符串转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆