Java Year From getYearBegin(int year)

Here you can find the source of getYearBegin(int year)

Description

get Year Begin

License

Apache License

Declaration

public static Date getYearBegin(int year) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import static java.util.Calendar.*;

import java.util.*;

public class Main {
    public static Date getYearBegin(int year) {
        Calendar ca = Calendar.getInstance();
        truncate(ca);/*www .  ja v  a 2 s.  c  o  m*/
        ca.set(DAY_OF_MONTH, 1);
        ca.set(MONTH, 0);
        ca.set(YEAR, year);
        return ca.getTime();
    }

    /**
     * Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond
     * @param d The date to be truncated
     * @return the result truncated date
     */
    public static Date truncate(Date d) {
        Calendar ca = Calendar.getInstance();
        ca.setTime(d);
        truncate(ca);
        return ca.getTime();
    }

    /**
     * Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond
     * @param d The date to be truncated
     * @return the result truncated date
     */
    public static Date truncate(Date d, TimeZone zone) {
        Calendar ca = Calendar.getInstance(zone);
        ca.setTime(d);
        truncate(ca);
        return ca.getTime();
    }

    /**
     * Truncate to begin of day: 0 hour 0 minute 0 second 0 millisecond
     * @param ca The calendar to be truncated
     */
    public static Calendar truncate(Calendar ca) {
        truncateHour(ca);
        ca.set(Calendar.HOUR_OF_DAY, 0);
        return ca;
    }

    public static void truncateHour(Calendar ca) {
        ca.set(Calendar.MILLISECOND, 0);
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.MINUTE, 0);
    }
}

Related

  1. getYear(boolean fourDigit)
  2. getYear(int num)
  3. getYear(long t)
  4. getYearFirst(int year)
  5. getYearFormat(String dateString)
  6. getYearFromDay(int days)
  7. getYearFromISODate(String isoDate)