Java Get Current Date
There are many ways to get current date and time in java. There are 3 classes that can be used to get current date.
- java.util.Date class
- java.sql.Date class
- java.util.Calendar class
1) Get Current Date by java.util.Date class
By printing the instance of java.util.Date class, you can print current date and time in java. There are two ways to do so.
1st way:
2nd way:
Output:
Thu Mar 26 08:22:02 IST 2015
2) Get Current Date by java.sql.Date class
By printing the instance of java.sql.Date class, you can print current date in java. It doesn't print time. This date instance is generally used to save current date in database.
Output:
2015-03-26
3) Get Current Date by java.util.Calendar class
Calendar class can be used to get the instance of Date class. The getTime() method of Calendar class returns the instance of java.util.Date. The Calendar.getInstance() method returns the instance of Calendar class.
Output:
Thu Mar 26 08:22:02 IST 2015
No comments:
Post a Comment