Java Date to Year getYear(Date d)

Here you can find the source of getYear(Date d)

Description

get Year

License

Open Source License

Declaration

public static int getYear(Date d) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    public static int getYear(Date d) {
        Calendar c = getCalendar();
        c.setTime(d);//  w w w .ja  v  a  2  s .c o  m
        return (c.get(Calendar.YEAR));
    }

    /**
     * month ranges from 1 to 12 (not 0 to 11)
     */
    private static Calendar getCalendar() {
        return (getCalendar(null, null));
    }

    private static Calendar getCalendar(TimeZone timeZone, Locale locale) {
        if (timeZone == null)
            timeZone = TimeZone.getDefault();

        if (locale == null)
            locale = Locale.getDefault();

        Calendar c = new GregorianCalendar(timeZone, locale);
        return (c);
    }

    public static Date setTime(Date d, long time) {
        d.setTime(time);
        return (d);
    }
}

Related

  1. getYear(Date aDate)
  2. getYear(Date d)
  3. getYear(Date d)
  4. getYear(Date date)
  5. getYear(Date date)