Finding the Day of WeekMonthYear or Week Number


Finding the Day of Week/Month/Year or Week Number

Calendar cal = Calendar.getInstance(); System.out.println("Day of week: " +         cal.get(Calendar.DAY_OF_WEEK)); System.out.println("Month: " +         cal.get(Calendar.MONTH)); System.out.println("Year: " +         cal.get(Calendar.YEAR)); System.out.println("Week number: " +         cal.get(Calendar.WEEK_OF_YEAR));



You can easily determine values such as the day of the week, the month, the year, the week number, and more using the Calendar object's get() method. In this phrase, we get a Calendar object representing the current date and time using the getInstance() method. We then print out the day of the week, the month, the year, and the week of the year by using the get() method and passing the appropriate Calendar constant to specify the field we want to get.

If you have a Date object and want to get these values, you could convert the Date object to a Calendar object by using the setTime() method of a Calendar instance and passing in the Date object you want to convert. This is shown in the converting between date and calendar objects phrase earlier in this chapter.




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net