Creation timestamp and last update timestamp with Hibernate and MySQL

If you are using the JPA annotations, you can use @PrePersist and @PreUpdate event hooks do this: @Entity @Table(name = “entities”) public class Entity { … private Date created; private Date updated; @PrePersist protected void onCreate() { created = new Date(); } @PreUpdate protected void onUpdate() { updated = new Date(); } } or you … Read more

Extract date (yyyy/mm/dd) from a timestamp in PostgreSQL

You can cast your timestamp to a date by suffixing it with ::date. Here, in psql, is a timestamp: # select ‘2010-01-01 12:00:00′::timestamp; timestamp ——————— 2010-01-01 12:00:00 Now we’ll cast it to a date: wconrad=# select ‘2010-01-01 12:00:00’::timestamp::date; date ———— 2010-01-01 On the other hand you can use date_trunc function. The difference between them is … Read more

How to convert date to timestamp in PHP?

This method works on both Windows and Unix and is time-zone aware, which is probably what you want if you work with dates. If you don’t care about timezone, or want to use the time zone your server uses: $d = DateTime::createFromFormat(‘d-m-Y H:i:s’, ’22-09-2008 00:00:00′); if ($d === false) { die(“Incorrect date string”); } else … Read more

How to convert a unix timestamp (seconds since epoch) to Ruby DateTime?

Sorry, brief moment of synapse failure. Here’s the real answer. require ‘date’ Time.at(seconds_since_epoch_integer).to_datetime Brief example (this takes into account the current system timezone): $ date +%s 1318996912 $ irb ruby-1.9.2-p180 :001 > require ‘date’ => true ruby-1.9.2-p180 :002 > Time.at(1318996912).to_datetime => #<DateTime: 2011-10-18T23:01:52-05:00 (13261609807/5400,-5/24,2299161)> Further update (for UTC): ruby-1.9.2-p180 :003 > Time.at(1318996912).utc.to_datetime => #<DateTime: 2011-10-19T04:01:52+00:00 … Read more

How to parse/format dates with LocalDateTime? (Java 8)

Parsing date and time To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern. String str = “1986-04-08 12:30”; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm”); LocalDateTime dateTime = LocalDateTime.parse(str, formatter); Formatting date and … Read more

In Ruby on Rails, what’s the difference between DateTime, Timestamp, Time and Date?

The difference between different date/time formats in ActiveRecord has little to do with Rails and everything to do with whatever database you’re using. Using MySQL as an example (if for no other reason because it’s most popular), you have DATE, DATETIME, TIME and TIMESTAMP column data types; just as you have CHAR, VARCHAR, FLOAT and … Read more

How to get the unix timestamp in C#

As of .NET 4.6, there is DateTimeOffset.ToUnixTimeSeconds. This is an instance method, so you are expected to call it on an instance of DateTimeOffset. You can also cast any instance of DateTime, though beware the timezone. To get the current timestamp: DateTimeOffset.Now.ToUnixTimeSeconds() To get the timestamp from a DateTime: DateTime foo = DateTime.Now; long unixTime … Read more

How can one change the timestamp of an old commit in Git?

You can do an interactive rebase and choose edit for the commit whose date you would like to alter. When the rebase process stops for amending the commit you type in for instance: git commit –amend –date=”Wed Feb 16 14:00 2011 +0100″ –no-edit P.S. –date=now will use the current time. Afterward, you continue your interactive … Read more

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