如何使用 GWT 将日期时间字符串转换为日期? [英] How to convert String of datetime to date using GWT?
问题描述
在 mysql 中,我有一个 datetime 类型的字段 time_entered(示例数据:2012-06-20 16:00:47).我还有一个方法 getTimeEntered(),它将值作为字符串返回.我想使用 GWT 中的 DateTimeFormat 以这种格式 2012-06-20 显示日期.
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));
问题是,格式方法不接受字符串形式的参数.因此,如果只有一种方法可以将日期从 String 转换为 Date 类型,它可能会起作用.我尝试过类型转换,但没有奏效.
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);
否则有一个轻量级版本支持这种模式的 SimpleDateFormat.
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
这篇关于如何使用 GWT 将日期时间字符串转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!