Date->String, String->Date

2 views
Skip to first unread message

Daniel

unread,
Sep 23, 2009, 4:00:54 PM9/23/09
to Google Web Toolkit
I have a class called DateFormatter, which currently lives in both the
client and server directories, because I haven't found a way to do
what it does within a single file, or rather what I want it to do
within a single file.

Here's what the code looks like on the server side:

******************************************************************************************************
(package name removed)

import java.text.DateFormat;

public class DateFormatter {
private static final DateFormat dateFormatter =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public static final Date timeToDate(String time) {
Date date = null;
try {
date = dateFormatter.parse(time);
} catch(ParseException e) {}
return date;
}

public static final String dateToTime(Date date) {
return dateFormatter.format(date);
}
}
******************************************************************************************************

...and here's the code on the client side:


******************************************************************************************************
package org.codingventures.blog.client;

import com.google.gwt.i18n.client.DateTimeFormat;

public class DateFormatter {

private static DateTimeFormat dateFormatter =
DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");

public static final Date timeToDate(String time) {
Date date = null;
try {
date = dateFormatter.parse(time);
} catch(IllegalArgumentException e) {}
return date;
}

public static final String dateToTime(Date date) {
return dateFormatter.format(date);
}
}
******************************************************************************************************

Currently this is breaking just about everything because the client
code won't run on the server and the server code won't run on the
client. This _wouldn't_ be an issue but because I have serializable
classes being sent across GWT's RPC which have to use one of these I'm
running into a lot of problems.

So, is there a way to combine these classes?

Daniel

unread,
Sep 23, 2009, 4:17:31 PM9/23/09
to Google Web Toolkit
I suppose I can roll my own here....

Daniel

unread,
Sep 23, 2009, 10:38:54 PM9/23/09
to Google Web Toolkit
Here's my quick, lame, unreliable implementation if anyone wants it.

package ...;

import java.util.Date;

/**
* I had to roll my own implementation (and it's not particularly
good),
* because a client solution and a server solution were mutually
exclusive.
* This will only parse and return strings of the form:
* "yyyy-MM-dd HH:mm:ss"
*
* ...and I don't garuntee it's reliability. Sorry.
*
*
* @author doubleagent
*
*/
public class DateFormatter {

public static final Date timeToDate(String input) {
String dateTime[] = input.split(" ");
String dateStr[] = dateTime[0].split("-");
String timeStr[] = dateTime[1].split(":");
int date[] = reduceToInt(dateStr);
int time[] = reduceToInt(timeStr);
// cavaets: year counts from 1900 and month starts from 0
return new Date(date[0] - 1900, date[1] - 1, date[2],
time[0], time[1], time[2]);
}

private static int[] reduceToInt(String[] strs) {
int ints[] = new int[strs.length];
for(int i=0;i<strs.length - 1;i++)
ints[i] = Integer.parseInt(strs[i]);
// another caveat - mysql treats the seconds as a float
ints[ints.length - 1] = (int)Float.parseFloat(
strs[strs.length - 1]);
return ints;
}

public static final String dateToTime(Date d) {
String parts[] = {d.getYear() + 1900 + "", d.getMonth() + 1 + "",
"" + d.getDate(), "" + d.getHours(),
"" + d.getMinutes(), "" + d.getSeconds()};
for(int i=1;i<parts.length;i++)
if(parts[i].length()<2)
parts[i] = "0" + parts[i];
return parts[0] + "-" + parts[1] + "-" + parts[2] + " " +
parts[3] + ":" + parts[4] + ":" + parts[5];
}
}
Reply all
Reply to author
Forward
0 new messages