Daniel
unread,Sep 23, 2009, 10:38:54 PM9/23/09Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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];
}
}