package pl.bnsit.peaa.temporalobject;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Utils {

	public static Date findMaxDate( Map<Date, OrderVersion> map ) {
			
		final Set<Date> keySet = map.keySet();
		List<Date> aList = new ArrayList<Date>( keySet );
		Collections.sort( aList );
			
		return aList.get( aList.size() - 1 );
			
	}
	
	public static Date getNow() {
		return fromUtilDate( Calendar.getInstance().getTime() );
	}
	
	public static Date trimToHours( Date date ) {
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime( date );
		calendar.set( Calendar.MILLISECOND, 0 );
		calendar.set( Calendar.SECOND, 0 );
		calendar.set( Calendar.MINUTE, 0 );
		
		return fromUtilDate( calendar.getTime() );
	}
	
	public static Date trimToMinutes( Date date ) {
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime( date );
		calendar.set( Calendar.MILLISECOND, 0 );
		calendar.set( Calendar.SECOND, 0 );
		
		return fromUtilDate( calendar.getTime() );
	}	
	
	public static Date fromUtilDate( java.util.Date utilDate ) {
		return new Date( utilDate.getTime() );
	}
	
}
