예제 #1
0
        } // convertFromString()

        private static NDate convertFromNumber(NNumber value)
        {
            NNumber numberValue = (NNumber)value;
            long    long_value  = numberValue.asLong();

            NNumber n            = new NNumber(long_value);
            String  string_value = n.ToString();

            // test if the number is actually a format of the type yyyyMMdd
            if (string_value.Length == 8 && (string_value.StartsWith("1") || string_value.StartsWith("2")))
            {
                try
                {
                    string             format = "yyyyMMdd";
                    DateTimeFormatInfo dtfi   = NDateUtils.createDateFormat(format);
                    return(new NDate(string_value, format, dtfi));
                }
                catch (Exception e)
                {
                    // do nothing, proceed to next method of conversion
                }
            }

            // test if the number is actually a format of the type yyMMdd
            if (string_value.Length == 6)
            {
                try
                {
                    string             format = "yyMMdd";
                    DateTimeFormatInfo dtfi   = NDateUtils.createDateFormat(format);
                    return(new NDate(string_value, format, dtfi));
                }
            #pragma warning disable 0168
                catch (Exception e)
                {
                    // do nothing, proceed to next method of conversion
                }
            #pragma warning restore 0168
            }

            if (long_value > 5000000)
            {
                // Java: this number is most probably amount of milliseconds since 1970
                // C#:   this number is most probably amount of milliseconds since 1900
                NDate d = new NDate(DateTime.Now);
                return(new NDate(long_value));
            }
            else
            {
                // Java: this number is most probably amount of milliseconds since 1970
                // C#:   this number is most probably amount of milliseconds since 1900
                return(new NDate(long_value * 1000 * 60 * 60 * 24));
            }
        } // convertFromNumber()
예제 #2
0
        private static NDate convertFromString(String value)
        {
            try
            {
                long long_value = long.Parse(value);
                return(convertFromNumber(new NNumber(long_value)));
            }
            //catch (NumberFormatException e)
        #pragma warning disable 0168
            catch (FormatException e)
            {
                // do nothing, proceed to next method of conversion
            }
        #pragma warning restore 0168

            // try with Date.toString() date format first
            try
            {
                // https://msdn.microsoft.com/fr-fr/library/system.globalization.datetimeformatinfo.shortdatepattern(v=vs.110).aspx
                // https://mdsaputra.wordpress.com/2012/02/01/c-convert-datetime-to-formated-string-formated-string-to-datetime-with-datetimeformatinfo/
                // DateFormatSymbols dateFormatSymbols = DateFormatSymbols.getInstance(Locale.US);
                CultureInfo        ci   = new CultureInfo("en-US");
                DateTimeFormatInfo dtfi = ci.DateTimeFormat;
                dtfi.LongTimePattern = "EEE MMM dd HH:mm:ss zzz yyyy";
                //SimpleDateFormat   dateFormat  = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", dtfi);
                //return dateFormat.parse(value);
                return(new NDate(DateTime.ParseExact(value, "T", dtfi)));
            }
            catch (NParseException e)
            {
                // do noting
            }

            foreach (String prototype_pattern in prototypePatterns)
            {
                if (prototype_pattern.Length == value.Length)
                {
                    DateTimeFormatInfo dtfi;
                    try
                    {
                        dtfi = NDateUtils.createDateFormat(prototype_pattern);
                        return(new NDate(value, prototype_pattern, dtfi)); //  dateFormat.parse(value);
                    }
                    catch (Exception e)
                    {
                        // proceed to next formatter
                    }

                    if (prototype_pattern.IndexOf('-') != -1)
                    {
                        // try with '.' in stead of '-'
                        try
                        {
                            dtfi = NDateUtils.createDateFormat(prototype_pattern.Replace("\\-", "\\."));
                            return(new NDate(value, prototype_pattern, dtfi)); //dateFormat.parse(value);
                        }
                        catch (Exception e)
                        {
                            // proceed to next formatter
                        }

                        // try with '/' in stead of '-'
                        try
                        {
                            dtfi = NDateUtils.createDateFormat(prototype_pattern.Replace("\\-", "\\/"));
                            return(new NDate(value, prototype_pattern, dtfi)); // dateFormat.parse(value);
                        }
                        catch (Exception e)
                        {
                            // proceed to next formatter
                        }
                    }
                }
            }
            return(null);
        } // convertFromString()