Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert String of datetime to date using GWT?

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.

here's my code:

String date = aprHeaderDW.getTimeEntered();
DateTimeFormat fmt = DateTimeFormat.getFormat("MM-dd-yyyy");
dateEntered.setText("" + fmt.format(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.

like image 531
Mr. Xymon Avatar asked Jul 05 '12 10:07

Mr. Xymon


People also ask

How to convert String to date in GWT?

Date class itself: parse(String s) and toString() . Although the first is deprecated it is the method to use with GWT. If you want more control over how the Date is formatted, when converting to String use the GWT specific class: com.


1 Answers

You should be able to just use 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.

Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-06-20 16:00:47");
like image 118
Keppil Avatar answered Oct 05 '22 23:10

Keppil