Java Year From getYear(int num)

Here you can find the source of getYear(int num)

Description

get Year

License

Open Source License

Declaration

public static String getYear(int num) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {

    public static String getYear(int num) {
        Calendar calendar = new GregorianCalendar();
        int year = calendar.get(Calendar.YEAR) + num;
        int month = calendar.get(Calendar.MONTH);
        String yearstr = "";
        if (month < 8) {
            yearstr = String.valueOf(-1) + "/" + String.valueOf(year);
        } else {/*from  w w  w . j a va2s . c  o  m*/
            yearstr = String.valueOf(year) + "/" + String.valueOf(year + 1);
        }
        return yearstr;
    }

    public static String getYear(Date parseDate) {
        if (parseDate == null) {
            return getYear(0);
        }
        String result = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        if (month < 8) {
            result = String.valueOf(year - 1) + "/" + String.valueOf(year);
        } else {
            result = String.valueOf(year) + "/" + String.valueOf(year + 1);
        }
        return result;
    }
}

Related

  1. getYear(boolean fourDigit)
  2. getYear(long t)
  3. getYearBegin(int year)
  4. getYearFirst(int year)
  5. getYearFormat(String dateString)