How do I calculate the date six months from the current date using the datetime Python module?

I found this solution to be good. (This uses the python-dateutil extension) from datetime import date from dateutil.relativedelta import relativedelta six_months = date.today() + relativedelta(months=+6) The advantage of this approach is that it takes care of issues with 28, 30, 31 days etc. This becomes very useful in handling business rules and scenarios (say invoice … Read more

java.util.Date vs java.sql.Date

Congratulations, you’ve hit my favorite pet peeve with JDBC: Date class handling. Basically databases usually support at least three forms of datetime fields which are date, time and timestamp. Each of these have a corresponding class in JDBC and each of them extend java.util.Date. Quick semantics of each of these three are the following: java.sql.Date … Read more

How do I translate an ISO 8601 datetime string into a Python datetime object? [duplicate]

I prefer using the dateutil library for timezone handling and generally solid date parsing. If you were to get an ISO 8601 string like: 2010-05-08T23:41:54.000Z you’d have a fun time parsing that with strptime, especially if you didn’t know up front whether or not the timezone was included. pyiso8601 has a couple of issues (check … Read more

How can I get the DateTime for the start of the week?

Use an extension method. They’re the answer to everything, you know! 😉 public static class DateTimeExtensions { public static DateTime StartOfWeek(this DateTime dt, DayOfWeek startOfWeek) { int diff = (7 + (dt.DayOfWeek – startOfWeek)) % 7; return dt.AddDays(-1 * diff).Date; } } Which can be used as follows: DateTime dt = DateTime.Now.StartOfWeek(DayOfWeek.Monday); DateTime dt = … Read more

ISO time (ISO 8601) in Python

Local to ISO 8601: import datetime datetime.datetime.now().isoformat() >>> 2020-03-20T14:28:23.382748 UTC to ISO 8601: import datetime datetime.datetime.utcnow().isoformat() >>> 2020-03-20T01:30:08.180856 Local to ISO 8601 without microsecond: import datetime datetime.datetime.now().replace(microsecond=0).isoformat() >>> 2020-03-20T14:30:43 UTC to ISO 8601 with TimeZone information (Python 3): import datetime datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() >>> 2020-03-20T01:31:12.467113+00:00 UTC to ISO 8601 with Local TimeZone information without microsecond (Python 3): import datetime datetime.datetime.now().astimezone().replace(microsecond=0).isoformat() >>> 2020-03-20T14:31:43+13:00 Local to … Read more

Convert java.util.Date to java.time.LocalDate

Short answer Date input = new Date(); LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); Explanation Despite its name, java.util.Date represents an instant on the time-line, not a “date”. The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start of 1970 GMT/UTC). The equivalent class to java.util.Date in JSR-310 is … Read more

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