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

- 532
- 2
- 17

- 51
- 3
-
Are you trying to get the current date/time? – Jamie Reid Feb 28 '15 at 18:37
-
Read about Date API in Java....also, remember to read about Joda time, currently quite popular.....usually people using java.util.Date.....`Date date = new Date()`; – ha9u63a7 Feb 28 '15 at 18:38
-
Oracle provide excellent online tutorials for most aspects of Java programming. Start there. – Dawood ibn Kareem Feb 28 '15 at 18:38
-
May be this can help http://stackoverflow.com/questions/5175728/how-to-get-the-current-date-time-in-java and lots of tutorial related to this – Raunak Kathuria Feb 28 '15 at 18:40
-
If you use Java 8 you should probably use the new java time api instead: http://docs.oracle.com/javase/tutorial/datetime/iso/index.html – assylias Feb 28 '15 at 18:41
-
@ Jamie Reid just trying to entirely understand every step. Everyone else, thanks, I'll take a look – Aleksey Galanov Feb 28 '15 at 18:44
2 Answers
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:

- 532
- 2
- 17
Full examples provided in the links below:
1)examples
The java.util.Calendar.getInstance() method gets a calendar using the specified time zone and specified locale.The point of using getInstance() is that it will take the default locale and time-zone into account when deciding which Calendar implementation to return and how to initialize it.

- 376
- 1
- 2
- 12
-
its better to add code snippet in answer itself rather than providing links – Raunak Kathuria Feb 28 '15 at 18:41
-
@Raunak Kathuria In this case man the link have obviously examples with explaining.If it is more information needed by the asker i will provide them. – crAlexander Feb 28 '15 at 18:44
-
I have not downvoted relax. just saying so that next person who comes can get an overview of your answer and if more details required he can open link – Raunak Kathuria Feb 28 '15 at 18:46