Friday, 25 June 2010

Displaying the Date

I always always get in trouble when using Date, because of the huge number of deprecated methods it has. We should use Calendar almost exclusively.

Here's a simple example of a bad way and a good way to display a nice date:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DisplayDate
{

    public static void main(String args[]) 
    {
        // worse way
        Date myDate = new Date();
        int month = myDate.getMonth();
        int year = myDate.getYear();
        int day = myDate.getDate();
        System.out.println("Current date : " +
            day + "/" + (month + 1) + "/"
            + (year + 1900));
        Calendar cal = Calendar.getInstance();

        // bad way
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println("Current date : " +
            day + "/" + (month + 1) + "/" + year);

        // good way
        SimpleDateFormat simpleDateFormat = 
            new SimpleDateFormat("dd/MM/yyyy");
        String s =
           simpleDateFormat.format(cal.getTime());
        System.out.println("Current date : " + s);

    }
}

References

Javadoc - SimpleDateFormat
http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Wednesday, 23 June 2010

JSON and the single-value array

The one you really need to read can be found here : https://blogs.oracle.com/japod/entry/configuring_json_for_restful_web

Jersey NATURAL JSON notation is awesome.

28-05-2012: Updated with new links to more up to date content.

Monday, 21 June 2010

Getting your Windows Process Id

A java-unrelated post for once. I've been having huge issues with the fact that all my java programs running on my windows machine all have the command line "java.exe", which is no surprise. But how to decide which one (that's run out of memory or otherwise hangs) to shut down?

I needed the process id.

Thank god for http://www.scheibli.com/projects/getpids/index.html.