/** * @param dateFormat pass <code>null</code> for default YYYYMMDD * @return <code>null</code> if timeStr is <code>null</code> */ private static Double ConvertDate(String dateStr, SimpleDateFormat dateFormat) { if (dateStr == null) { return Double.NaN; } DateTime dateVal; if (dateFormat == null) { dateVal = HSSFDateUtil.ParseYYYYMMDDDate(dateStr); } else { try { dateVal = DateTime.Parse(dateStr, CultureInfo.CurrentCulture); } catch (FormatException e) { throw new InvalidOperationException("Failed to parse date '" + dateStr + "' using specified format '" + dateFormat + "'", e); } } return HSSFDateUtil.GetExcelDate(dateVal); }
private ValueEval TryParseDateTime(double s0, string s1) { try { FormatBase dateFormatter = new SimpleDateFormat(s1); //first month of java Gregorian Calendar month field is 0 DateTime dt = new DateTime(1899, 12, 30, 0, 0, 0); dt = dt.AddDays((int)Math.Floor(s0)); double dayFraction = s0 - Math.Floor(s0); dt = dt.AddMilliseconds((int)Math.Round(dayFraction * 24 * 60 * 60 * 1000)); return new StringEval(dateFormatter.Format(dt)); } catch (Exception) { return ErrorEval.VALUE_INVALID; } }
/// <summary> /// Formats a number or date cell, be that a real number, or the /// answer to a formula /// </summary> /// <param name="cell">The cell.</param> /// <param name="value">The value.</param> /// <returns></returns> private String FormatNumberDateCell(CellValueRecordInterface cell, double value) { // Get the built in format, if there is one int formatIndex = ft.GetFormatIndex(cell); String formatString = ft.GetFormatString(cell); if (formatString == null) { return value.ToString(CultureInfo.InvariantCulture); } else { // Is it a date? if (Zephyr.Utils.NPOI.SS.UserModel.DateUtil.IsADateFormat(formatIndex, formatString) && Zephyr.Utils.NPOI.SS.UserModel.DateUtil.IsValidExcelDate(value)) { // Java wants M not m for month formatString = formatString.Replace('m', 'M'); // Change \- into -, if it's there formatString = formatString.Replace("\\\\-", "-"); // Format as a date DateTime d = Zephyr.Utils.NPOI.SS.UserModel.DateUtil.GetJavaDate(value, false); SimpleDateFormat df = new SimpleDateFormat(formatString); return df.Format(d); } else { if (formatString == "General") { // Some sort of wierd default return value.ToString(CultureInfo.InvariantCulture); } // Format as a number DecimalFormat df = new DecimalFormat(formatString); return df.Format(value); } } }