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

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.