Q:

Getting a date in java

I've just started to learn java and can't fully understand every step of getting a date (e.g. Calendar.getInstance().get(Calendar.DATE); . Can someone please explain it to me? Sorry if the question is stupid

A:

In Java, you can get the current date time via following two classes – Date and Calendar. And, later use SimpleDateFormat class to convert the date into a user friendly format.
Using Date() + SimpleDateFormat():
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2014/08/06 15:59:48

Using Calender() + SimpleDateFormat()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22

Here's a full example to show you how to use Date() and Calender() classes to get and display the current date time.
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTime {
  public static void main(String[] args) {

       DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
       //get current date time with Date()
       Date date = new Date();
       System.out.println(dateFormat.format(date));

       //get current date time with Calendar()
       Calendar cal = Calendar.getInstance();
       System.out.println(dateFormat.format(cal.getTime()));

  }
}

Please review the following two manual pages:

Calendar
Date

