The singleton

While I agree that a Singleton shouldn’t be used to often, I do want to share my favourite way to implement the singleton pattern for reference.

public class Example
{
    private static Example instance;

    private Example()
    {
        // this class cannot get instantiated
    }

    public static Example getInstance()
    {
        if (instance == null)
        {
            instance = new Example();
        }

        return instance;
    }
}

Compare objects in java

If you want to compare if plain old java objects have the same properties, it is easiest to override the equals method.  This way, if you initialise objects on different places, you can be certain when comparing that they are the same.

We will show you how to do it with this simple class:

public class Insect
{
    private int legs;
    private int eyes;
    private int colour;
    private String sortName;
}

Override equals

@Override
public boolean equals(Object o)
{
    if (this == o)
    {
        return true;
    }

    if (o == null || this.getClass() != o.getClass())
    {
        return false;
    }

    final Insect that = (Insect) o;

    if (this.legs != that.legs)
    {
        return false;
    }

    if (this.eyes != that.eyes)
    {
        return false;
    }

    if (this.colour != that.colour)
    {
        return false;
    }

    if (this.sortName != null ? (this.sortName != that.sortName) : (that.sortName != null))
    {
        return false;
    }

    return true;
}

Override hashCode()

@Override
public int hashCode()
{
    int hash = super.hashCode();
    hash = 14 * hash + this.legs;
    hash = 14 * hash + this.eyes;
    hash = 14 * hash + this.colour;
    hash = 14 * hash + (this.sortName != null ? this.sortName.hashCode() : 0);
    return hash;
}

Creating a new Date or DateTime with ease in Java

For a school application in Java, we had to make a coffee application. The coffee application is a very small application, but trains a few very hard techniques like observer and composition.

Each time an amount of 1 – 10 coffees are made, a log entry has to be stored. With how much coffees there have been made, and the date time of it.

So I needed the date and time it was NOW, to store. Unfortunately I could’t find easy ways to do this, on line. That’s why I’ve searched a good way, and will share it with everyone, on eurekadesign.be!

[ad#Adsense]

Packages

There are a few packages you will need.

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

Date, is the variable type where in you can store the date and time. Calendar is a system to provide us with the current date and time, in the international calendar system. There is also a GregorianCalendar. SimpleDateFormat is a package to reformat and change our Date into a string.

This may look like a lot of packages, but believe, it is the easiest (and best) way to do it like this. And when you have already imported java.util.*, You do not have to add Date and Calendar any more.

The date!!

How to get the current date?

Date currentDate = Calendar.getInstance().getTime();

The getInstance() returns the current date and time, and the getTime() makes that you can store this in the Date. Next we need to create a custom format.

SimpleDateFormat fullFormat = new SimpleDateFormat("dd-MMMM-yyyy  HH.mm.ss");

Other formats?

Hell yeah! A litlle list and what it returns:

  • dd -> 07
  • MM-> 05
  • MMMM-> february (automatic in the language of local user)
  • yyyy-> 2010
  • yy->10
  • HH-> 8 (am pm format)
  • HHHH-> (0 – 24 format)

Format it to string

String stringTime = fullFormat.format(currentDate);

Test it

Now we simply send it to System.out.println to show that it works!

System.out.println(stringTime);

Now you will see that it works. You could also get our currentDate printed, but that will print a very long string with every information about the time, timezone, … .

Only date?

Just change the format to something like this:

SimpleDateFormat fullFormat = new SimpleDateFormat("dd-MMMM-yyyy");

With the SimpleDateFormat you can really do everything you would like!

Another date?

Of course it is possible. Here below you can see a code example to do that!

Calendar newTime = Calendar.getInstance();
newTime.set(year, month, date, hourOfDay, minute, second);

You have to use like (2007, 12, 35, 11, 30, 35) to add the date you want.

Example application

import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
Date currentDate = Calendar.getInstance().getTime();
SimpleDateFormat fullFormat = new SimpleDateFormat("dd-MMMM-yyyy HH.mm.ss");
String stringTime = fullFormat.format(currentDate);
System.out.println(stringTime);
}
}

Resources

http://www.devdaily.com/java/simpledateformat-convert-date-to-string-formatted-parse
http://www.tutorialhero.com/tutorial-70-java_date.php