package com.github.jep42.formatcompare.util;

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

public final class DateHelper {

	private DateHelper() {
		super();
	}

	public static Date getDateFromFormattedDateString(String dateString, String pattern, TimeZone timezone) throws ParseException {
		long timestamp = DateHelper.getTimestamp(dateString, pattern, timezone);
		return new Date(timestamp);
	}

	private static long getTimestamp(String dateValue, String pattern, TimeZone timezone) throws ParseException {
		DateFormat d = new SimpleDateFormat(pattern);
		Calendar c = Calendar.getInstance(timezone);
		c.setTime(d.parse(dateValue));
		return c.getTimeInMillis();
	}

	public static Date setTimeToEndOfDay(Date date, TimeZone timezone) {
		//log timezone and result of c here!!!
		Calendar c = Calendar.getInstance(timezone);
		System.out.println("[debug] Timezone: " + timezone.toString());
		System.out.println("[debug] c.getTimeInMillis(): " + c.getTimeInMillis());
		System.out.println("[debug] c.getTime().toString(): " + c.getTime().toString());
		c.setTime(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		c.set(Calendar.MILLISECOND, 999);
		System.out.println("[debug] c.getTimeInMillis(): " + c.getTimeInMillis());
		System.out.println("[debug] c.getTime().toString(): " + c.getTime().toString());
		//return new Date(c.getTimeInMillis());
		return c.getTime();
	}
}

@@ -895,16 +895,18 @@
 one) %7B%0D%0A
+//
 %09%09//log 
@@ -939,16 +939,18 @@
 ere!!!%0D%0A
+//
 %09%09Calend
@@ -985,24 +985,26 @@
 timezone);%0D%0A
+//
 %09%09System.out
@@ -1054,24 +1054,26 @@
 String());%0D%0A
+//
 %09%09System.out
@@ -1130,32 +1130,91 @@
 meInMillis());%0D%0A
+//%09%09//c.getTime() ==%3E the time in UTC *not* in timezone%0D%0A//
 %09%09System.out.pri
@@ -1277,24 +1277,26 @@
 String());%0D%0A
+//
 %09%09c.setTime(
@@ -1299,24 +1299,26 @@
 ime(date);%0D%0A
+//
 %09%09c.set(Cale
@@ -1337,24 +1337,26 @@
 _DAY, 23);%0D%0A
+//
 %09%09c.set(Cale
@@ -1366,32 +1366,34 @@
 r.MINUTE, 59);%0D%0A
+//
 %09%09c.set(Calendar
@@ -1403,24 +1403,26 @@
 COND, 59);%0D%0A
+//
 %09%09c.set(Cale
@@ -1442,24 +1442,26 @@
 OND, 999);%0D%0A
+//
 %09%09System.out
@@ -1526,16 +1526,18 @@
 is());%0D%0A
+//
 %09%09System
@@ -1616,47 +1616,403 @@
 );%0D%0A
-%09%09//return new Date(c.getTimeInMillis()
+//%09%09System.out.println(%22%5Bdebug%5D ret %22 + new Date(c.getTimeInMillis() + timezone.getOffset(c.getTimeInMillis())));%0D%0A//%09%09return new Date(c.getTimeInMillis() + timezone.getOffset(c.getTimeInMillis()));%0D%0A%0D%0A%0D%0A%09%09Calendar c = Calendar.getInstance();%0D%0A%09%09c.setTime(date);%0D%0A%09%09c.set(Calendar.HOUR_OF_DAY, 23);%0D%0A%09%09c.set(Calendar.MINUTE, 59);%0D%0A%09%09c.set(Calendar.SECOND, 59);%0D%0A%09%09c.set(Calendar.MILLISECOND, 999
 );%0D%0A

test date fixing