Home > Java > JavaBase > body text

java get week

angryTom
Release: 2019-11-18 14:59:32
Original
3604 people have browsed it

java get week

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); //获得周数 
    } 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template