domingo, 5 de abril de 2015

Obtain minor date differences between two dates using joda time

Good day,

I recently stumbled contents of blogs and articles on the internet, which shows an interesting fact about a simple metadata: the date and time of the publication of content / article / blog post.

Specifically, in an application that i'm developing, I've encountered the displaying the date difference between that content and the current date, as examples, the following:

"18 minutes ago"
"1 hour ago"
"3 weeks ago"
"5 months ago"

To find a way to make the time difference, I researched a few tutorials, but just gave me the difference in milliseconds to hours .. in my case, I needed to get the difference in number of weeks, days and years, and calculate them more clearly.

For exercise, I used the opensource joda-time library, which contains utilities to calculate difference in dates / times for each of the details that I need (hours, days, weeks, etc).

Additionally, the solution to develop, did not show for dates applied, all calculations using these differences found, I had to compare and find the smallest difference calculated, and the term (detail) of time applied to this data , could make the simplest representation of how much time has passed since the publication of content and friendlier.

The operation in detail is in github, and shared in case any of you ever need ideas.

There's a sample code that applies utility method (using java); and for mi exercise, the label mentioned in above examples, its updated using the calculations made for this utility.

Regards and good coding

Check the gist

miércoles, 14 de enero de 2015

Generate a hash using java with few lines of code (complete)

In my previous post, I shared a method for converting a text using hash algorithms such as MD5 and SHA-1 .. well, now, I share a utility in java that allows conversions using various algorithms.

see code

You hope you find it useful

Generate a hash using java with few lines of code

Below I share an snippet of code in Java language, which can generate with just 3 lines of code, a text using cryptographic hash functions SHA1 (Secure Hash Algorithm, Secure Hash Algorithm version 2)

public static String generate (String input) throws Exception {
  MessageDigest md = MessageDigest.getInstance ("SHA1");
  byte [] = md.digest digested (input.getBytes ());
  return new BigInteger (1, digested) .toString (16);
}


To use MD5, just change the string "SHA1" with "MD5"

more info:
SHA, Wikipedia
Standard Algorithm Name Documentation