package se.chalmers.dat255.grupp12; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; /** * Created with IntelliJ IDEA. * User: Hagej * Date: 2013-10-07 * Time: 13:33 * To change this template use File | Settings | File Templates. */ public class Utils { /** * Try parse a Date from a String with 2 different formats, otherwise return null * * @param date - The String with the date to be parsed * @return - A Date if the String had the right format, otherwise null */ public static Calendar parseDate(String date){ if (date == null || date.equals("null")) { return null; } List<SimpleDateFormat> dateFormats = new ArrayList<SimpleDateFormat>(); dateFormats.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); Date d = new Date(); for(SimpleDateFormat format : dateFormats){ try { d = format.parse(date); break; } catch (ParseException e) { e.printStackTrace(); } } Calendar c = new GregorianCalendar(); c.setTime(d); System.out.println("RETURNING CALENDAR: " + c); return c; } public static String parseDate(Calendar date){ if (date == null) { return "null"; } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("TIME IN MILLIS: " + date.getTime()); String d = format.format(date.getTime()); return d; } }