Java Date Set setYear(Date d, int year)

Here you can find the source of setYear(Date d, int year)

Description

set Year

License

Open Source License

Declaration

public static Date setYear(Date d, int year) 

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 Date setYear(Date d, int year) {
        Calendar c = getCalendar();
        c.setTime(d);//from w w w.jav  a  2s .c  o m
        c.set(Calendar.YEAR, year);
        return (c.getTime());
    }

    /**
     * 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);
    }

    public static long getTime() {
        Date d = new Date();
        return (d.getTime());
    }

    public static long getTime(Date d) {
        return (d.getTime());
    }
}

Related

  1. setTimeToZero(final Date date)
  2. setTimeZero(Date dt)
  3. setToDayStartTime(Date date)
  4. setToLastDayInMonth(final Date date)
  5. setToMidnight(Date d)
  6. setYear(Date date, int year)
  7. stripTime(Date d)
  8. stripTime(Date date)
  9. stripTime(Date date)