/**
 * 
 */
package se.chalmers.watchme.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import se.chalmers.watchme.R;

/**
 * A utility class for converting date's to common formats
 */
public class DateTimeUtils {
	
	private final static int HOUR = 60;
	
	/**
	 * Recieves a calendar instance and returns a String with simple
	 * date format(dd-MM-yyyy)
	 * 
	 * @param calendar The calendar object to be used to create the string
	 * @returns A simple string representing a date in the format: dd-MM-yyyy
	 */
	public static String toSimpleDate(Calendar calendar) {
		
		SimpleDateFormat simpleDate = new SimpleDateFormat("dd-MM-yyyy");
		String simpleDateString = simpleDate.format(calendar.getTime());
		
		return simpleDateString;
		
	}
	
	/**
	 * Convert duration in minutes to a more human readable form.
	 * 
	 * @param minutes The duration in minutes
	 * @return A string on the format "HH:mm"
	 */
	public static String minutesToHuman(int minutes) {
		int remainingMinutes = minutes % HOUR;
		int hours = minutes / HOUR;
		
		return hours+":" + ((remainingMinutes < 10) ? "0"+remainingMinutes : remainingMinutes);
	}
	
	
	/* TODO Is it okay to require the string to be formated in a specific way?
	 * Is this a place for pre-condition?
	 */
	
	/**
	 * Receives a String in the format "MM/dd/yyyy" and returns a Calendar object
	 * 
	 * @param string The string to convert to a Calendar object
	 * @returns Calendar object set to the recieved date
	 */
	public static Calendar toCalendar(String simpleDateString) {
		
		int month = 1;
		int day = 1;
		int year = 2000;
		
		/* TODO Feels like super-ugly code. But this will have to do for now.
		 * Use token to select substring according to slash (/) signs?
		 * Better ideas?
		 */
		
		// Try to convert the date in the string to integers
		try {
			month = Integer.parseInt(simpleDateString.substring(0, 1));
			day = Integer.parseInt(simpleDateString.substring(3, 4));
			year = Integer.parseInt(simpleDateString.substring(6, 9));
		}
		
		catch(NumberFormatException e) {
			System.err.println("Parse exception: " + e.toString());
			e.printStackTrace();
		}
		
		catch(IndexOutOfBoundsException e) {
			System.err.println("String formatted wrong " + e.toString());
			e.printStackTrace();
		}
		
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		
		return calendar;
		
	}

}

@@ -1148,1248 +1148,6 @@
 %7D%0A%09%0A
-%09%0A%09/* TODO Is it okay to require the string to be formated in a specific way?%0A%09 * Is this a place for pre-condition?%0A%09 */%0A%09%0A%09/**%0A%09 * Receives a String in the format %22MM/dd/yyyy%22 and returns a Calendar object%0A%09 * %0A%09 * @param string The string to convert to a Calendar object%0A%09 * @returns Calendar object set to the recieved date%0A%09 */%0A%09public static Calendar toCalendar(String simpleDateString) %7B%0A%09%09%0A%09%09int month = 1;%0A%09%09int day = 1;%0A%09%09int year = 2000;%0A%09%09%0A%09%09/* TODO Feels like super-ugly code. But this will have to do for now.%0A%09%09 * Use token to select substring according to slash (/) signs?%0A%09%09 * Better ideas?%0A%09%09 */%0A%09%09%0A%09%09// Try to convert the date in the string to integers%0A%09%09try %7B%0A%09%09%09month = Integer.parseInt(simpleDateString.substring(0, 1));%0A%09%09%09day = Integer.parseInt(simpleDateString.substring(3, 4));%0A%09%09%09year = Integer.parseInt(simpleDateString.substring(6, 9));%0A%09%09%7D%0A%09%09%0A%09%09catch(NumberFormatException e) %7B%0A%09%09%09System.err.println(%22Parse exception: %22 + e.toString());%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%0A%09%09catch(IndexOutOfBoundsException e) %7B%0A%09%09%09System.err.println(%22String formatted wrong %22 + e.toString());%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09%0A%09%09Calendar calendar = Calendar.getInstance();%0A%09%09calendar.set(year, month, day);%0A%09%09%0A%09%09return calendar;%0A%09%09%0A%09%7D%0A%0A
 %7D%0A

Remove unused toCalendar helper function