package systems.sdw.baseapps;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import systems.sdw.baseapps.Protokoll;
import systems.sdw.basetech.json.JSONException;
import systems.sdw.basetech.json.JSONObject;


public class TYPE {
	
	public static final String timeStampFormatDayDateTime = "EE dd.MM.yyyy HH:mm";
	public static final String dateFormatDayDateTime = "EE dd.MM.yyyy";
	public static final String timeFormatDayDateTime = "HH:mm";

	public final static String timeStampFormatFull_EN="EEE MMM dd HH:mm:ss zzz yyyy";
	public final static String timeStampFormatFull_DE="EE dd. MMM HH:mm:ss zzz yyyy";

	
	public final static String patternISOFull = "yyyy-MM-dd'T'HH:mm:ss.SSS";
	public final static String patternISONoMillis= "yyyy-MM-dd'T'HH:mm:ss";
	
	public static final Date infinityDate = new Date(9223372036825200000L);
	public static final String infinityTimestamp="292278994-08-17 00:00:00.0";
	

	
	/**
	 * Experimentell Erzeugt Timstamps aus Linus-Logeinträgen
	 * @param tsString
	 * @param tsFormat
	 * @return
	 */
	static public Timestamp ISOString2TimestampFull(String tsString, SimpleDateFormat tsFormat) {

		Calendar cal=Calendar.getInstance();	
		cal.clear();

		LocalDateTime ldt;
		try {
			//	Erst die englische Version testen
			ldt = ZonedDateTime.parse(tsString, DateTimeFormatter.ofPattern(timeStampFormatFull_EN, Locale.ENGLISH)).toLocalDateTime();

		} catch(java.time.format.DateTimeParseException eDTPEEn) {
			//	Die Ausgabe von date gibt immer das englische Wort für die Zeitzone zurück, der Rest ist deutsch falls die locale gesetzt ist 
			tsString=tsString.replace("CEST", "MESZ");
			tsString=tsString.replace("CET", "MEZ");
			ldt = ZonedDateTime.parse(tsString, DateTimeFormatter.ofPattern(timeStampFormatFull_DE, Locale.GERMAN)).toLocalDateTime();
		}
		cal.set(ldt.getYear(), ldt.getMonthValue()-1, ldt.getDayOfMonth());
		cal.set(Calendar.HOUR, ldt.getHour());
		cal.set(Calendar.MINUTE, ldt.getMinute());
		cal.set(Calendar.SECOND, ldt.getSecond());
		cal.set(Calendar.MILLISECOND, 0);
		return new java.sql.Timestamp(cal.getTimeInMillis());
	}


	
	static public Date Cal2Date(Calendar cal) {
		return new Date(cal.getTime().getTime());
	}
	
	static public Date getRootDate() {
		Calendar cal=Calendar.getInstance();
		cal.set(2000, 0, 1);
		return Cal2Date(cal);
	}
	
	static public Date getExcelRootDate() {
		Calendar cal=Calendar.getInstance();
		cal.set(1900, 0, 1);
		return Cal2Date(cal);
	}


	static public Date getFinalDate() {
		Calendar cal=Calendar.getInstance();
		cal.set(2999, 11, 31);
		return Cal2Date(cal);
	}

	static public String Decimal2String (Double nDouble, int nStellen) {
		if(nDouble==null) return "";
		return Decimal2String(nDouble, nStellen, null);
	}

	
	public static String string2Listitem(String string) {
		return "* "+string+"\n";
	}
	
	static public String Decimal2String (Double nDouble, int nStellen, Locale locale) {
		
		String sResult=new String("");
		
		if (nDouble==null) return sResult;
		else {
			
			java.text.DecimalFormat df=null;
			
			switch (nStellen) {
			case 0:
				df=new java.text.DecimalFormat( "###,##0" );
				break;
			case 1:
				df=new java.text.DecimalFormat( "###,##0.0" );
				break;
			case 2:
				df=new java.text.DecimalFormat( "###,##0.00" );
				break;	
			case 3:
				df=new java.text.DecimalFormat( "###,##0.000" );
				break;
			case 4:
				df=new java.text.DecimalFormat( "###,##0.0000" );
				break;
			default:	//	1 Nachkommastelle
				df=new java.text.DecimalFormat( "###,##0.0" );
			break;
			}
			if(locale != null) 	sResult= NumberFormat.getInstance(locale).format(df);
			else	sResult=df.format(nDouble);
		}		
		return sResult;
		


		
	}

	static public String Decimal2String (BigDecimal nDecimal, int nStellen) {
		
		String sResult=new String("");
		
		if (nDecimal==null) return sResult;
		else {
			
			java.text.DecimalFormat df=null;
			
			switch (nStellen) {
			case 0:
				df=new java.text.DecimalFormat( "###,##0" );
				break;
			case 1:
				df=new java.text.DecimalFormat( "###,##0.0" );
				break;
			case 2:
				df=new java.text.DecimalFormat( "###,##0.00" );
				break;	
			case 3:
				df=new java.text.DecimalFormat( "###,##0.000" );
				break;				
			default:	//	1 Nachkommastelle
				df=new java.text.DecimalFormat( "###,##0.0" );
			break;
			}
			sResult=df.format(nDecimal);
		}		
		return sResult;
	}
	
	
	static public String getWaehrungszeichen(String sWaehrungId) {
	
		String sZeichen=new String();
		int nWaehrungId=Integer.valueOf(sWaehrungId).intValue();
		switch (nWaehrungId) {
		case 1:
			sZeichen="&euro;";
			break;
		case 2:
			sZeichen="DM";
			break;
		case 3:
			sZeichen="BF";
			break;
		case 4:
			sZeichen="FF";
			break;
		case 5:
			sZeichen="HFL";
			break;
		case 6:
			sZeichen="Peseta";
			break;
		case 7:
			sZeichen="SKK";
			break;
		}
		
		return sZeichen;
	}
	
	
	static public String String2Searchstring(String vorname, String mittelname, String nachname) {

		StringBuffer buf=new StringBuffer();
		if (nachname!=null && nachname.length()>0) buf.append(nachname);
		if (mittelname!=null && mittelname.length()>0) buf.append(mittelname);
		if (vorname!=null && vorname.length()>0) buf.append(vorname);

		String s=buf.toString();
		if (s!=null && s.length()>0) {
			s=s.toLowerCase();
			s=s.replace("ä", "ae");
			s=s.replace("ü", "ue");
			s=s.replace("ö", "oe");
			s=s.replace("ß", "ss");
			s=s.replace(".", "");
			s=s.replace(",", "");
			s=s.replace("_", "");
			s=s.replace("-", "");
			s=s.replace(";", "");
		}

		return s;
	}

	static public String String2mailadress(String s) {
		s=s.toLowerCase();
		if (s!=null && s.length()>0) {
			s=s.toLowerCase();
			s=s.replace("ä", "ae");
			s=s.replace("ü", "ue");
			s=s.replace("ö", "oe");
			s=s.replace("ß", "ss");
			s=s.replace(" ", ".");
			
		}
		return s;
	}
	

	static public Date String2Date (String date) {
		
		if (date==null) return null;

		//	Excel/CSV sind Ganzzahlen, die am 01.01.1900 -2 Tage starten
		try {
			Integer exceldate=Integer.parseInt(date);
			
			// https://stackoverflow.com/questions/19028192/converting-number-representing-a-date-in-excel-to-a-java-date-object
			//	
			Calendar cal=Calendar.getInstance();
			cal.set(Calendar.YEAR, 1899);
			cal.set(Calendar.MONTH, 11);
			cal.set(Calendar.DAY_OF_MONTH, 30);
			//
			cal.add(Calendar.DAY_OF_MONTH, exceldate);
			//
			return new Date(cal.getTime().getTime());
			
		} catch (NumberFormatException eNoNumber) {
			;
		}
		
		if (date.length()!=10) return null;
		
	
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, Integer.valueOf(date.substring(0, 2)).intValue());
		cal.set(Calendar.MONTH, Integer.valueOf(date.substring(3, 5)).intValue()-1);
		cal.set(Calendar.YEAR, Integer.valueOf(date.substring(6, 10)).intValue());
		
		return new Date (cal.getTime().getTime());
	}
	
	
	static public Timestamp String2Timestamp (String buf) {
		
		System.out.print(buf);
		if (buf==null) return null;
		if (buf=="") return null;
	
		Calendar cal=Calendar.getInstance();
	
		cal.set(Calendar.DAY_OF_MONTH, Integer.valueOf(buf.substring(0, 2)).intValue());
		cal.set(Calendar.MONTH, Integer.valueOf(buf.substring(3, 5)).intValue()-1);
		cal.set(Calendar.YEAR, Integer.valueOf(buf.substring(6, 10)).intValue());
		
		cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(buf.substring(11, 13)).intValue());
		cal.set(Calendar.MINUTE, Integer.valueOf(buf.substring(14, 16)).intValue());
		cal.set(Calendar.SECOND, Integer.valueOf(buf.substring(17, 19)).intValue());
		
		return new Timestamp (cal.getTime().getTime());
	}

	
	static public Date getErsterVormonat() {
		
		Calendar cal=Calendar.getInstance();

		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
		
		return new Date (cal.getTime().getTime());
	}

	static public Date getLetzterVorvormonat() {
		
		Calendar cal=Calendar.getInstance();

		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);

		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)-1);
		cal.set(Calendar.DAY_OF_MONTH, 0);
		
		return new Date (cal.getTime().getTime());
	}
	
	
	
	static public Date AmericanString2Date (String date) {
		
		if (date==null) return null;
		if (date.length()!=8) return null;
	
		Calendar cal=Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, Integer.valueOf(date.substring(6, 8)).intValue());
		cal.set(Calendar.MONTH, Integer.valueOf(date.substring(4, 6)).intValue()-1);
		cal.set(Calendar.YEAR, Integer.valueOf(date.substring(0, 4)).intValue());
		
		return new Date (cal.getTime().getTime());
	}

	
	static public String String2String (String value) {
		if (value==null||value.equalsIgnoreCase("null")) return "";
		else return value;
	}

	static public boolean Long2Boolean(Long n) {
		if (n.intValue()==1) return true;
		else return false;
	}

	
	static public String Boolean2String(Boolean oBoolean) {
		String sBoolean="nein";
		if ((oBoolean!=null)&&(oBoolean.booleanValue()==true)) sBoolean="ja";
		return sBoolean;
	}
	/**
	 * Returns String with Format "dd.MM.yyyy"
	 * @param date
	 * @return
	 */
	static public String Date2String (Date date) {
		if (date!=null) return new java.text.SimpleDateFormat("dd.MM.yyyy").format(date);
		else return "";
	}

	static public String Date2StringFull (Date date) {
		if (date!=null) return new java.text.SimpleDateFormat("EEE dd.MM.yyyy").format(date);
		else return "";
	}

	static public String Date2FileDate (Date date) {
		if (date!=null) return new java.text.SimpleDateFormat("yyyy_MM_dd").format(date);
		else return "";
	}
	
	/**
	 * Bei Ranges von einem Tag gibt es das Datum des Tages zurück, bei mehrtätigen wird der String in folgendem Format erzeugt:
	 * Fr. 26.01.2018 bis Mo 31.01.2018 (5 Tage)
	 * @param von
	 * @param bis
	 * @return
	 */
	static public String Date2StringFull (Date von, Date bis) {
		if(von == null && bis == null) return "";
		if(bis == null || bis.equals(infinityDate)) return Date2StringFull(von)+" bis unbegrenzt";
		
		int tage = getDaysBetween(von, bis);
		
		if (tage == 0) return Date2StringFull(von);
		return Date2StringFull(von) +" bis "+ Date2StringFull(bis) +" ("+(tage+1)+" Tage)";
	}

	/**
	 * Timerange als pretty printed String
	 * @param von
	 * @param bis
	 * @return
	 */
	static public String Time2StringFull (Time von, Time bis) {
		if(von == null && bis == null) return "";
		if(bis == null) return  Time2String(von);
		return Time2String(von)+" bis "+ Time2String(bis);
	}

	/**
	 * Return an ISO 8601 combined date and time string for specified date/time
	 * 
	 * @param timestamp
	 * @return String with format "yyyy-MM-dd'T'HH:mm:ss'Z'"
	 */

	public static boolean isInfinity(Timestamp timestamp) {
		return timestamp.toString().equals(infinityTimestamp);
	}
	
	public static String Timestamp2ISO8601String(Timestamp timestamp) {
		if (timestamp==null) return "";
		if (timestamp.toString().equals(infinityTimestamp)) return "";
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		return dateFormat.format(timestamp);
	}
	
	
	public static String Timestamp2ISO8601StringUTC(Timestamp timestamp) {
		if (timestamp==null) return "";
		if (timestamp.toString().equals(infinityTimestamp)) return "";
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
		return dateFormat.format(timestamp);
	}
	
	public static String Timestamp2ISO8601String(Timestamp timestamp, String formatstring) {
		if (timestamp==null || timestamp.toString().equals(infinityTimestamp)) return "";
		DateFormat dateFormat = new SimpleDateFormat(formatstring);
		return dateFormat.format(timestamp);
	}

	
	public static String Date2ISO8601String(Date date) {
		if (date==null || date.equals(infinityDate)) return "";
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(date);
	}

//	public static String Timestamp2ISO8601String(Date date) {
//		if (date==null || date.equals(infinityDate)) return "";
//		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
//		return dateFormat.format(date);
//	}

	
	public static String Date2String(Date date, String formatstring) {
		if (date==null || date.equals(infinityDate)) return "";
		if (formatstring==null) return Date2String(date); 
		DateFormat dateFormat = new SimpleDateFormat(formatstring);
		return dateFormat.format(date);
	}
	
	public static String Time2ISO8601String(Time time) {
		if (time==null) return "";
		DateFormat dateFormat = new SimpleDateFormat("'T'HH:mm:ss");
		return dateFormat.format(time);
	}

	
	public static String getWeekday(Date date) {
		if (date==null) return null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		switch (cal.get(Calendar.DAY_OF_WEEK)) {
		case (Calendar.MONDAY):
			return "Montag";
		case (Calendar.TUESDAY):
			return "Dienstag";
		case (Calendar.WEDNESDAY):
			return "Mittwoch";
		case (Calendar.THURSDAY):
			return "Donnerstag";
		case (Calendar.FRIDAY):
			return "Freitag";
		case (Calendar.SATURDAY):
			return "Samstag";
		case (Calendar.SUNDAY):
			return "Sonntag";
		default: return null;
		}
	}


	public static Date ISO8601String2Date(String string) {
		if (string==null || string.length()==0 || string.equalsIgnoreCase("null")) return null;

		try {
			String pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'";
			SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.GERMANY);
			sdf.setTimeZone(TimeZone.getTimeZone("UTC+01:00"));

			Date date = new Date(sdf.parse(string).getTime());
			TimeZone ts = TimeZone.getTimeZone("CET");
			if(ts.inDaylightTime(date)) {
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
				cal.add(Calendar.HOUR, 1);
				date = new Date(date.getTime());
			}
			return date;
		} catch (ParseException e1) {

			try {
				String pattern = "yyyy-MM-dd'T'HH:mm:ss";
				SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.GERMANY);
				return new Date(sdf.parse(string).getTime());
				
			}
			catch (ParseException e2) {

				try {
					String pattern = "yyyy-MM-dd";
					SimpleDateFormat sdf = new SimpleDateFormat(pattern);
					return new Date(sdf.parse(string).getTime());
				} catch (ParseException e3) {
					e2.printStackTrace();
					return null;
				}
			}
		}

//		java.util.Date date = Date.from(java.time.ZonedDateTime.parse(string).toInstant()); 
//		return new Date(date.getTime());
	}

	
	
    public static Date ISO8601String2Date(String string, String pattern) {
		if (string==null || string.length()==0 || string.equalsIgnoreCase("null")) return null;

		if (pattern==null) pattern=patternISOFull;
		
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.GERMANY);
			Date date = new Date(sdf.parse(string).getTime());
			return date;
		} catch (ParseException e1) {
			e1.printStackTrace();
			return null;
		}
	}

	public static Timestamp ISO8601String2Timestamp(String string, String pattern) {
		if (string==null || string.length()==0) return null;
		return new Timestamp(ISO8601String2Date(string, pattern).getTime());
	}
	
	
	public static Timestamp ISO8601String2Timestamp(String string) {
		if (string==null || string.length()==0) return null;
		return new Timestamp(ISO8601String2Date(string).getTime());
	}
	
	
	
	public static Time ISO8601String2Time(String string) {
		if (string==null || string.length()==0) return null;
		if (string.length()==9) {
			// T08:30:00
			string="1970-01-01"+string;
		}
		Date date = ISO8601String2Date(string);
		if(date != null) return new Time(date.getTime());
		return null;
		
	}

	static public String Time2String (Time zeit) {
		if (zeit!=null) return new java.text.SimpleDateFormat("HH:mm").format(zeit);
		else return "";
	}
	
	
	static public String Time2String (Time zeit, String formatstring) {
		if (zeit!=null) return new java.text.SimpleDateFormat(formatstring).format(zeit);
		else return "";
	}

	
	static public Double getDiffHourTimestamp(Timestamp beginn, Timestamp ende, boolean ignoreSeconds) {
		
		if(ignoreSeconds) {
			Calendar cBeginn = Calendar.getInstance();
			cBeginn.setTime(beginn);
			cBeginn.set(Calendar.SECOND, 0);
			cBeginn.set(Calendar.MILLISECOND, 0);

			Calendar cEnde = Calendar.getInstance();
			cEnde.setTime(ende);
			cEnde.set(Calendar.SECOND, 0);
			cEnde.set(Calendar.MILLISECOND, 0);
			
			return MillisToHour(cEnde.getTimeInMillis()-cBeginn.getTimeInMillis());
		}
		
		return MillisToHour(ende.getTime()-beginn.getTime());
	}
	
	static public Integer getDiffMinutesTimestamp(Timestamp beginn, Timestamp ende, boolean ignoreSeconds) {
		
		if(ignoreSeconds) {
			Calendar cBeginn = Calendar.getInstance();
			cBeginn.setTime(beginn);
			cBeginn.set(Calendar.SECOND, 0);
			cBeginn.set(Calendar.MILLISECOND, 0);

			Calendar cEnde = Calendar.getInstance();
			cEnde.setTime(ende);
			cEnde.set(Calendar.SECOND, 0);
			cEnde.set(Calendar.MILLISECOND, 0);
			
			return MillisToMinute(cEnde.getTimeInMillis()-cBeginn.getTimeInMillis());
		}
		
		return MillisToMinute(ende.getTime()-beginn.getTime());
	}
	
	
	static public Double MillisToHour(Long milliseconds) {
		return milliseconds/1000.0/60.0/60.0;
		
	}
	
	static public Integer MillisToMinute(Long milliseconds) {
		Double result = milliseconds/1000.0/60.0;
		return result.intValue();
	}


	static public Time String2Time (String zeit) {
		DateFormat formatter = new SimpleDateFormat("HH:mm");
		Date time;
		try {
			time = new Date(formatter.parse(zeit).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
		return new Time(time.getTime());
	}


//	static public Time String2Time (String date) {
//		
//		if (date==null) return null;
//		if (date=="") return null;
//	
//		Calendar cal=Calendar.getInstance();
//		cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(date.substring(0, 2)).intValue());
//		cal.set(Calendar.MINUTE, Integer.valueOf(date.substring(3, 5)).intValue());
//		cal.set(Calendar.SECOND, Integer.valueOf(date.substring(6, 8)).intValue());
//		
//		return new Time (cal.getTime().getTime());
//	}

	
	static public java.sql.Date Timestamp2Date(Timestamp zeitstempel) {
		if (zeitstempel==null) return null;
		return new java.sql.Date(zeitstempel.getTime());
	}

	static public java.sql.Time Timestamp2Time(Timestamp zeitstempel) {
		if (zeitstempel==null) return null;
		return new java.sql.Time(zeitstempel.getTime());
	}

	static public String Timestamp2String (Timestamp zeitstempel) {
		if (zeitstempel!=null) return new java.text.SimpleDateFormat("dd.MM.yyyy HH.mm.ss").format(zeitstempel);
		else return "";
	}

	static public String Timestamp2StringTimeOnly (Timestamp zeitstempel) {
		if (zeitstempel!=null) return new java.text.SimpleDateFormat("HH:mm").format(zeitstempel);
		else return "";
	}

	static public String Timestamp2StringDateOnly (Timestamp zeitstempel) {
		if (zeitstempel!=null) return new java.text.SimpleDateFormat("dd.MM.yyyy").format(zeitstempel);
		else return "";
	}
	
	static public String Timestamp2StringFormatISO (Timestamp zeitstempel) {
		if (zeitstempel!=null) return new java.text.SimpleDateFormat(timeStampFormatDayDateTime).format(zeitstempel);
		else return "";
	}

	
	static public String TimestampRange2String (Timestamp von, Timestamp bis) {
		return TimestampRange2String(von, bis, null);
	}

	/**
	 * Fall der Tag der gleiche ist wird das Datum nur einmal angegben
	 * @param von
	 * @param bis
	 * @param formatString
	 * @return
	 */
	static public String TimestampRange2String (Timestamp von, Timestamp bis, String formatString) {
		if (von==null) return "";
		if (formatString==null) formatString=timeStampFormatDayDateTime;

		if (Timestamp2StringDateOnly(von).equals(Timestamp2StringDateOnly(bis))) {
			return TYPE.Timestamp2String(von, formatString)+"-"+Timestamp2StringTimeOnly(bis);
		} else {
			return TYPE.Timestamp2String(von, formatString)+" - "+TYPE.Timestamp2String(bis, formatString);
		}
	}

	
	static public String DateRange2String (Date von, Date bis) {
		if (von==null) return "";

		StringBuffer buf = new StringBuffer(TYPE.Date2String(von));
		if (bis==null || bis==TYPE.infinityDate) buf.append(" bis unbegrenzt");
		else buf.append(" bis "+TYPE.Date2String(bis));
		//
		return buf.toString();
	}

	static public String TimeRange2String (Time von, Time bis) {
		if (von==null) return "";

		StringBuffer buf = new StringBuffer(TYPE.Time2String(von));
		if (bis==null) {
			buf.append(" bis unbegrenzt");
		}
		else buf.append(" bis "+TYPE.Time2String(bis));
		//
		return buf.toString();
	}

	static public String Timestamp2String (Timestamp zeitstempel, String formatString) {
		return Timestamp2String(zeitstempel, formatString, false);
	}
	
	static public String Timestamp2String (Timestamp zeitstempel, String formatString, boolean unbegrenzt) {
		if (formatString==null) formatString=timeStampFormatDayDateTime;
		if (zeitstempel!=null && zeitstempel.before(TYPE.infinityDate)) return new java.text.SimpleDateFormat(formatString).format(zeitstempel);
		else {
			if (unbegrenzt==false) return "";
			else return "unbegrenzt";
		}
	}

	static public String Timestamp2String (Timestamp zeitstempel, String formatString, Locale locale) {
		if (formatString==null) formatString=timeStampFormatDayDateTime;
		if (zeitstempel!=null) return new java.text.SimpleDateFormat(formatString, locale).format(zeitstempel);
		else return "";
	}

	
	static public String Double2String2(double d) {
		return Double2String(d);
	}

	static public String Double2String(double d, java.util.Locale locale, String formatString) {
		NumberFormat nf = NumberFormat.getNumberInstance(locale);
		java.text.DecimalFormat df = (java.text.DecimalFormat) nf;
		df.applyPattern(formatString);
		return df.format(d);
	}

	static public String Double2String(double d) {
		java.text.DecimalFormat df=new java.text.DecimalFormat( "###,##0.00" );
		return df.format(d);
	}

	static public String Double2String1(double d) {		
		java.text.DecimalFormat df=new java.text.DecimalFormat( "###,##0.0" );
		return df.format(d);
	}

	static public String Double2String0(double d) {		
		java.text.DecimalFormat df=new java.text.DecimalFormat( "###,##0" );
		return df.format(d);
	}
	

	static public String percent (double d) {		
		java.text.DecimalFormat df=new java.text.DecimalFormat( "###,##0" );
		return df.format(d)+"%";
	}

	static public String Stunden(double d) {		
		java.text.DecimalFormat df=new java.text.DecimalFormat( "###,##0.00" );
		return df.format(d);
	}
	
	static public boolean dateIsToday(Date date) {
		return datesAreSameDay(date, getToday());
	}
	
	static public boolean datesAreSameDay(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		return cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);
	}
		
	static public Date getToday() {
		return new Date(new java.util.Date().getTime());
	}
	
	static public Timestamp getZeitstempel() {
		return new Timestamp(new java.util.Date().getTime());
	}
	
	
	static public String Monat2String(Long nMonat) {
		switch (nMonat.intValue()) {
		case 1: return "Januar";
		case 2: return "Februar";
		case 3: return "März";
		case 4: return "April";
		case 5: return "Mai";
		case 6: return "Juni";
		case 7: return "Juli";
		case 8: return "August";
		case 9: return "September";
		case 10: return "Oktober";
		case 11: return "November";
		case 12: return "Dezember";		
		}
		return "Monat unbekannt";
	}
	
	static public int getQuartal(Date datum) {
		if (datum==null) return 0;
		
		Calendar stichtag=Calendar.getInstance();
		stichtag.setTime(datum);
		//
		int month=stichtag.get(Calendar.MONTH)+1;
		
		switch (month) {
		case 1: 
		case 2: 
		case 3: 
			return 1;
		case 4: 
		case 5: 
		case 6: 
			return 2;
		case 7: 
		case 8: 
		case 9: 
			return 3;
		case 10: 
		case 11: 
		case 12: 
			return 4;
		}
		return 0;
	}
	
	static public Date getDateAtDayBefore(Date date) {
		Calendar cal= Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return new Date(cal.getTime().getTime());
	}
	
	static public Date getDateAtNextDay(Date date) {
		Calendar cal= Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, +1);
		return new Date(cal.getTime().getTime());
	}
	
	static public boolean isWorkday(Date date) {
		Calendar cal= Calendar.getInstance();
		cal.setTime(date);
		return isWorkday(cal);
	}

	static public boolean isWorkday(Calendar cal) {
		if( cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
			return false;
		} else {
			return true;
		}
	}

	static public Date getDateAtNextWorkday(Date date) {
		Calendar cal= Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, +1);
		if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			cal.add(Calendar.DAY_OF_MONTH, +1);
		}
		return new Date(cal.getTime().getTime());
	}

	static public Date getDateFromWorkdaysStartingWithTomorrow(int workdays) {
		Date startdate=getDateAtNextDay(getToday());
		Calendar cal=Calendar.getInstance();
		cal.setTime(startdate);
		cal.add(Calendar.DAY_OF_YEAR, getDaysFromWorkdays(startdate, workdays));
		return new Date(cal.getTime().getTime());
	}
	
	static public Date addWorkdays(Date startdate, int workdays) {
		Calendar cal=Calendar.getInstance();
		cal.setTime(startdate);
		cal.add(Calendar.DAY_OF_YEAR, getDaysFromWorkdays(startdate, workdays));
		return new Date(cal.getTime().getTime());
	}
	
	static public Date addDays(Date startdate, int days) {
		Calendar cal=Calendar.getInstance();
		cal.setTime(startdate);
		cal.add(Calendar.DAY_OF_YEAR, days);
		return new Date(cal.getTime().getTime());
	}
		
	static public int getDaysFromWorkdays(Date von, int workdays) {
		if (workdays<=0) return 0;
		//
		int i=workdays, days=workdays;
		Calendar cal= Calendar.getInstance();
		cal.setTime(von);
		for (i=workdays; i>0; i--) {
			if ((cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) days++;
			cal.add(Calendar.DAY_OF_YEAR, +1);
		}
		// substract 1 day 'cause the first day counts as one day, too
		days--;
		//	check target date for weekend. if so expand to next monday
		cal.setTime(von);
		cal.add(Calendar.DAY_OF_YEAR, days);
		if (cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY) days+=2;
		else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) days+=1;
		return days;
	}

	static public int getDaysBetween(Timestamp a, Timestamp b) {
		Date von = TYPE.Timestamp2Date(a);
		Date bis = TYPE.Timestamp2Date(b);
		return getDaysBetween(von, bis);
	}

	static public int getDaysBetween(Date a, Date b) {
		
		if(a == null || b == null) return 0;
		
		Calendar date1= Calendar.getInstance();
		Calendar date2= Calendar.getInstance();

		if(a.before(b)) {
			date1.setTime(a);
			date2.setTime(b);
		} else {
			date1.setTime(b);
			date2.setTime(a);
		}
		
		date1.set(Calendar.HOUR, 0);
		date1.set(Calendar.MINUTE, 0);
		date1.set(Calendar.SECOND, 0);
		date1.set(Calendar.MILLISECOND, 0);

		date2.set(Calendar.HOUR, 0);
		date2.set(Calendar.MINUTE, 0);
		date2.set(Calendar.SECOND, 0);
		date2.set(Calendar.MILLISECOND, 0);
		
		//long diffInMillis = date2.get(Calendar.DAY_OF_YEAR) - date1.get(Calendar.DAY_OF_YEAR);
//		long diffInDays = diffInMillis / 1000 / 60 / 60 / 24;
		
	//	return (int) diffInDays;
		
		if(date2.get(Calendar.YEAR) == date1.get(Calendar.YEAR)) 	return date2.get(Calendar.DAY_OF_YEAR) - date1.get(Calendar.DAY_OF_YEAR);
		else return date2.get(Calendar.DAY_OF_YEAR) - date1.get(Calendar.DAY_OF_YEAR) +(365*(date2.get(Calendar.YEAR) - date1.get(Calendar.YEAR)));
		
	}
	
	static public boolean equals(Date a, Date b) {
		Calendar cala= Calendar.getInstance();
		cala.setTime(a);
		Calendar calb= Calendar.getInstance();
		calb.setTime(b);
		if (cala.get(Calendar.YEAR)==calb.get(Calendar.YEAR) && cala.get(Calendar.MONTH)==calb.get(Calendar.MONTH) && cala.get(Calendar.DAY_OF_MONTH)==calb.get(Calendar.DAY_OF_MONTH)) return true;
		else return false;
	}
	
	
	public static Date minDate(Date a, Date b)  {
		if (b.before(a)) return b;
		else return a;
	}
	
	public static Date maxDate(Date a, Date b)  {
		if (b.before(a)) return a;
		else return b;
	}
	
	public static Double runden(Double wert, int dezimalstellen) {
		Double ergebnis = 0.0;
		if(wert==null) return null;
		Double pot = Math.pow(10,dezimalstellen);
		Long tmp = Math.round(wert*pot);
		ergebnis = tmp/pot;
		return ergebnis;
	}

	public static Integer runden(Double wert) {
		Double gerundet=runden(wert, 0);
		if (gerundet==null) return null;
		return (int)(double) gerundet;
	}
	
	public static Double roundToNearestHalf(Double number) {
	    return Math.round(number * 2) / 2.0;
	}
	
	public static Double roundToNearestHalf(Double number, Double minvalue) {
	    Double result =  Math.round(number * 2) / 2.0;
	    if(minvalue!=null && result < minvalue) return minvalue;
	    else return result;
	}

	public static Double round(Double value, int places) {
		if (value==null) return null;
		
	    if (places < 0) throw new IllegalArgumentException();

	    BigDecimal bd = BigDecimal.valueOf(value);
	    bd = bd.setScale(places, RoundingMode.HALF_UP);
	    return bd.doubleValue();
	}
	

	public static Timestamp getCurrentTimestamp() {
		return new java.sql.Timestamp(Calendar.getInstance().getTime().getTime());
	}

	public static int getCurrentYear() {
		return Calendar.getInstance().get(Calendar.YEAR);
	}

	public static int getYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);		
		return cal.get(Calendar.YEAR);
	}
	
	public static Boolean string2Boolean(String string) {
		if(string== null) return false;
		return string.equalsIgnoreCase("true") ||
				string.equalsIgnoreCase("1") ||
				string.equalsIgnoreCase("wahr") ||
				string.equalsIgnoreCase("ja") ||
				string.equalsIgnoreCase("on") ||
				string.equalsIgnoreCase("aktiv") ||
				string.equalsIgnoreCase("active");
		
	}
	
	
	public static double stundenZuTagen(double stunden, double arbeitZeitProTag) {
		if(stunden == 0.0) return stunden;
		else return TYPE.runden(stunden/arbeitZeitProTag, 2);
	}
	
	public static java.sql.Timestamp addTime2Timestamp(Timestamp von, double zeit) {

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(von.getTime());
			
		// Ganzzahl addieren
		int stunden = ((Double) Math.floor(zeit)).intValue();
		cal.add(Calendar.HOUR_OF_DAY, stunden);
		// Minuten addieren
		
		double diff =zeit-Math.floor(zeit);
		int minuten;
		if(diff!=0) {
			minuten = ((Double) (60*diff)).intValue();
			cal.add(Calendar.MINUTE, minuten);
		}
		return new java.sql.Timestamp(cal.getTimeInMillis());

	}
	
	public static java.sql.Timestamp addTimeSeconds2Timestamp(Timestamp von, int sekunden) {

		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(von.getTime());
		cal.add(Calendar.SECOND, sekunden);
		java.sql.Timestamp bis = new java.sql.Timestamp(cal.getTimeInMillis());
		
		return bis;
	
	}

	public static int getRemainingDaysInMonth(Date beginn) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(beginn);
		
		Calendar letzterTag = Calendar.getInstance();
		letzterTag.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, -1);
		
		return getDaysBetween(new Date(cal.getTime().getTime()), new Date(letzterTag.getTime().getTime()));
	}

	
	public static Date getFirstDayOfNextMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar ersterTag = Calendar.getInstance();
		ersterTag.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, 1);
		Date result =new Date(ersterTag.getTime().getTime());
		return result;
	}

	
	public static Date getFirstDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
		if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
			return new Date(cal.getTime().getTime());
		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			cal.add(Calendar.DAY_OF_YEAR, -6);
		    return new Date(cal.getTime().getTime());
		} else {
			cal.add(Calendar.DAY_OF_YEAR, -1*(cal.get(Calendar.DAY_OF_WEEK)-2));
		    return new Date(cal.getTime().getTime());
			
		}
	}
	
	
	public static Date getLastDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
		if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
			return new Date(cal.getTime().getTime());
		} else if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
			cal.add(Calendar.DAY_OF_YEAR, +6);
		    return new Date(cal.getTime().getTime());
		} else {
			cal.add(Calendar.DAY_OF_YEAR, (8-cal.get(Calendar.DAY_OF_WEEK)));
		    return new Date(cal.getTime().getTime());
			
		}
	}
	
	public static Date getFirstDayOfMonth(Date date, int anzahl) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar ersterTag = Calendar.getInstance();
		ersterTag.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+anzahl, 1);
		Date result =new Date(ersterTag.getTime().getTime());
		return result;
	}
	
	
	public static Date getLastDayOfMonth(Date date, int anzahl) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1+anzahl);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.DATE, -1);
		return new Date(calendar.getTime().getTime());
	}
	
	
	public static Date getLastDayOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar letzterTag = Calendar.getInstance();
		letzterTag.set(cal.get(Calendar.YEAR), 11, 31);
		Date result =new Date(letzterTag.getTime().getTime());
		return result;
	}
	
	public static Date getLastDayOfNextMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar ersterTag = Calendar.getInstance();
		ersterTag.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		Date result =new Date(ersterTag.getTime().getTime());
		return result;
	}

	public static Date getFirstDayOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar letzterTag = Calendar.getInstance();
		letzterTag.set(cal.get(Calendar.YEAR), 0, 1);
		Date result =new Date(letzterTag.getTime().getTime());
		return result;
	}
	
	

	/**
	 * Gibt einen Timestamp für das gegeben Datum mit der Uhrzeit 00:00:01 zurück
	 * @param date
	 * @return
	 */
	public static Timestamp date2TimeStampBeginOfDay(Date date) {
		if (date == null) return null;
		return date2TimeStampAtTime(date, 0, 0, 1);
	}
	
	/**
	 * Gibt einen Timestamp für das gegebene Datum mit der Uhrzeit 23:59:59 zurück
	 * @param ts
	 * @return
	 */
	public static Timestamp getLastMinuteOfDay(Timestamp ts) {
		if (ts == null) return null;
		Date date = TYPE.Timestamp2Date(ts);
		if (date == null) return null;
		return date2TimeStampAtTime(date, 23, 59, 59);
	}
	
	/**
	 * Gibt einen Timestamp für das gegebene Datum mit der Uhrzeit 00:00:01 zurück
	 * @param ts
	 * @return
	 */
	public static Timestamp getFirstMinuteOfDay(Timestamp ts) {
		if (ts == null) return null;
		Date date = TYPE.Timestamp2Date(ts);
		if (date == null) return null;
		return date2TimeStampAtTime(date, 0, 0, 1);
	}
	
	/**
	 * Gibt einen Timestamp für das gegebene Datum mit der Uhrzeit 23:59:59 zurück
	 * @param date
	 * @return
	 */
	public static Timestamp date2TimeStampEndOfDay(Date date) {
		if (date == null) return null;
		return date2TimeStampAtTime(date, 23, 59, 59);
		
	}
	/**
	 * Gibt ein Timestamp zum gegeben Datum zurück und stellt die Uhrzeit auf die übergebenen Parameter ein
	 * @param date
	 * @param hour
	 * @param min
	 * @param sec
	 * @return
	 */
	public static Timestamp date2TimeStampAtTime(Date date, int hour, int min, int sec) {
		if (date==null) return null;
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, hour);
		cal.set(Calendar.MINUTE, min);
		cal.set(Calendar.SECOND, sec);
		
		return new Timestamp(cal.getTimeInMillis());
	}

	public static String trim(String string) {
		if (string==null) return null;

		// vorangestellte Leerzeichen entfernen
		String result = string.replaceAll("^ +","");
		
		// Nachgestellte Leerzeichen entfernen
	    int i = result.length()-1;
        while (i >= 0 && Character.isWhitespace(result.charAt(i))) {
            i--;
        }
        return result.substring(0,i+1);
	}

	public static int boolean2int(Boolean b) {
		return  b? 1 : 0;
	}

	/**
	 * Variante die die Groß-/Kleinschreibung des Dateinamens beibehält. Wird bisher nur für das rekursive Zippen von Ordnern
	 * verwendet. Falls sich die Dateien in dem Ordner nur durch Groß-/Kleinschreibung unterscheiden wird in der alten Version
	 * String2Filename eine java.util.zip.ZipException geworfen und nur die 1. Datei wird in das Archiv übernommen.
	 * @param name
	 * @return
	 */
	public static String String2FilenameCaseSensitiv(String name) {
		
		return name
				.replaceAll("ü", "ue")
				.replaceAll("ä", "ae")
				.replaceAll("ö", "oe")
				.replaceAll("ß", "ss")
				.replaceAll("Ü", "Ue")
				.replaceAll("Ä", "Ae")
				.replaceAll("Ö", "Oe")
				
				.replaceAll("\"", "_")	// Neu: Keine Anführungszeichen
				.replaceAll("'", "_")
				
				.replaceAll("/", "_")
				.replaceAll(" ", "_")
				.replaceAll("\n", "_")
				.replaceAll("\t", "_")
		//		.replaceAll("\\.", "_")		Punkt sollte erlaubt sein. BW 03/2022
				.replaceAll(":", "_")
				.replaceAll("\\\\", "_")
				.replaceAll("\\(", "")
				.replaceAll("\\)", "")
				.replaceAll("-", "_");
	}
	
	/**
	 * Entfernt alle unerlaubten Zeichen aus Dateinamen
	 * @param name
	 * @return
	 */
	public static String String2Filename(String name) {
		
		return name.toLowerCase()
				.replaceAll("ü", "ue")
				.replaceAll("ä", "ae")
				.replaceAll("ö", "oe")
				.replaceAll("ß", "ss")
				
				.replaceAll("\"", "_")	// Neu: Keine Anführungszeichen
				.replaceAll("'", "_")

				.replaceAll("/", "_")
				.replaceAll(" ", "_")
				.replaceAll("\n", "_")
				.replaceAll("\t", "_")
				.replaceAll("\\?", "_")
		//		.replaceAll("\\.", "_")		Punkt sollte erlaubt sein. BW 03/2022
				.replaceAll(":", "_")
				.replaceAll("\\\\", "_")
				.replaceAll("\\(", "")
				.replaceAll("\\)", "")
				.replaceAll("-", "_");
	}

	
	public static String Double2Money(Double value, int stellen, Locale locale, String waehrungcode) {
		if (value==null) return "";
		java.util.Currency cur = java.util.Currency.getInstance(waehrungcode);
		java.text.NumberFormat format = java.text.NumberFormat.getCurrencyInstance(locale);
		format.setCurrency(cur);
 		return format.format(runden(value, stellen));
	}

	public static int getMinutesOfDay(Timestamp time) {
		int result = 0;
		Calendar cal = Calendar.getInstance();
		cal.setTime(time);
		result = cal.get(Calendar.HOUR_OF_DAY)*60+cal.get(Calendar.MINUTE);
		return result;
	}

	
	public static String getMonthname(int monat) {
		switch (monat) {
		case 1: return "Januar";
		case 2: return "Februar";
		case 3: return "März";
		case 4: return "April";
		case 5: return "Mai";
		case 6: return "Juni";
		case 7: return "Juli";
		case 8: return "August";
		case 9: return "September";
		case 10: return "Oktober";
		case 11: return "November";
		case 12: return "Dezember";
		default: return "Fehler";
		}
	}

	public static String unbreakableSpace(String inputstring) {
		return inputstring.replace(' ', '\u00a0');
	}



	public static String getAge(Date geburtsdatum, boolean allyear) {
		return getAge(geburtsdatum, allyear, null);
	}

	
	public static String getAge(Date geburtsdatum, boolean allyear, Date stichtag) {

		if(geburtsdatum ==null) return "";
		Calendar today = Calendar.getInstance();
		if(stichtag != null) today.setTime(stichtag);
		else today.setTime(TYPE.getToday());
		
		Calendar gebThisYear = Calendar.getInstance();
		gebThisYear.setTime(geburtsdatum);
		gebThisYear.set(Calendar.YEAR, today.get(Calendar.YEAR));

		Calendar geb = Calendar.getInstance();
		geb.setTime(geburtsdatum);
		
		int diff = today.get(Calendar.YEAR) - geb.get(Calendar.YEAR);
		if (!allyear) {
			// Der n-te Geburtstag im Jahr ist immer gleich, das Alter aber hängt vom aktuellen Datum ab
			if(gebThisYear.after(today)) diff--;
		}
		return diff+"";
	}
	
	
	public static Date convertDateToCurrentYear(Date datum) {

		if(datum ==null) return null;
		Calendar today = Calendar.getInstance();
		today.setTime(TYPE.getToday());
		
		Calendar gebThisYear = Calendar.getInstance();
		gebThisYear.setTime(datum);
		gebThisYear.set(Calendar.YEAR, today.get(Calendar.YEAR));

		return new Date(gebThisYear.getTime().getTime());

	}

	public static java.util.Date addDiff2Date(Calendar beginnCal, Timestamp von, Timestamp bis) {
		beginnCal.add(Calendar.MINUTE, getDiffMinutesTimestamp(von, bis, true));
		return beginnCal.getTime();
	}

	public static String String2JsonEscapedString4Grid(String textInput) {
		if(textInput != null) {
			textInput = textInput.replaceAll("\"", "\\\\\"");
			textInput = textInput.replaceAll("\n","<br>");
			textInput = textInput.replaceAll("\r","<br>");
			textInput = textInput.replaceAll("\t", "\\t");
			return textInput;
		} else {
			return "";
		}
	}
	
	
	public static String String2JsonEscapedString(String textInput) {
		if(textInput != null) {
			textInput = textInput.replaceAll("\"", "\\\\\"");
			textInput = textInput.replaceAll("\n","\\n");
			textInput = textInput.replaceAll("\r","\\r");
			textInput = textInput.replaceAll("\t", "\\t");
			return textInput;
		} else {
			return "";
		}
	}

	public static Timestamp getTimestamp(Date datum, Time von) {
		Calendar vonTime=Calendar.getInstance();
		vonTime.setTimeInMillis(von.getTime());
		
		Calendar cal=Calendar.getInstance();
		cal.setTime(datum);
		
		cal.set(Calendar.HOUR_OF_DAY, vonTime.get(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, vonTime.get(Calendar.MINUTE));
		cal.set(Calendar.SECOND, vonTime.get(Calendar.SECOND));
		return new Timestamp(cal.getTimeInMillis());
	}

	public static String zeitraum2String(Timestamp von, Timestamp bis) {
		String result;
		Calendar calVon =Calendar.getInstance();
		Calendar calBis = Calendar.getInstance();
		calVon.setTime(von);
		calBis.setTime(bis);

		if(calVon.get(Calendar.DAY_OF_YEAR) == calBis.get(Calendar.DAY_OF_YEAR)) {
			result = Timestamp2StringDateOnly(von) + " von " + Timestamp2StringTimeOnly(von) + " bis "+  Timestamp2StringTimeOnly(bis);
		} else {
			result = Timestamp2StringDateOnly(von) + " " + Timestamp2StringTimeOnly(von) + " bis "+Timestamp2StringDateOnly(von) +" "+  Timestamp2StringTimeOnly(bis);
		}
		System.out.println(result);

		return result;
	}

   
}
