Get Day, Month, Year from Dates in Java
Please refer to this post for an explanation of how to create Date objects in Java. The code below builds on the code described there. Unfortunately, it takes a little extra code to find the day, month, or year from a Date object. You have to create a Calendar object. Apparently, the Java developers wanted to make date handling as flexible as possible, but in the process they made coding pretty ugly. In my opinion they should have used the GregorianCalendar as a default and made the coding simpler. But I digress… Below is an example of how you would go about finding the day, month, and year from a date.
Date date1 = Dates.CreateDate(2008, 2, 3); GregorianCalendar cal = new GregorianCalendar(); cal.setTime(date1); System.out.println(cal.get(cal.DAY_OF_MONTH)); // 3 System.out.println(cal.get(cal.MONTH)); // 2 System.out.println(cal.get(cal.YEAR)); // 2008