Java string to date conversion

That’s the hard way, and those java.util.Date setter methods have been deprecated since Java 1.1 (1997). Moreover, the whole java.util.Date class was de-facto deprecated (discommended) since introduction of java.time API in Java 8 (2014). Simply format the date using DateTimeFormatter with a pattern matching the input string (the tutorial is available here). In your specific … Read more

How to get a function name as a string?

my_function.__name__ Using __name__ is the preferred method as it applies uniformly. Unlike func_name, it works on built-in functions as well: >>> import time >>> time.time.func_name Traceback (most recent call last): File “<stdin>”, line 1, in ? AttributeError: ‘builtin_function_or_method’ object has no attribute ‘func_name’ >>> time.time.__name__ ‘time’ Also the double underscores indicate to the reader this … Read more

How would you count occurrences of a string (actually a char) within a string?

If you’re using .NET 3.5 you can do this in a one-liner with LINQ: int count = source.Count(f => f == “https://stackoverflow.com/”); If you don’t want to use LINQ you can do it with: int count = source.Split(“https://stackoverflow.com/”).Length – 1; You might be surprised to learn that your original technique seems to be about 30% … Read more

How to check if a String is numeric in Java

This is generally done with a simple user-defined function (i.e. Roll-your-own “isNumeric” function). Something like: public static boolean isNumeric(String str) { try { Double.parseDouble(str); return true; } catch(NumberFormatException e){ return false; } } However, if you’re calling this function a lot, and you expect many of the checks to fail due to not being a … Read more

Creating a comma separated list from IList or IEnumerable

.NET 4+ IList<string> strings = new List<string>{“1″,”2″,”testing”}; string joined = string.Join(“,”, strings); Detail & Pre .Net 4.0 Solutions IEnumerable<string> can be converted into a string array very easily with LINQ (.NET 3.5): IEnumerable<string> strings = …; string[] array = strings.ToArray(); It’s easy enough to write the equivalent helper method if you need to: public static … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)