How do I get day, month, year value from the current date?
Category: java.util, viewed: 12147 time(s).
What day, month, year, day of week, day of month, day of year is today? If we want to answer these question we can use java.util.Calendar and java.util.GregorianCalendar which is the implementation of Calendar abstract class.
These classes can help us to get integer value such as day, month, year from a Date object. Let's see the example code.
import java.util.Calendar; public class CalendarExample { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); int day = cal.get(Calendar.DATE); int month = cal.get(Calendar.MONTH) + 1; int year = cal.get(Calendar.YEAR); int dow = cal.get(Calendar.DAY_OF_WEEK); int dom = cal.get(Calendar.DAY_OF_MONTH); int doy = cal.get(Calendar.DAY_OF_YEAR); System.out.println("Current Date: " + cal.getTime()); System.out.println("Day: " + day); System.out.println("Month: " + month); System.out.println("Year: " + year); System.out.println("Day of Week: " + dow); System.out.println("Day of Month: " + dom); System.out.println("Day of Year: " + doy); } } |
Here is the result of this example:
Current Date: Thu Dec 29 13:41:09 ICT 2005 Day: 29 Month: 12 Year: 2005 Day of Week: 5 Day of Month: 29 Day of Year: 363
Related Examples
- How do I convert milliseconds value to date?
- How do I split a string using Scanner class?
- How do I read file using Scanner class?
- How do I read / write data in Windows registry?
- How do I read user input from console using Scanner class?
- How do I use ResourceBundle for i18n?
- How do I convert time between timezone?
- How do I set a default Locale?
- How do I remove duplicate element from array?
- How do I convert array to Set?
|
|