java gets the week
1. First create a getWeek function that receives a Date type parameter;
2. Then create a GregorianCalendar instance within the function;
3. Then set the time of the GregorianCalendar instance as the received parameter;
4. Finally, use get Just get the week number.
import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class GetWeek { /** * @param args */ public static void main(String[] args) { System.out.println(getWeek(new Date())); } public static int getWeek(Date date) { GregorianCalendar g = new GregorianCalendar(); g.setTime(date); return g.get(Calendar.WEEK_OF_YEAR); //获得周数 } }
Supplement:
The Calendar class implements the Gregorian calendar, and GregorianCalendar is a specific implementation of the Calendar class.
Calendar’s getInstance() method returns a GregorianCalendar object initialized by default with the current locale and time zone.
php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!
The above is the detailed content of java get week. For more information, please follow other related articles on the PHP Chinese website!