Skip to content

Commit

Permalink
- better date formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
szimano committed Feb 25, 2012
1 parent 25aee8f commit d6240a6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package pl.softwaremill.jozijug.joziawsdemo.impl.sdb;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
* User: szimano
*/
public class DateFormatter {

public SimpleDateFormat getDateFormat() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat format;

static {
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
}

public String formatDate(Date date) {
return format.format(date);
}

public Date parseDate(String dateString) {
try {
return format.parse(dateString);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import com.xerox.amazonws.simpledb.SDBException;
import org.jboss.seam.solder.core.Veto;
import pl.softwaremill.jozijug.joziawsdemo.MessageMappingConstants;
import pl.softwaremill.jozijug.joziawsdemo.entity.Message;
import pl.softwaremill.jozijug.joziawsdemo.service.AWS;
Expand Down Expand Up @@ -40,9 +39,9 @@ public void addMessage(Message msg) {
attrs.put(MessageMappingConstants.ROOM, msg.getRoom());
attrs.put(MessageMappingConstants.CONTENT, msg.getContent());
attrs.put(MessageMappingConstants.DATE,
dateFormatter.getDateFormat().format(msg.getDate()));
dateFormatter.formatDate(msg.getDate()));
attrs.put(MessageMappingConstants.SAVE_DATE,
dateFormatter.getDateFormat().format(msg.getSaveDate()));
dateFormatter.formatDate(msg.getSaveDate()));

try {
String itemId = msg.getUuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import pl.softwaremill.jozijug.joziawsdemo.service.MessagesLister;

import javax.inject.Inject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -62,17 +60,13 @@ private String escapeValue(String value) {
}

private Message convertItemToMessage(Item item) {
try {
return new Message(
UUID.fromString(item.getIdentifier()),
item.getAttribute(ROOM),
item.getAttribute(CONTENT),
dateFormatter.getDateFormat().parse(item.getAttribute(DATE)),
dateFormatter.getDateFormat().parse(item.getAttribute(SAVE_DATE))
);
} catch (ParseException e) {
throw new RuntimeException(e);
}
return new Message(
UUID.fromString(item.getIdentifier()),
item.getAttribute(ROOM),
item.getAttribute(CONTENT),
dateFormatter.parseDate(item.getAttribute(DATE)),
dateFormatter.parseDate(item.getAttribute(SAVE_DATE))
);
}

}

0 comments on commit d6240a6

Please sign in to comment.