package ch.elexis.core.time;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.GregorianCalendar;

import ch.elexis.core.jdt.Nullable;

public class TimeUtil {
	
	public static final DateTimeFormatter FULL_GER =
		DateTimeFormatter.ofPattern("dd.MM.yyyy, HH:mm:ss");
	public static DateTimeFormatter DATE_GER = DateTimeFormatter.ofPattern("dd.MM.yyyy");
	
	public static String formatSafe(LocalDateTime localDateTime){
		if (localDateTime != null) {
			return FULL_GER.format(localDateTime);
		}
		return "";
	}
	
	public static String formatSafe(LocalDate date){
		if (date != null) {
			return date.format(DATE_GER);
		}
		return "";
	}
	
	public static @Nullable LocalDate toLocalDate(Date date){
		if (date == null) {
			return null;
		}
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTimeInMillis(date.getTime());
		return gc.toZonedDateTime().toLocalDate();
	}
	
	public static LocalDateTime toLocalDateTime(Date date){
		if (date == null) {
			return null;
		}
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTimeInMillis(date.getTime());
		return gc.toZonedDateTime().toLocalDateTime();
	}
	
	public static Date toDate(LocalDateTime localDateTime){
		if (localDateTime == null) {
			return null;
		}
		return java.util.Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
	}
	
}

@@ -274,16 +274,51 @@
 til %7B%0A%09%0A
+%09/**%0A%09 * dd.MM.yyyy, HH:mm:ss%0A%09 */%0A
 %09public 
@@ -359,16 +359,16 @@
 L_GER =%0A
-
 %09%09DateTi
@@ -414,16 +414,41 @@
 m:ss%22);%0A
+%09/**%0A%09 * dd.MM.yyyy%0A%09 */%0A
 %09public 
@@ -526,16 +526,130 @@
 yyyy%22);%0A
+%09/**%0A%09 * dd.MM.yy%0A%09 */%0A%09public static DateTimeFormatter DATE_GER_SHORT = DateTimeFormatter.ofPattern(%22dd.MM.yy%22);%0A
 %09%0A%09publi
@@ -792,32 +792,91 @@
 return %22%22;%0A%09%7D%0A%09%0A
+%09/**%0A%09 * Format with default format %7B@link #DATE_GER%7D%0A%09 */%0A
 %09public static S
@@ -982,24 +982,181 @@
 rn %22%22;%0A%09%7D%0A%09%0A
+%09public static String formatSafe(LocalDate date, DateTimeFormatter formatter)%7B%0A%09%09if (date != null) %7B%0A%09%09%09return formatter.format(date);%0A%09%09%7D%0A%09%09return %22%22;%0A%09%7D%0A%09%0A
 %09public stat
@@ -1727,24 +1727,24 @@
 n null;%0A%09%09%7D%0A
-
 %09%09return jav
@@ -1821,13 +1821,214 @@
 t());%0A%09%7D
+%0A%0A%09public static Date toDate(LocalDate localDate)%7B%0A%09%09if (localDate == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09return java.util.Date%0A%09%09%09.from(localDate.atTime(9, 0).atZone(ZoneId.systemDefault()).toInstant());%0A%09%7D
 %0A%09%0A%7D%0A

Update TimeUtil