Java Date to Year getYearOnlyDate(String year)

Here you can find the source of getYearOnlyDate(String year)

Description

takes a string containing a 4 digit year and returns a Date object that has the year indicated and all other fields set to 0 (so jan 1 of that year at 00:00:00)

License

Open Source License

Parameter

Parameter Description
year a parameter

Exception

Parameter Description
NumberFormatException an exception

Declaration

public static Date getYearOnlyDate(String year) throws NumberFormatException 

Method Source Code


//package com.java2s;
/*/*from w  ww. j  a  v  a  2s. com*/
 *  Copyright (C) 2010-2012 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

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

public class Main {
    /**
     * takes a string containing a 4 digit year and returns a Date object that has the year
     * indicated and all other fields set to 0 (so jan 1 of that year at 00:00:00)
     * 
     * @param year
     * @return
     * @throws NumberFormatException
     */
    public static Date getYearOnlyDate(String year) throws NumberFormatException {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.MONTH, 0);
        cal.set(Calendar.YEAR, new Integer(year));
        // sets the time portion to 00:00:00 and returns
        return getDateNoTime(cal.getTime());
    }

    /**
     * Returns a new date that is the same as the one passed in except it has a time of 00:00:00
     * 
     * @param oldDate
     * @return
     */
    public static Date getDateNoTime(Date oldDate) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(oldDate);
        // drop the time portion
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }
}

Related

  1. getYearMonthInt(Date date)
  2. getYearMonthOfLastMon(Date date)
  3. getYearNumber(Date date)
  4. getYearOfDate(Date date)
  5. getYearOfThisYear(Date currentdate)
  6. getYears(java.util.Date date0, java.util.Date date1)
  7. getYearStart(Date date)
  8. getYearString(Date date)